Advertisement
Guest User

scrypt test 2

a guest
Feb 16th, 2020
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const bsv = require('bsv');
  3. var fs = require('fs');
  4. var path = require('path');
  5. let BN = bsv.crypto.BN
  6.  
  7. __dirname = path.resolve();
  8. function int2SM(num) {
  9.     let n = BN.fromNumber(num)
  10.     let m = n.toSM({ endian: 'little'} )
  11.     return m.toString('hex')
  12. }
  13.  
  14. (async() => {
  15.  
  16. const NETWORK = 'test'
  17. const API_PREFIX = 'https://api.whatsonchain.com/v1/bsv/' + NETWORK
  18. const MIN_FEE = 546
  19. const LOCK_AMOUNT = 100000
  20. const UNLOCK_AMOUNT = LOCK_AMOUNT - 10000
  21. const FLAGS = bsv.Script.Interpreter.SCRIPT_VERIFY_MINIMALDATA | bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID | bsv.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bsv.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES
  22. const SIGHASH_TYPE = bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID
  23.  
  24. // mainnet
  25. // const key = "L1HBjGiu9W5tsu7axNKRY1cz4xdLuwEnAC7u3b6n2Ex98nNh6UvK"
  26. // testnet
  27. const key = 'cU2eUM62Hkur8sQVv7z1VtkXHHTA1YSGqxjAGYgbDPQaYcg5NXKd'
  28. // const key = 'cPkteUUh4eG1mjkhphV1MZWm7JmYZuK4j8LaDpPwo3mxcZW49ibj'
  29. // const key = 'cTzbQjXykayjM7rkERDeyf9h6V9DjsHqv7twhtU78EkqksQ95LJD'
  30. const privateKey = new bsv.PrivateKey(key);
  31. const ADDR = privateKey.toAddress();
  32. // console.log(address.toString()) //'mpRPo9bMQ1HaZetSL7EfaDuzfdLyJ3PVoy' 'mg1Rwf7NTTbgRUYAFiqJk1ryaKx74Y7zDf' 'myfKHweiqRe4z1KRXBEkud3FteCQn23wEr'
  33. const scriptP2PKH = bsv.Script.buildPublicKeyHashOut(ADDR).toHex();
  34. // console.log(script) // 76a91461abec89cd14603c8c7d45f679e93ac0beea866288ac
  35.  
  36. const fetchUtxos = async (address) => {
  37.     let { data: utxos } = await axios.get(`${API_PREFIX}/address/${ADDR}/unspent`)
  38.     utxos = utxos.map(utxo => ({
  39.         txId: utxo.tx_hash,
  40.         outputIndex: utxo.tx_pos,
  41.         satoshis: utxo.value,
  42.         script: scriptP2PKH
  43.     }))
  44.     return utxos
  45. }
  46.  
  47. // lock fund in a script
  48. const buildScriptLockTx = (utxos, scriptPubKey) => {
  49.     let tx = new bsv.Transaction().from(utxos)
  50.     tx.addOutput(new bsv.Transaction.Output({
  51.         script: scriptPubKey,
  52.         satoshis: LOCK_AMOUNT
  53.     }))
  54.     tx.addOutput(new bsv.Transaction.Output({
  55.         script: new bsv.Script().add(bsv.Opcode.OP_FALSE).add(bsv.Script.buildDataOut(['sCrypt: the UTXO above (vout[0]) cannot be spent before CoinGeek London 2020'])),
  56.         satoshis: 546
  57.     }))
  58.     tx.change(ADDR)
  59.     if (tx.getFee() < MIN_FEE) {
  60.         tx.fee(MIN_FEE)
  61.     }
  62.     return tx.sign(privateKey)
  63. }
  64.  
  65. // unlock fund from a script
  66. const buildScriptUnlockTx = (prevTxId, scriptPubKey) => {
  67.     tx = new bsv.Transaction().addInput(new bsv.Transaction.Input({
  68.         prevTxId: prevTxId,
  69.         outputIndex: 0,
  70.         script: new bsv.Script(),   // placeholder
  71.     }), scriptPubKey, LOCK_AMOUNT)
  72.     tx.addOutput(new bsv.Transaction.Output({
  73.         script: scriptPubKey,
  74.         satoshis: UNLOCK_AMOUNT
  75.     }))
  76.     tx.nLockTime = 1582156800
  77.     // no need to sign since scriptSig is already set
  78.     return tx
  79. }
  80.  
  81. const sendTx = async (txhex) => {
  82.     const { data: txid } = await axios.post(`${API_PREFIX}/tx/raw`, {
  83.         txhex: txhex
  84.     })
  85.     return txid
  86. }
  87.  
  88. try {
  89.     const scriptPubKeyStr = fs.readFileSync(path.join(__dirname, 'cltv_asm.json'), { encoding: 'utf8' })
  90.     const scriptPubKey = bsv.Script.fromASM(scriptPubKeyStr)
  91.  
  92.     // step 1: fetch utxos
  93.     const utxos = await fetchUtxos(ADDR)
  94.     // console.log('utxos: ', utxos)
  95.    
  96.     // step 2: build the locking tx and sign it
  97.     const lockingTx = buildScriptLockTx(utxos, scriptPubKey)
  98.    
  99.     // step 3: serialize and broadcast the locking tx
  100.     const lockingTxid = await sendTx(lockingTx.serialize())
  101.     console.log('locking tx id:     ', lockingTxid)
  102.  
  103.     // step 4: build the unlocking tx
  104.     const unlockingTx = await buildScriptUnlockTx(lockingTxid, scriptPubKey)
  105.     const INPUT_IDX = 0
  106.     const preimage = bsv.Transaction.sighash.sighashPreimage(unlockingTx, SIGHASH_TYPE, INPUT_IDX, scriptPubKey, new bsv.crypto.BN(LOCK_AMOUNT), FLAGS)
  107.     const preimageASM = preimage.toString('hex')
  108.     const scriptSigStr = preimageASM
  109.     const scriptSig = bsv.Script.fromASM(scriptSigStr)
  110.     unlockingTx.inputs[INPUT_IDX].setScript(scriptSig)
  111.  
  112.     // step 5: serialized and send it
  113.     const unlockingTxid = await sendTx(unlockingTx.serialize())
  114.     console.log('unlocking tx id:   ', unlockingTxid)
  115.  
  116. } catch (error) {
  117.     // Error
  118.     if (error.response) {
  119.         // The request was made and the server responded with a status code
  120.         // that falls out of the range of 2xx
  121.         console.log('Failed - StatusCodeError: ' + error.response.status + ' - "' + error.response.data + '"')
  122.         // console.log(error.response.headers);
  123.     } else if (error.request) {
  124.         // The request was made but no response was received
  125.         // `error.request` is an instance of XMLHttpRequest in the
  126.         // browser and an instance of
  127.         // http.ClientRequest in node.js
  128.         console.log(error.request);
  129.     } else {
  130.         // Something happened in setting up the request that triggered an Error
  131.         console.log('Error', error.message);
  132.     }
  133.     console.log(error.config);
  134. }
  135.  
  136.  
  137. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement