From c9b147aeafc24afac2cc077caa15674121083997 Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Fri, 28 Oct 2022 12:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20coinFamily=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ticc.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/ticc.js b/ticc.js index 1511d66..6e4fb1b 100644 --- a/ticc.js +++ b/ticc.js @@ -30,7 +30,8 @@ my.OUTPUT_LIST = ['hex', 'latin1', 'base64'] // or 'buf' to Buffer explicitly my.INPUT = 'utf8' // 默认的加密方法的明文格式。utf8 能够兼容 latin1, ascii 的情形 my.INPUT_LIST = ['utf8', 'ascii', 'latin1'] // ignored for Buffer/TypedArray/DataView my.COIN = 'TIC' // 默认的币种 -my.COIN_LIST = ['TIC', 'BTC', 'ETH'] +my.COIN_FAMILY = 'TIC' +my.COIN_FAMILY_LIST = ['TIC', 'BTC', 'ETH'] my.WORLD = 'COMET' my.REGEXP_ALPHABET = { hex: /^[0-9a-fA-F]+$/, @@ -554,23 +555,22 @@ class TicCrypto { * @memberof TicCrypto * 只要提供了 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 字段。 coin = coin?.toUpperCase?.() || my.COIN let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, pathIndex, path, tool, hasher }) if (kp) { if (coin === 'ETH') { world = world || 'mainnet' - let uncompressedPubkey = this.decompress_pubkey(kp.pubkey) - kp.address = this.pubkey_to_address({ pubkey: uncompressedPubkey, coin: 'ETH', world }) + kp.address = this.pubkey_to_address({ pubkey: this.decompress_pubkey(kp.pubkey), coin, coinFamily, world }) } else if (coin === 'BTC') { 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 { 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 } @@ -584,9 +584,8 @@ class TicCrypto { * @return {String} address * @memberof TicCrypto */ - static secword_to_address ({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) { - const account = this.secword_to_account({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher }) - return account?.address + static secword_to_address (options = {}) { + return this.secword_to_account(options)?.address } /** @@ -630,17 +629,17 @@ class TicCrypto { * @return {*} * @memberof TicCrypto */ - static prikey_to_address ({ prikey, coin, world } = {}) { + static prikey_to_address ({ prikey, coin, coinFamily, world } = {}) { coin = coin?.toUpperCase?.() || my.COIN if (this.is_prikey({ prikey })) { /** @type {*} */ let pubkey if (coin === 'ETH') { 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 { 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 @@ -656,11 +655,11 @@ class TicCrypto { * @memberof TicCrypto * position 就是通常所说的 PubKeyHash,出现在比特币交易的锁定脚本里 */ - static pubkey_to_position ({ pubkey, coin } = {}) { + static pubkey_to_position ({ pubkey, coin, coinFamily = my.COIN_FAMILY } = {}) { // tic, btc, eth 的 position 都是 20节=40字符的。 coin = coin?.toUpperCase?.() || my.COIN if (this.is_pubkey({ pubkey })) { - if (coin === 'ETH') { + if (coin === 'ETH' || coinFamily === 'ETH') { // 注意,必须要用非压缩的64字节的公钥的buffer,并去掉开头的 04。 if (pubkey.length === 66) { pubkey = this.decompress_pubkey(pubkey) @@ -693,11 +692,11 @@ class TicCrypto { * @return {*} * @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字符的。 coin = coin?.toUpperCase?.() || my.COIN let address - if (coin === 'ETH') { + if (coin === 'ETH' || coinFamily === 'ETH') { // 对以太坊,按照 EIP55,把纯位置转换为大小写敏感能自我验证的hex地址。仍然为20节=40符。 position = position.toLowerCase().replace('0x', '') let hash = keccak('keccak256') @@ -712,7 +711,7 @@ class TicCrypto { } } return address - } else if (coin === 'BTC') { + } else if (coin === 'BTC' || coinFamily === 'BTC') { // 对比特币,把纯位置转换为大小写敏感能自我验证的bs58check地址:先加前缀1节,再加校验4节,共25字节,再转base58。得到26~34个字符,大多数34个。 // Base58: https://en.bitcoin.it/wiki/Base58Check_encoding // https://en.bitcoin.it/wiki/List_of_address_prefixes @@ -834,10 +833,10 @@ class TicCrypto { * @return {*} * @memberof TicCrypto */ - static pubkey_to_address ({ pubkey, coin, world } = {}) { + static pubkey_to_address ({ pubkey, coin, world, coinFamily = my.COIN_FAMILY } = {}) { // pubkey 应当是string类型 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 {*} * @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 }) - 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 }) } /**