diff --git a/package.json b/package.json index 907c5b7..15562b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto-coin-kit", - "version": "0.3.2", + "version": "0.3.3", "description": "crypto coin kit for common coins", "main": "./dist/index.js", "scripts": { diff --git a/src/TCFX/index.ts b/src/TCFX/index.ts index f866fcd..bc744ab 100644 --- a/src/TCFX/index.ts +++ b/src/TCFX/index.ts @@ -6,6 +6,7 @@ import {Result, SignProviderSync, SignProvider} from '../Common/sign'; import {Coin} from '../Common/coin'; // @ts-ignore import abi from 'human-standard-token-abi'; +import {decodeCfxAddress} from './lib/util/address'; const {publicKeyToAddress, checksumAddress} = util.sign; const {format} = util; @@ -31,12 +32,21 @@ export class TCFX implements Coin { public generateAddress = (publicKey: string) => { const address = publicKeyToAddress(format.buffer(publicKey)); - return '0x'+Buffer.from(address).toString('hex'); + return '0x' + Buffer.from(address).toString('hex'); }; public convertAddress = (address: string, networkId = 1029) => { return format.address(address, networkId); - } + }; + + public convertAddressToHex = (address: string) => { + if (address.startsWith('cfx')) { + // @ts-ignore + const {hexAddress} = decodeCfxAddress(address); + return '0x' + Buffer.from(hexAddress).toString('hex'); + } + return address; + }; public isAddressValid = (address: string) => { try { @@ -101,6 +111,7 @@ export class TCFX implements Coin { }; protected constructTransaction = (data: TxData) => { + data.to = this.convertAddressToHex(data.to); // @ts-ignore return new Transaction(this.formatTxData(data)); }; diff --git a/src/TCFX/lib/util/address.js b/src/TCFX/lib/util/address.js index 83a401b..362316a 100644 --- a/src/TCFX/lib/util/address.js +++ b/src/TCFX/lib/util/address.js @@ -73,6 +73,7 @@ function hasNetworkPrefix(address) { module.exports = { encodeCfxAddress, + decodeCfxAddress, isValidCfxAddress, hasNetworkPrefix, }; diff --git a/src/__tests__/coins/TCFX.test.ts b/src/__tests__/coins/TCFX.test.ts index 36e0a8c..64a772f 100644 --- a/src/__tests__/coins/TCFX.test.ts +++ b/src/__tests__/coins/TCFX.test.ts @@ -160,4 +160,11 @@ describe('coin.CFX', () => { 'cfx:aaawgvnhveawgvnhveawgvnhveawgvnhve86d4d90g', ); }); + + it('should decode address', function() { + const b1 = 'cfx:aaawgvnhveawgvnhveawgvnhveawgvnhve86d4d90g'; + expect(cfx.convertAddressToHex(b1)).toBe( + '0x0123456789012345678901234567890123456789', + ); + }); });