support TRX coin
This commit is contained in:
parent
8e3ae5639f
commit
22f93d0a7b
@ -15,6 +15,7 @@
|
|||||||
"js-crypto-key-utils": "^1.0.4",
|
"js-crypto-key-utils": "^1.0.4",
|
||||||
"keccak": "^3.0.2",
|
"keccak": "^3.0.2",
|
||||||
"secp256k1": "^4.0.3",
|
"secp256k1": "^4.0.3",
|
||||||
|
"tronweb": "^6.0.3",
|
||||||
"tweetnacl": "^1.0.3"
|
"tweetnacl": "^1.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
31
ticc.js
31
ticc.js
@ -16,6 +16,7 @@ const hdkey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或
|
|||||||
const secp256k1 = require('secp256k1')
|
const secp256k1 = require('secp256k1')
|
||||||
const base32encode = require('base32-encode')
|
const base32encode = require('base32-encode')
|
||||||
const base32decode = require('base32-decode')
|
const base32decode = require('base32-decode')
|
||||||
|
const { TronWeb } = require('tronweb')
|
||||||
|
|
||||||
// 全部以hex为默认输入输出格式,方便人的阅读,以及方便函数之间统一接口
|
// 全部以hex为默认输入输出格式,方便人的阅读,以及方便函数之间统一接口
|
||||||
|
|
||||||
@ -532,6 +533,8 @@ class TicCrypto {
|
|||||||
return `m/44'/0'/${path}`
|
return `m/44'/0'/${path}`
|
||||||
} else if (coin === 'ETH') {
|
} else if (coin === 'ETH') {
|
||||||
return `m/44'/60'/${path}`
|
return `m/44'/60'/${path}`
|
||||||
|
} else if (coin === 'TRX') {
|
||||||
|
return `m/44'/195'/${path}`
|
||||||
} else if (coin === 'TIC') {
|
} else if (coin === 'TIC') {
|
||||||
return `m/44'/60000'/${path}`
|
return `m/44'/60000'/${path}`
|
||||||
} else if (coin === 'MATIC' || coin === 'POL') {
|
} else if (coin === 'MATIC' || coin === 'POL') {
|
||||||
@ -578,6 +581,14 @@ class TicCrypto {
|
|||||||
} else if (coin === 'BTC' || coinFamily === 'BTC') {
|
} else if (coin === 'BTC' || coinFamily === 'BTC') {
|
||||||
world = world || 'mainnet'
|
world = world || 'mainnet'
|
||||||
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world })
|
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 {
|
} else {
|
||||||
world = world || my.WORLD
|
world = world || my.WORLD
|
||||||
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world })
|
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, coinFamily, world })
|
||||||
@ -1613,26 +1624,26 @@ class TicCrypto {
|
|||||||
|
|
||||||
static convert_pexid (key) {
|
static convert_pexid (key) {
|
||||||
key = key.toLowerCase()
|
key = key.toLowerCase()
|
||||||
let pextokenCid, pextokenCosh, nftToid, tokenURI
|
let pextokenCid, pextokenCahex, nftToid, tokenURI
|
||||||
try {
|
try {
|
||||||
if (key.length < 64 && /^bafkrei/.test(key)) {
|
if (key.length < 64 && /^bafkrei/.test(key)) {
|
||||||
pextokenCid = key
|
pextokenCid = key
|
||||||
pextokenCosh = this.cid_to_cahex({ cid: pextokenCid })
|
pextokenCahex = this.cid_to_cahex({ cid: pextokenCid })
|
||||||
nftToid = BigInt('0x' + pextokenCosh).toString()
|
nftToid = BigInt('0x' + pextokenCahex).toString()
|
||||||
} else if (key.length > 64 && /^\d+$/.test(key)) {
|
} else if (key.length > 64 && /^\d+$/.test(key)) {
|
||||||
nftToid = key
|
nftToid = key
|
||||||
pextokenCosh = BigInt(nftToid).toString(16)
|
pextokenCahex = BigInt(nftToid).toString(16)
|
||||||
pextokenCid = this.cahex_to_cid({ cahex: pextokenCosh })
|
pextokenCid = this.cahex_to_cid({ cahex: pextokenCahex })
|
||||||
} else if (/^[0-9a-f]{64}$/.test(key)) {
|
} else if (/^[0-9a-f]{64}$/.test(key)) {
|
||||||
pextokenCosh = key
|
pextokenCahex = key
|
||||||
pextokenCid = this.cahex_to_cid({ cahex: pextokenCosh })
|
pextokenCid = this.cahex_to_cid({ cahex: pextokenCahex })
|
||||||
nftToid = BigInt('0x' + pextokenCosh).toString()
|
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 {}
|
} catch {}
|
||||||
return {
|
return {
|
||||||
pextokenCid,
|
pextokenCid,
|
||||||
pextokenCosh,
|
pextokenCahex,
|
||||||
nftToid,
|
nftToid,
|
||||||
tokenURI,
|
tokenURI,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user