-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp2pkh.ts
83 lines (67 loc) · 2.53 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { BITBOX } from 'bitbox-sdk';
import { stringify } from '@bitauth/libauth';
import {
Contract,
SignatureTemplate,
ElectrumNetworkProvider,
} from 'cashscript';
import { compileFile } from 'cashc';
import path from 'path';
import { createElectrumTestnetProvider, rawTxToStr } from './utils/utils';
// Initialise BITBOX
const bitbox = new BITBOX();
// Initialise HD node and alice's keypair
const rootSeed = bitbox.Mnemonic.toSeed('smartBCH_faucet');
const hdNode = bitbox.HDNode.fromSeed(rootSeed);
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);
// Initialise a network provider for network operations on TESTNET
const provider = createElectrumTestnetProvider();
// Compile the P2PKH contract to an artifact object
const artifact = compileFile(path.join(__dirname, 'p2pkh.cash'));
// Instantiate a new contract using the compiled artifact and network provider
// AND providing the constructor parameters (pkh: alicePkh)
const contract = new Contract(artifact, [alicePkh], provider);
run();
async function run(): Promise<void> {
if (process.argv.length < 4) {
console.log('Usage: ts-node p2pkh.ts <address> <amt> [op_return_data]');
console.log('');
await printInfo();
return;
}
const addr = process.argv[2];
const amt = process.argv[3];
let opRetData = "";
if (process.argv.length > 4) {
opRetData = process.argv[4];
}
await sendTo(addr, +amt, opRetData);
}
async function printInfo() {
// Get contract balance & output address + balance
console.log('contract address:', contract.address);
// console.log('contract balance:', (await contract.getBalance()) / 10**8);
const utxos = await provider.getUtxos(contract.address);
const sum = utxos.reduce((partialSum, utxo) => partialSum + utxo.satoshis / 10**8, 0);
console.log('UTXOs:', utxos.length);
console.log('balance:', sum);
console.table(utxos);
}
async function sendTo(addr: string, amt: number, opRetData: string): Promise<void> {
await printInfo();
// Call the spend() function with alice's signature + pk
// And use it to send BCH to addr
let tx = contract.functions
.spend(alicePk, new SignatureTemplate(alice))
.to(addr, amt);
if (opRetData.length > 0) {
tx = tx.withOpReturn([opRetData]);
}
// const rawTx = await tx.build();
// console.log('rawTx', rawTx);
const td = await tx.send();
console.log('transaction details:', stringify(td));
}