增加 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_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。得到2634个字符大多数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 })
}
/**