UNPKG

lotus-sdk

Version:

Central repository for several classes of tools for integrating with, and building for, the Lotusia ecosystem

151 lines (150 loc) 4.09 kB
function prefixToArray(prefix) { const result = []; for (let i = 0; i < prefix.length; i++) { result.push(prefix.charCodeAt(i) & 31); } return result; } export class Network { name; alias; pubkeyhash; privatekey; scripthash; xpubkey; xprivkey; networkMagic; port; dnsSeeds; prefix; prefixArray; networkbyte; constructor(config) { this.name = config.name; this.alias = config.alias; this.pubkeyhash = config.pubkeyhash; this.privatekey = config.privatekey; this.scripthash = config.scripthash; this.xpubkey = config.xpubkey; this.xprivkey = config.xprivkey; this.networkMagic = config.networkMagic; this.port = config.port; this.dnsSeeds = config.dnsSeeds; this.prefix = config.prefix; this.prefixArray = config.prefixArray; this.networkbyte = config.networkbyte; } toString() { return this.name; } } export const livenet = new Network({ name: 'livenet', alias: 'mainnet', prefix: 'bitcoincash', networkbyte: '_', pubkeyhash: 0, privatekey: 0x80, scripthash: 5, xpubkey: 0x0488b21e, xprivkey: 0x0488ade4, networkMagic: Buffer.from('ece7eff3', 'hex'), port: 10605, dnsSeeds: ['seed.lotusia.org'], prefixArray: prefixToArray('bitcoincash'), }); export const testnet = new Network({ name: 'testnet', alias: 'test', prefix: 'bchtest', networkbyte: 'T', pubkeyhash: 0x6f, privatekey: 0xef, scripthash: 0xc4, xpubkey: 0x043587cf, xprivkey: 0x04358394, networkMagic: Buffer.from('ecf4f3f4', 'hex'), port: 11605, dnsSeeds: ['seed.lotusia.org'], prefixArray: prefixToArray('bchtest'), }); export const regtest = new Network({ name: 'regtest', alias: 'reg', prefix: 'bchreg', networkbyte: 'R', pubkeyhash: 0x6f, privatekey: 0xef, scripthash: 0xc4, xpubkey: 0x043587cf, xprivkey: 0x04358394, networkMagic: Buffer.from('ecf2e5e7', 'hex'), port: 12605, dnsSeeds: [], prefixArray: prefixToArray('bchreg'), }); export const networks = [livenet, testnet, regtest]; export const networkMaps = {}; networks.forEach(network => { networkMaps[network.name] = network; networkMaps[network.alias] = network; networkMaps[network.pubkeyhash] = network; networkMaps[network.privatekey] = network; networkMaps[network.scripthash] = network; networkMaps[network.xpubkey] = network; networkMaps[network.xprivkey] = network; }); export const defaultNetwork = livenet; export function get(arg, keys) { if (networks.includes(arg)) { return arg; } if (keys) { const keyArray = Array.isArray(keys) ? keys : [keys]; for (const network of networks) { const filteredNet = keyArray.reduce((acc, key) => { acc[key] = network[key]; return acc; }, {}); const netValues = Object.values(filteredNet); if (netValues.includes(arg)) { return network; } } return undefined; } return networkMaps[arg]; } export function add(data) { const network = new Network(data); networks.push(network); networkMaps[network.name] = network; networkMaps[network.alias] = network; networkMaps[network.pubkeyhash] = network; networkMaps[network.privatekey] = network; networkMaps[network.scripthash] = network; networkMaps[network.xpubkey] = network; networkMaps[network.xprivkey] = network; return network; } export function remove(network) { const index = networks.indexOf(network); if (index !== -1) { networks.splice(index, 1); Object.keys(networkMaps).forEach(key => { if (networkMaps[key] === network) { delete networkMaps[key]; } }); } } export const Networks = { add, remove, get, defaultNetwork, livenet, mainnet: livenet, testnet, regtest, };