Skip to content

Commit

Permalink
fix cfx (#159)
Browse files Browse the repository at this point in the history
Co-authored-by: soralit <soralitria@gmail.com>
  • Loading branch information
soralit and soralit authored Feb 26, 2021
1 parent 24325fb commit 38426e1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 13 additions & 2 deletions src/TCFX/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
};
Expand Down
1 change: 1 addition & 0 deletions src/TCFX/lib/util/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function hasNetworkPrefix(address) {

module.exports = {
encodeCfxAddress,
decodeCfxAddress,
isValidCfxAddress,
hasNetworkPrefix,
};
7 changes: 7 additions & 0 deletions src/__tests__/coins/TCFX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ describe('coin.CFX', () => {
'cfx:aaawgvnhveawgvnhveawgvnhveawgvnhve86d4d90g',
);
});

it('should decode address', function() {
const b1 = 'cfx:aaawgvnhveawgvnhveawgvnhveawgvnhve86d4d90g';
expect(cfx.convertAddressToHex(b1)).toBe(
'0x0123456789012345678901234567890123456789',
);
});
});

0 comments on commit 38426e1

Please sign in to comment.