-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathp2pkh.js
More file actions
55 lines (42 loc) · 2.14 KB
/
p2pkh.js
File metadata and controls
55 lines (42 loc) · 2.14 KB
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
import { URL } from 'url';
import { compileFile } from 'cashc';
import { ElectrumNetworkProvider, Contract, SignatureTemplate, TransactionBuilder } from 'cashscript';
import { stringify } from '@bitauth/libauth';
// Import Alice's keys from common.ts
import { alicePkh, alicePriv, aliceAddress, alicePub } from './common.js';
// Compile the P2PKH contract to an artifact object
const artifact = compileFile(new URL('p2pkh.cash', import.meta.url));
// Initialise a network provider for network operations on CHIPNET
const provider = new ElectrumNetworkProvider('chipnet');
// 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 });
// Get contract balance & output address + balance
console.log('contract address:', contract.address);
const contractUtxos = await contract.getUtxos();
console.log('contract utxos:', contractUtxos);
console.log('contract balance:', await contract.getBalance());
// Select a contract UTXO to spend from
const contractInputUtxo = contractUtxos.find(utxo => utxo.satoshis > 12_000n);
if (!contractInputUtxo) throw new Error('No contract UTXO with enough satoshis found');
const inputAmount = contractInputUtxo.satoshis;
// construct an output
const outputAmount = 10_000n;
const output = { amount: outputAmount, to: aliceAddress };
// Construct a changeOutput
const minerFee = 1000n;
const changeAmount = inputAmount - outputAmount - minerFee;
const changeOutput = {
amount: changeAmount,
to: contract.address,
};
const aliceTemplate = new SignatureTemplate(alicePriv);
// Call the spend() function with alice's signature + pk
// And use it to send 0. 000 100 00 BCH to aliceAddress and the remainder
// back to the contract's address
const transactionBuilder = new TransactionBuilder({ provider });
transactionBuilder.addInput(contractInputUtxo, contract.unlock.spend(alicePub, aliceTemplate));
transactionBuilder.addOutput(output);
if (changeAmount > 1000n) transactionBuilder.addOutput(changeOutput);
const tx = await transactionBuilder.send();
console.log('transaction details:', stringify(tx));