forked from samrockcash/cashscript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslp_genesis.ts
64 lines (55 loc) · 2.2 KB
/
slp_genesis.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
import { BITBOX } from 'bitbox-sdk';
import { Contract, SignatureTemplate, ElectrumNetworkProvider } from 'cashscript';
import { compileFile } from 'cashc';
import path from 'path';
import { stringify } from '@bitauth/libauth';
run();
async function run(): Promise<void> {
// Initialise BITBOX
const bitbox = new BITBOX();
// Initialise HD node and alice's keypair
const rootSeed = bitbox.Mnemonic.toSeed('CashScript');
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);
// Compile the P2PKH contract to an artifact object
const artifact = compileFile(path.join(__dirname, 'p2pkh.cash'));
// Initialise a network provider for network operations on MAINNET
const provider = new ElectrumNetworkProvider('mainnet');
// 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);
console.log('contract balance:', await contract.getBalance());
// Call the spend function with alice's signature + pk
// And use it to create a new token.
// Output #0 - OP RETURN
// Output #1 - Mint 0.00000001 tokens
// Output #2 - The mint baton
// Output #3 - Change output (implicit)
try {
const tx = await contract.functions
.spend(alicePk, new SignatureTemplate(alice))
.withOpReturn([
'0x534c5000', // Lokad ID
'0x01', // Token type
'GENESIS', // Action
'CSS', // Symbol
'CashScriptSLP', // Name
'https://cashscript.org/', // Document URI
'', // Document hash
'0x08', // Decimals
'0x02', // Minting baton vout
'0x0000000000000001', // Initial quantity
])
.to(contract.address, 546)
.to(contract.address, 1000)
.send();
console.log('transaction details:', stringify(tx));
} catch (e) {
console.log(e);
}
}