Last active
November 7, 2020 01:47
-
-
Save zhfnjust/5bef4285c7eead1439deabc1a9ba4a65 to your computer and use it in GitHub Desktop.
p2pkh unit test
This file contains hidden or 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, buildContractClass, Ripemd160, Sig, PubKey, signTx, toHex } = require('scryptlib'); | |
| /** | |
| * an example test for contract containing signature verification | |
| */ | |
| const { compileContract, inputIndex, inputSatoshis, tx } = require('../../helper'); | |
| const privateKey = new bsv.PrivateKey.fromRandom('testnet') | |
| const publicKey = privateKey.publicKey | |
| const pkh = bsv.crypto.Hash.sha256ripemd160(publicKey.toBuffer()) | |
| const privateKey2 = new bsv.PrivateKey.fromRandom('testnet') | |
| describe('Test sCrypt contract DemoP2PKH In Javascript', () => { | |
| let demo, sig, context | |
| before(() => { | |
| const DemoP2PKH = buildContractClass(compileContract('p2pkh.scrypt')) | |
| demo = new DemoP2PKH(new Ripemd160(toHex(pkh))) | |
| // any contract that includes checkSig() must be verified in a given context | |
| context = { tx, inputIndex, inputSatoshis } | |
| }); | |
| it('signature check should succeed when right private key signs', () => { | |
| sig = signTx(tx, privateKey, demo.lockingScript.toASM(), inputSatoshis) | |
| result = demo.unlock(new Sig(toHex(sig)), new PubKey(toHex(publicKey))).verify(context) | |
| expect(result.success, result.error).to.be.true | |
| /* | |
| * print out parameters used in debugger, see ""../.vscode/launch.json" for an example | |
| console.log(toHex(pkh)) | |
| console.log(toHex(sig)) | |
| console.log(toHex(publicKey)) | |
| console.log(tx.toString()) | |
| */ | |
| }); | |
| it('signature check should fail when wrong private key signs', () => { | |
| sig = signTx(tx, privateKey2, demo.lockingScript.toASM(), inputSatoshis) | |
| result = demo.unlock(new Sig(toHex(sig)), new PubKey(toHex(publicKey))).verify(context) | |
| 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