Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cfx #159

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
);
});
});