diff --git a/package.json b/package.json index ac479cd..bd14d3e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "js-crypto-key-utils": "^1.0.4", "keccak": "^3.0.2", "secp256k1": "^4.0.3", + "tronweb": "^6.0.3", "tweetnacl": "^1.0.3" }, "devDependencies": { diff --git a/ticc.js b/ticc.js index 6844833..3c334b6 100644 --- a/ticc.js +++ b/ticc.js @@ -16,6 +16,7 @@ const hdkey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或 const secp256k1 = require('secp256k1') const base32encode = require('base32-encode') const base32decode = require('base32-decode') +const { TronWeb } = require('tronweb') // 全部以hex为默认输入输出格式,方便人的阅读,以及方便函数之间统一接口 @@ -532,6 +533,8 @@ class TicCrypto { return `m/44'/0'/${path}` } else if (coin === 'ETH') { return `m/44'/60'/${path}` + } else if (coin === 'TRX') { + return `m/44'/195'/${path}` } else if (coin === 'TIC') { return `m/44'/60000'/${path}` } else if (coin === 'MATIC' || coin === 'POL') { @@ -578,6 +581,14 @@ class TicCrypto { } else if (coin === 'BTC' || coinFamily === 'BTC') { world = world || 'mainnet' kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world }) + } else if (coin === 'TRX' || coinFamily === 'TRX') { + const prikey = kp.prikey.replace(/^0x/, '') // tronweb 需要去掉开头的 0x + const tronweb = new TronWeb({ + fullHost: 'https://api.trongrid.io', + privateKey: prikey, + }) + world = world || 'tron' + kp.address = tronweb.address.fromPrivateKey(prikey) // 返回的是带 41 开头的地址 } else { world = world || my.WORLD kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world }) @@ -1613,26 +1624,26 @@ class TicCrypto { static convert_pexid (key) { key = key.toLowerCase() - let pextokenCid, pextokenCosh, nftToid, tokenURI + let pextokenCid, pextokenCahex, nftToid, tokenURI try { if (key.length < 64 && /^bafkrei/.test(key)) { pextokenCid = key - pextokenCosh = this.cid_to_cahex({ cid: pextokenCid }) - nftToid = BigInt('0x' + pextokenCosh).toString() + pextokenCahex = this.cid_to_cahex({ cid: pextokenCid }) + nftToid = BigInt('0x' + pextokenCahex).toString() } else if (key.length > 64 && /^\d+$/.test(key)) { nftToid = key - pextokenCosh = BigInt(nftToid).toString(16) - pextokenCid = this.cahex_to_cid({ cahex: pextokenCosh }) + pextokenCahex = BigInt(nftToid).toString(16) + pextokenCid = this.cahex_to_cid({ cahex: pextokenCahex }) } else if (/^[0-9a-f]{64}$/.test(key)) { - pextokenCosh = key - pextokenCid = this.cahex_to_cid({ cahex: pextokenCosh }) - nftToid = BigInt('0x' + pextokenCosh).toString() + pextokenCahex = key + pextokenCid = this.cahex_to_cid({ cahex: pextokenCahex }) + nftToid = BigInt('0x' + pextokenCahex).toString() } - tokenURI = pextokenCosh ? `https://ipfs.tic.cc/ipfs/f01551220${pextokenCosh}` : undefined + tokenURI = pextokenCahex ? `https://ipfs.tic.cc/ipfs/f01551220${pextokenCahex}` : undefined } catch {} return { pextokenCid, - pextokenCosh, + pextokenCahex, nftToid, tokenURI, }