forked from CashScript/cashscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2pkh.ts
48 lines (40 loc) · 1.84 KB
/
p2pkh.ts
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
44
45
46
47
48
import { BITBOX } from 'bitbox-sdk';
import { TxnDetailsResult } from 'bitcoin-com-rest';
import { Contract, SignatureTemplate } from 'cashscript';
import path from 'path';
run();
async function run(): Promise<void> {
// Initialise BITBOX
const network = 'testnet';
const bitbox = new BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
// Initialise HD node and alice's keypair
const rootSeed = bitbox.Mnemonic.toSeed('CashScript');
const hdNode = bitbox.HDNode.fromSeed(rootSeed, network);
const alice = bitbox.HDNode.toKeyPair(bitbox.HDNode.derive(hdNode, 0));
// Derive alice's public key and public key hash
const alicePk = bitbox.ECPair.toPublicKey(alice);
const alicePkh = bitbox.Crypto.hash160(alicePk);
// Compile the P2PKH Cash Contract
const P2PKH = Contract.compile(path.join(__dirname, 'p2pkh.cash'), network);
// Instantiate a new P2PKH contract with constructor arguments: { pkh: alicePkh }
const instance = P2PKH.new(alicePkh);
// Get contract balance & output address + balance
const contractBalance = await instance.getBalance();
console.log('contract address:', instance.address);
console.log('contract balance:', contractBalance);
// Call the spend function with alice's signature + pk
// And use it to send 0. 000 100 00 BCH back to the contract's address
const tx = await instance.functions
.spend(alicePk, new SignatureTemplate(alice))
.to(instance.address, 10000)
.send();
console.log('transaction details:', tx);
// Call the spend function with alice's signature + pk
// And use it to send two outputs of 0. 000 150 00 BCH back to the contract's address
const tx2: TxnDetailsResult = await instance.functions
.spend(alicePk, new SignatureTemplate(alice))
.to(instance.address, 15000)
.to(instance.address, 15000)
.send();
console.log('transaction details:', tx2);
}