forked from Bitcoin-com/slp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.js
57 lines (47 loc) · 1.6 KB
/
conversion.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
/*
Convert between address formats
*/
// Set NETWORK to either testnet or mainnet
const NETWORK = `mainnet`
const SLPSDK = require("../../lib/SLP")
// 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 conversion() {
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
const cashAddress = SLP.HDNode.toCashAddress(change)
const slpAddress = SLP.Address.toSLPAddress(cashAddress)
const legacyAddress = SLP.Address.toLegacyAddress(cashAddress)
console.log(`SLP Address: ${slpAddress}:`)
console.log(`Cash Address: ${cashAddress}:`)
console.log(`Legacy Address: ${legacyAddress}:`)
} catch (err) {
console.error(`Error in conversion: `, err)
console.log(`Error message: ${err.message}`)
throw err
}
}
conversion()