forked from Bitcoin-com/slp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-token.js
92 lines (78 loc) · 2.96 KB
/
create-token.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
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
84
85
86
87
88
89
90
91
92
/*
Create a new SLP token. Requires a wallet created with the create-wallet
example. Also requires that wallet to have a small BCH balance.
*/
// Set NETWORK to either testnet or mainnet
const NETWORK = `mainnet`
const SLPSDK = require("../../lib/SLP")
// Used for debugging and investigating JS objects.
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }
// Instantiate SLP based on the network.
let SLP
if (NETWORK === `mainnet`)
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })
// Open the wallet generated with create-wallet.
let walletInfo
try {
walletInfo = require(`../create-wallet/wallet.json`)
} catch (err) {
console.log(
`Could not open wallet.json. Generate a wallet with create-wallet first.`
)
process.exit(0)
}
async function createToken() {
try {
const mnemonic = walletInfo.mnemonic
// root seed buffer
const rootSeed = SLP.Mnemonic.toSeed(mnemonic)
// master HDNode
let masterHDNode
if (NETWORK === `mainnet`) masterHDNode = SLP.HDNode.fromSeed(rootSeed)
else masterHDNode = SLP.HDNode.fromSeed(rootSeed, "testnet") // Testnet
// HDNode of BIP44 account
const account = SLP.HDNode.derivePath(masterHDNode, "m/44'/145'/0'")
const change = SLP.HDNode.derivePath(account, "0/0")
// get the cash address and get some tBCH form our faucet https://developer.bitcoin.com/faucets/bch/
const cashAddress = SLP.HDNode.toCashAddress(change)
const slpAddress = SLP.Address.toSLPAddress(cashAddress)
const fundingAddress = slpAddress
const fundingWif = SLP.HDNode.toWIF(change) // <-- compressed WIF format
const tokenReceiverAddress = slpAddress
const batonReceiverAddress = slpAddress
const bchChangeReceiverAddress = cashAddress
// Create a config object defining the token to be created.
const createConfig = {
fundingAddress,
fundingWif,
tokenReceiverAddress,
batonReceiverAddress,
bchChangeReceiverAddress,
decimals: 8,
name: "SLP SDK example using BITBOX",
symbol: "SLPSDK",
documentUri: "developer.bitcoin.com",
documentHash: null,
initialTokenQty: 507
}
// Generate, sign, and broadcast a hex-encoded transaction for creating
// the new token.
const genesisTxId = await SLP.TokenType1.create(createConfig)
console.log(`genesisTxID: ${util.inspect(genesisTxId)}`)
console.log(
`The genesis TxID above is used to uniquely identify your new class of SLP token. Save it and keep it handy.`
)
console.log(` `)
console.log(`View this transaction on the block explorer:`)
if (NETWORK === `mainnet`)
console.log(`https://explorer.bitcoin.com/bch/tx/${genesisTxId}`)
else console.log(`https://explorer.bitcoin.com/tbch/tx/${genesisTxId}`)
} catch (err) {
console.error(`Error in createToken: `, err)
console.log(`Error message: ${err.message}`)
throw err
}
}
createToken()