Can someone explain this JS code to me please?
Maybe line by line? I am trying to understand and learn from it. Is this code creating a raw transaction based on an existing redeemScript? Or is it actually creating a redeemScript and then a transaction based on it?
I am on Windows server 2016 with Bitcoin Core and node installed.
Particulary this line I don't get it.
var scriptSig = bitcore.Script.fromASM('04678afd04678a ' + redeemScript.toHex())
What is 04678afd04678a
?:
He is the entire code, found it on some JS examples from 2 years ago. It's about creating unsecured P2SH transactions.
> var bitcore = require('bitcore');
undefined
> var redeemScript = bitcore.Script.fromASM('OP_SHA256 894eeb82f9a851f5d1cb1be324
9f58bc8d259963832c5e7474a76f7a859ee95c OP_EQUAL');
undefined
> var scriptPubKey = redeemScript.toScriptHashOut();
undefined
> scriptPubKey
<Script: OP_HASH160 20 0x028d07ac41994c915b76e00490289f5933454f90 OP_EQUAL>
> scriptPubKey.toAddress()
<Address: 31vWDrvuRXaEaymhyuZqkbNBkPrVzw6DBc, type: scripthash, network: livenet>
> scriptPubKey.toHex()
'a914028d07ac41994c915b76e00490289f5933454f9087'
> var utxo = new bitcore.Transaction.UnspentOutput({
... "txId" : "a0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9"
, //fake, don't try!
... "outputIndex" : 0,
... "script" : "a914028d07ac41994c915b76e00490289f5933454f9087",
... "satoshis" : 60000,
... "address" : "31vWDrvuRXaEaymhyuZqkbNBkPrVzw6DBc"
... });
> var address = '13mDYExLFx7stcYJN5uJRhzxwLuTEKNJbg';
undefined
> var tx = new bitcore.Transaction().from(utxo).to(address, 50000);
undefined
> tx.inputs[0]
{ output: <Output (60000 sats) <Script: OP_HASH160 20 0x028d07ac41994c915b76e0049
0289f5933454f90 OP_EQUAL>>,
prevTxId: <Buffer a0 a0 8e 39 72 03 df 68 39 2e e9 5b 3f 08 b0 b3 b3 e2 40 14 1
0 a3 8d 46 ae 08 74 f7 48 46 f2 e9>,
outputIndex: 0,
sequenceNumber: 4294967295,
_script: <Script: >,
_scriptBuffer: <Buffer > }
> tx.outputs
[ <Output (50000 sats) <Script: OP_DUP OP_HASH160 20 0x1e4d054693c02c60e3f77f87f5
ba79a281bf9141 OP_EQUALVERIFY OP_CHECKSIG>> ]
> var scriptSig = bitcore.Script.fromASM('04678afd04678a ' + redeemScript.toHex()
)
undefined
> tx.inputs[0].setScript(scriptSig);
{ output: <Output (60000 sats) <Script: OP_HASH160 20 0x028d07ac41994c915b76e0049
0289f5933454f90 OP_EQUAL>>,
prevTxId: <Buffer a0 a0 8e 39 72 03 df 68 39 2e e9 5b 3f 08 b0 b3 b3 e2 40 14 1
0 a3 8d 46 ae 08 74 f7 48 46 f2 e9>,
outputIndex: 0,
sequenceNumber: 4294967295,
_script: <Script: 7 0x04678afd04678a 35 0xa820894eeb82f9a851f5d1cb1be3249f58bc8
d259963832c5e7474a76f7a859ee95c87>,
_scriptBuffer: <Buffer 07 04 67 8a fd 04 67 8a 23 a8 20 89 4e eb 82 f9 a8 51 f5
d1 cb 1b e3 24 9f 58 bc 8d 25 99 63 83 2c 5e 74 74 a7 6f 7a 85 9e e9 5c 87> }
> var rawTx = tx.toString('hex');
undefined
> rawTx
'0100000001e9f24648f77408ae468da3101440e2b3b3b0083f5be92e3968df0372398ea0a0000000
002c0704678afd04678a23a820894eeb82f9a851f5d1cb1be3249f58bc8d259963832c5e7474a76f7
a859ee95c87ffffffff0150c30000000000001976a9141e4d054693c02c60e3f77f87f5ba79a281bf
914188ac00000000'
>
On this line
> var scriptSig = bitcore.Script.fromASM('04678afd04678a ' + redeemScript.toHex()
the046xxx
is just a string (can be any string) which is currently hashed so it becomes equal to894eeb82f9a851f5d1xxxxx
or is the other way around where this894eexxxxxxxxxx
is the hash and the04678axx
is the redeemScript?What do you mean by "a miner can change the script"? Once the tx gets mined the redeemScript is revealed so it becomes vulnerable? I imagined that once the script is hashed that's irreversible.