import * as bsv from '@ts-bitcoin/core';
export function rawTxToTxo(transaction, options) {
    const gene = bsv.Tx.fromHex(transaction);
    //   const t = gene.toObject();
    const inputs = [];
    const outputs = [];
    if (gene.txIns) {
        gene.txIns.forEach(function (input, input_index) {
            if (input.script) {
                const xput = { i: input_index, seq: input.nSequence };
                input.script.chunks.forEach(function (c, chunk_index) {
                    if (c.buf) {
                        if (c.buf.byteLength >= 1000000) {
                            xput['xlb' + chunk_index] = c.buf.toString('base64');
                        }
                        else if (c.buf.byteLength >= 512 && c.buf.byteLength < 1000000) {
                            xput['lb' + chunk_index] = c.buf.toString('base64');
                        }
                        else {
                            xput['b' + chunk_index] = c.buf.toString('base64');
                        }
                        if (options && options.h && options.h > 0) {
                            xput['h' + chunk_index] = c.buf.toString('hex');
                        }
                    }
                    else {
                        if (typeof c.opCodeNum !== 'undefined') {
                            xput['b' + chunk_index] = {
                                op: c.opCodeNum,
                            };
                        }
                        else {
                            xput['b' + chunk_index] = c;
                        }
                    }
                });
                const sender = {
                    h: input.txHashBuf.toString('hex'),
                    i: input.txOutNum,
                };
                // const address = input.script.toAddress(bsv.Networks.livenet.name).toString();
                const pubkey = input.script.chunks[1].buf;
                // const hashpubkey = bsv.Hash.sha256Ripemd160(pubkey!);
                const address = bsv.Address.fromPubKey(bsv.PubKey.fromBuffer(pubkey));
                if (address && address.toString().length > 0) {
                    sender.a = address.toString();
                    sender.p = pubkey === null || pubkey === void 0 ? void 0 : pubkey.toString('hex');
                }
                xput.e = sender;
                inputs.push(xput);
            }
        });
    }
    if (gene.txOuts) {
        gene.txOuts.forEach(function (output, output_index) {
            if (output.script) {
                const xput = { i: output_index };
                output.script.chunks.forEach(function (c, chunk_index) {
                    if (c.buf) {
                        if (c.buf.byteLength >= 1000000) {
                            xput['xlb' + chunk_index] = c.buf.toString('base64');
                            xput['xls' + chunk_index] = c.buf.toString('utf8');
                        }
                        else if (c.buf.byteLength >= 512 && c.buf.byteLength < 1000000) {
                            xput['lb' + chunk_index] = c.buf.toString('base64');
                            xput['ls' + chunk_index] = c.buf.toString('utf8');
                        }
                        else {
                            xput['b' + chunk_index] = c.buf.toString('base64');
                            xput['s' + chunk_index] = c.buf.toString('utf8');
                        }
                        if (options && options.h && options.h > 0) {
                            xput['h' + chunk_index] = c.buf.toString('hex');
                        }
                    }
                    else {
                        if (typeof c.opCodeNum !== 'undefined') {
                            xput['b' + chunk_index] = {
                                op: c.opCodeNum,
                            };
                        }
                        else {
                            xput['b' + chunk_index] = c;
                        }
                    }
                });
                const receiver = {
                    v: output.valueBn.toNumber(),
                    i: output_index,
                    a: '',
                };
                if (isPublicKeyHashOut(output.script)) {
                    const address = bsv.Address.fromPubKeyHashBuf(output.script.chunks[2].buf);
                    receiver.a = address.toString();
                }
                xput.e = receiver;
                outputs.push(xput);
            }
        });
    }
    const r = {
        tx: { h: gene.hash().toString('utf-8') },
        in: inputs,
        out: outputs,
        lock: gene.nLockTime,
    };
    // confirmations
    if (options && options.confirmations) {
        r.confirmations = options.confirmations;
    }
    return r;
}
function isPublicKeyHashOut(script) {
    return !!(script.chunks.length === 5 &&
        script.chunks[0].opCodeNum === bsv.OpCode.OP_DUP &&
        script.chunks[1].opCodeNum === bsv.OpCode.OP_HASH160 &&
        script.chunks[2].buf &&
        script.chunks[2].buf.length === 20 &&
        script.chunks[3].opCodeNum === bsv.OpCode.OP_EQUALVERIFY &&
        script.chunks[4].opCodeNum === bsv.OpCode.OP_CHECKSIG);
}
//# sourceMappingURL=txo.js.map