/
p2pkh.scrypttest.js
43 lines (37 loc) · 1.71 KB
/
p2pkh.scrypttest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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, newTx } = 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')
const tx = newTx();
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
});
});