Created
July 19, 2017 19:30
-
-
Save ilyavf/0306552350a504d41aaabd8e512baf2d to your computer and use it in GitHub Desktop.
Blockchain transaction with OP_RETURN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"result": { | |
"txid": "62fe533dc5a506716affa362ca5cb7bba5232150ce5c676c56d6be6769022c52", | |
"hash": "62fe533dc5a506716affa362ca5cb7bba5232150ce5c676c56d6be6769022c52", | |
"size": 264, | |
"vsize": 264, | |
"version": 1, | |
"locktime": 0, | |
"vin": [ | |
{ | |
"txid": "2d7a9f0534ddac231ef1978bda388791c32321f7e14e18e7df3bbed261615f54", | |
"vout": 0, | |
"scriptSig": { | |
"asm": "3045022100e7f32b8d7c260babaaf7cf022e6fb80397ca348854f274e16e74114e148872c302202ec0a57d67a99c656b23015afd0e58c9119b394b7e3eb5e09451ab7969e0894d[ALL] 033701fc7f242ae2dd63a18753518b6d1425e53496878924b6c0dc08d800af46ad", | |
"hex": "483045022100e7f32b8d7c260babaaf7cf022e6fb80397ca348854f274e16e74114e148872c302202ec0a57d67a99c656b23015afd0e58c9119b394b7e3eb5e09451ab7969e0894d0121033701fc7f242ae2dd63a18753518b6d1425e53496878924b6c0dc08d800af46ad" | |
}, | |
"sequence": 4294967295 | |
} | |
], | |
"vout": [ | |
{ | |
"value": 0, | |
"n": 0, | |
"scriptPubKey": { | |
"asm": "OP_RETURN 68656c6c6f206f6e652074776f20746872656520666f757220666976652073697820736576656e2065697468206e696e652074656e206576656c656e2e", | |
"hex": "6a3d68656c6c6f206f6e652074776f20746872656520666f757220666976652073697820736576656e2065697468206e696e652074656e206576656c656e2e", | |
"type": "nulldata" | |
} | |
}, | |
{ | |
"value": 1, | |
"n": 1, | |
"scriptPubKey": { | |
"asm": "OP_DUP OP_HASH160 61ca8116d03694952a3ad252d53c695da7d95f61 OP_EQUALVERIFY OP_CHECKSIG", | |
"hex": "76a91461ca8116d03694952a3ad252d53c695da7d95f6188ac", | |
"reqSigs": 1, | |
"type": "pubkeyhash", | |
"addresses": [ | |
"mpS2RuNkAEALvMhksCa6fPpLVb5yCPanLu" | |
] | |
} | |
} | |
] | |
}, | |
"error": null, | |
"id": null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bitcoin from "bitcoinjs-lib" | |
// buildTransaction :: KeyPair -> Array<Input> -> Array<Output> -> String | |
function buildTransaction (keyPair, inputs, outputs) { | |
const tx = new bitcoin.TransactionBuilder(bitcoin.networks.testnet) | |
inputs.forEach(([inputId, inputIndex]) => tx.addInput(inputId, inputIndex)) | |
outputs.forEach(([output, value]) => tx.addOutput(output, value)) | |
tx.sign(0, keyPair) | |
return tx.build().toHex() | |
} | |
// Data to be stored in the OP_RETURN output: | |
const data = "Some data ... e.g. stringified JSON" | |
const opData = bitcoin.script.compile([ | |
OPS.OP_RETURN, | |
data | |
]) | |
// User's keyPair: | |
const keyPair = bitcoin.ECPair.fromWIF("cNY7A5QT4ZzLvYpaQ4JCUExaSMXPLEz9KpQWtczejyQBrzzxRSaR") | |
// Transaction as a HEX string: | |
const transactionWithOpReturn = buildTransaction( | |
keyPair, | |
[ | |
['2d7a9f0534ddac231ef1978bda388791c32321f7e14e18e7df3bbed261615f54', 0] | |
], | |
[ | |
[opData, 0], | |
['mpS2RuNkAEALvMhksCa6fPpLVb5yCPanLu', 1 * 100000000] | |
] | |
); | |
console.log('OP_RETURN transaction:', transactionWithOpReturn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0100000001545f6161d2be3bdfe7184ee1f72123c3918738da8b97f11e23acdd34059f7a2d010000006b483045022100e7f32b8d7c260babaaf7cf022e6fb80397ca348854f274e16e74114e148872c302202ec0a57d67a99c656b23015afd0e58c9119b394b7e3eb5e09451ab7969e0894d0121033701fc7f242ae2dd63a18753518b6d1425e53496878924b6c0dc08d800af46adffffffff0200000000000000003f6a3d68656c6c6f206f6e652074776f20746872656520666f757220666976652073697820736576656e2065697468206e696e652074656e206576656c656e2e00e1f505000000001976a91461ca8116d03694952a3ad252d53c695da7d95f6188ac00000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment