2

How can I calculate the output id with transaction id and output index?

An OutputId is the the concatenation of transactionId+outputIndex where outputIndex needs to be converted to little endian first. Hex-encoded with 0x prefix. (The transactionId is the BLAKE2b-256 hash of the transaction payload bytes).

2 Answers 2

2

An example OutputId is 0x0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a790000:

Hex-prefix TransactionId OutputIndex OutputIndex Decimal
0x 0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 0000 0
0x 0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 0100 1
0x 0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 0200 2
0x 0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 2a00 42
0x 0c78e998f5177834ecb3bae1596d5056af76e487386eecb19727465b4be86a79 8000 128

Code examples to hex encoded the OutputIndex:

Rust:

fn main() {
    fn output_index_to_hex(index: u16) -> String {
        let bytes = u16::to_le_bytes(index);

        bytes
            .iter()
            .map(|b| format!("{b:02x}"))
            .collect::<Vec<String>>()
            .join("")
    }

    let output_index_hex = output_index_to_hex(0);
    println!("{output_index_hex}");
}

Python:

def output_index_to_hex(index: int) -> str:
    return index.to_bytes(2, "little").hex()

print(output_index_to_hex(0))

JS:

// Works in browser and Node.js
function outputIndexToHex(number) {
    let buf = new ArrayBuffer(2);
    const view = new DataView(buf);
    view.setUint16(0, number, true)
    return [...new Uint8Array(buf)].map(x => x.toString(16).padStart(2, '0'))
        .join('')
}
console.log(outputIndexToHex(0))

// Cleaner version, but Node.js only
function outputIndexToHexNodeJs(number) {
    let buf = Buffer.allocUnsafe(2);
    buf.writeInt16LE(number)
    return buf.toString('hex')
}
console.log(outputIndexToHexNodeJs(0))

The reverse operation in JS:

function hexOutputIndexToInt(number) {
    const numberString = number.toString();
    let chunks = [];
    for (let i = 0, charsLength = numberString.length; i < charsLength; i += 2) {
        chunks.push(numberString.substring(i, i + 2));
    }
    const separated = chunks.map(n => parseInt(n, 16))
    const buf = Uint8Array.from(separated).buffer;
    const view = new DataView(buf);
    return view.getUint16(0, true)
}
0

Qt/C++ will be

to get the transaction id:

auto trpay=Payload::Transaction(essence,unlocks);
auto transactionId=trpay->get_id();
auto outputId=transactionId.append(0)
//auto outputId=transactionId.append(128)
qDebug()<<outputId.toHex();

To get the hex string:

qblocks::c_array output_index_to_hex(quint16 index)
{
    c_array var;
    var.append(index);
    return var;
}
qDebug()<<output_index_to_hex(0).toHex();
qDebug()<<output_index_to_hex(1).toHex();
qDebug()<<output_index_to_hex(2).toHex();
qDebug()<<output_index_to_hex(42).toHex();
qDebug()<<output_index_to_hex(128).toHex();

But I Have never used the later code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.