增加 coinFamily 参数

This commit is contained in:
陆柯 2022-10-28 12:09:31 +08:00
parent eed025c4e1
commit c9b147aeaf

43
ticc.js
View File

@ -30,7 +30,8 @@ my.OUTPUT_LIST = ['hex', 'latin1', 'base64'] // or 'buf' to Buffer explicitly
my.INPUT = 'utf8' // 默认的加密方法的明文格式。utf8 能够兼容 latin1, ascii 的情形 my.INPUT = 'utf8' // 默认的加密方法的明文格式。utf8 能够兼容 latin1, ascii 的情形
my.INPUT_LIST = ['utf8', 'ascii', 'latin1'] // ignored for Buffer/TypedArray/DataView my.INPUT_LIST = ['utf8', 'ascii', 'latin1'] // ignored for Buffer/TypedArray/DataView
my.COIN = 'TIC' // 默认的币种 my.COIN = 'TIC' // 默认的币种
my.COIN_LIST = ['TIC', 'BTC', 'ETH'] my.COIN_FAMILY = 'TIC'
my.COIN_FAMILY_LIST = ['TIC', 'BTC', 'ETH']
my.WORLD = 'COMET' my.WORLD = 'COMET'
my.REGEXP_ALPHABET = { my.REGEXP_ALPHABET = {
hex: /^[0-9a-fA-F]+$/, hex: /^[0-9a-fA-F]+$/,
@ -554,23 +555,22 @@ class TicCrypto {
* @memberof TicCrypto * @memberof TicCrypto
* 只要提供了 path pathRoot就创建 bip39 账户如果都不存在那就创建主账户 * 只要提供了 path pathRoot就创建 bip39 账户如果都不存在那就创建主账户
*/ */
static secword_to_account ({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) { static secword_to_account ({ secword, coin, coinFamily, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) {
// account 比 keypair 多了 address 字段。 // account 比 keypair 多了 address 字段。
coin = coin?.toUpperCase?.() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, pathIndex, path, tool, hasher }) let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, pathIndex, path, tool, hasher })
if (kp) { if (kp) {
if (coin === 'ETH') { if (coin === 'ETH') {
world = world || 'mainnet' world = world || 'mainnet'
let uncompressedPubkey = this.decompress_pubkey(kp.pubkey) kp.address = this.pubkey_to_address({ pubkey: this.decompress_pubkey(kp.pubkey), coin, coinFamily, world })
kp.address = this.pubkey_to_address({ pubkey: uncompressedPubkey, coin: 'ETH', world })
} else if (coin === 'BTC') { } else if (coin === 'BTC') {
world = world || 'mainnet' world = world || 'mainnet'
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, world }) kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world })
} else { } else {
world = world || my.WORLD world = world || my.WORLD
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, world }) kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world })
} }
return Object.assign(kp, { coin, world, secword }) return { ...kp, coin, world, secword }
} }
return null return null
} }
@ -584,9 +584,8 @@ class TicCrypto {
* @return {String} address * @return {String} address
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static secword_to_address ({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) { static secword_to_address (options = {}) {
const account = this.secword_to_account({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher }) return this.secword_to_account(options)?.address
return account?.address
} }
/** /**
@ -630,17 +629,17 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static prikey_to_address ({ prikey, coin, world } = {}) { static prikey_to_address ({ prikey, coin, coinFamily, world } = {}) {
coin = coin?.toUpperCase?.() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
if (this.is_prikey({ prikey })) { if (this.is_prikey({ prikey })) {
/** @type {*} */ /** @type {*} */
let pubkey let pubkey
if (coin === 'ETH') { if (coin === 'ETH') {
pubkey = this.prikey_to_pubkey({ prikey, compress: false }) pubkey = this.prikey_to_pubkey({ prikey, compress: false })
return this.pubkey_to_address({ pubkey: pubkey, coin, world }) return this.pubkey_to_address({ pubkey, coin, coinFamily, world })
} else { } else {
pubkey = this.prikey_to_pubkey({ prikey, compress: true }) pubkey = this.prikey_to_pubkey({ prikey, compress: true })
return this.pubkey_to_address({ pubkey: pubkey, coin, world }) return this.pubkey_to_address({ pubkey, coin, coinFamily, world })
} }
} }
return null return null
@ -656,11 +655,11 @@ class TicCrypto {
* @memberof TicCrypto * @memberof TicCrypto
* position 就是通常所说的 PubKeyHash出现在比特币交易的锁定脚本里 * position 就是通常所说的 PubKeyHash出现在比特币交易的锁定脚本里
*/ */
static pubkey_to_position ({ pubkey, coin } = {}) { static pubkey_to_position ({ pubkey, coin, coinFamily = my.COIN_FAMILY } = {}) {
// tic, btc, eth 的 position 都是 20节=40字符的。 // tic, btc, eth 的 position 都是 20节=40字符的。
coin = coin?.toUpperCase?.() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
if (this.is_pubkey({ pubkey })) { if (this.is_pubkey({ pubkey })) {
if (coin === 'ETH') { if (coin === 'ETH' || coinFamily === 'ETH') {
// 注意必须要用非压缩的64字节的公钥的buffer并去掉开头的 04。 // 注意必须要用非压缩的64字节的公钥的buffer并去掉开头的 04。
if (pubkey.length === 66) { if (pubkey.length === 66) {
pubkey = this.decompress_pubkey(pubkey) pubkey = this.decompress_pubkey(pubkey)
@ -693,11 +692,11 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static position_to_address ({ position, coin, world } = {}) { static position_to_address ({ position, coin, world, coinFamily } = {}) {
if (!/^[\da-fA-F]{40}$/.test(position)) return null // 不论 tic, btc, eth其 position 都是 40字符的。 if (!/^[\da-fA-F]{40}$/.test(position)) return null // 不论 tic, btc, eth其 position 都是 40字符的。
coin = coin?.toUpperCase?.() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
let address let address
if (coin === 'ETH') { if (coin === 'ETH' || coinFamily === 'ETH') {
// 对以太坊,按照 EIP55把纯位置转换为大小写敏感能自我验证的hex地址。仍然为20节=40符。 // 对以太坊,按照 EIP55把纯位置转换为大小写敏感能自我验证的hex地址。仍然为20节=40符。
position = position.toLowerCase().replace('0x', '') position = position.toLowerCase().replace('0x', '')
let hash = keccak('keccak256') let hash = keccak('keccak256')
@ -712,7 +711,7 @@ class TicCrypto {
} }
} }
return address return address
} else if (coin === 'BTC') { } else if (coin === 'BTC' || coinFamily === 'BTC') {
// 对比特币把纯位置转换为大小写敏感能自我验证的bs58check地址先加前缀1节再加校验4节共25字节再转base58。得到2634个字符大多数34个。 // 对比特币把纯位置转换为大小写敏感能自我验证的bs58check地址先加前缀1节再加校验4节共25字节再转base58。得到2634个字符大多数34个。
// Base58: https://en.bitcoin.it/wiki/Base58Check_encoding // Base58: https://en.bitcoin.it/wiki/Base58Check_encoding
// https://en.bitcoin.it/wiki/List_of_address_prefixes // https://en.bitcoin.it/wiki/List_of_address_prefixes
@ -834,10 +833,10 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static pubkey_to_address ({ pubkey, coin, world } = {}) { static pubkey_to_address ({ pubkey, coin, world, coinFamily = my.COIN_FAMILY } = {}) {
// pubkey 应当是string类型 // pubkey 应当是string类型
coin = coin?.toUpperCase?.() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
return this.position_to_address({ position: this.pubkey_to_position({ pubkey, coin }), coin, world }) return this.position_to_address({ position: this.pubkey_to_position({ pubkey, coin, coinFamily }), coin, world, coinFamily })
} }
/** /**
@ -935,9 +934,9 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static randomize_account ({ lang, wordCount, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) { static randomize_account ({ lang, wordCount, coin, coinFamily, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) {
let secword = this.randomize_secword({ lang, wordCount }) let secword = this.randomize_secword({ lang, wordCount })
return this.secword_to_account({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher }) return this.secword_to_account({ secword, coin, coinFamily, world, pass, pathSeed, pathIndex, path, tool, hasher })
} }
/** /**