Created
February 18, 2023 03:25
-
-
Save zhfnjust/8acd274dfe3b1da5852795f394d09e16 to your computer and use it in GitHub Desktop.
Put a signature in the output so that this output cannot be copied to other tx
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
const { expect } = require('chai'); | |
const { bsv, signTx, toHex } = require('scryptlib'); | |
const crypto = require('crypto'); | |
/** | |
* an example test for contract containing signature verification | |
*/ | |
const { inputSatoshis, dummyTxId } = require('../../helper'); | |
const privateKey = bsv.PrivateKey.fromRandom('testnet') | |
describe('OP_CHECKSIG with OP_CODESEPARATOR', () => { | |
it('Put a signature in the output so that this output cannot be copied to other tx', () => { | |
const tx = new bsv.Transaction().from({ | |
txId: dummyTxId, | |
outputIndex: 0, | |
script: '', // placeholder | |
satoshis: inputSatoshis | |
}); | |
const script = new bsv.Script("OP_CHECKSIGVERIFY"); | |
sig = signTx(tx, privateKey, script, inputSatoshis) // if sign with fake key, it will fails | |
const lockingScript = new bsv.Script(); | |
lockingScript.add(Buffer.from(sig, "hex")) | |
.add(Buffer.from(toHex(privateKey.publicKey), "hex")) | |
.add(bsv.Opcode.OP_CODESEPARATOR) | |
.add(bsv.Opcode.OP_CHECKSIGVERIFY) | |
// set ScriptPubKey | |
tx.inputs[0].output.script.add(lockingScript) | |
// set ScriptSig | |
tx.inputs[0].script.add(bsv.Opcode.OP_TRUE) | |
let result = tx.verifyInputScript(0); // should verify true | |
expect(result.success, result.error).to.be.true | |
const faketx = new bsv.Transaction().from({ | |
txId: crypto.randomBytes(32).toString('hex'), //with a different txId | |
outputIndex: 0, | |
script: '', // placeholder | |
satoshis: inputSatoshis | |
}); | |
// same ScriptPubKey | |
faketx.inputs[0].output.script.add(lockingScript) | |
// same ScriptSig | |
faketx.inputs[0].script.add(bsv.Opcode.OP_TRUE) | |
result = faketx.verifyInputScript(0); // should verify true | |
expect(result.success, result.error).to.be.false | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment