Skip to content

Commit e40bbd3

Browse files
hankliu418Hank Liu
andauthored
update to fit core v2.0 (#971)
Co-authored-by: Hank Liu <[email protected]>
1 parent d22bc80 commit e40bbd3

File tree

7 files changed

+67
-140
lines changed

7 files changed

+67
-140
lines changed

packages/coin-icx/package-lock.json

Lines changed: 32 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/coin-icx/package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
22
"name": "@coolwallet/icx",
3-
"version": "1.1.3",
3+
"version": "2.0.0-beta.0",
44
"description": "CoolWallet Icon sdk",
55
"main": "lib/index.js",
66
"scripts": {
7+
"test": "NODE_ENV=development jest --runInBand",
8+
"test-lite": "NODE_ENV=development CARD=lite jest --runInBand",
9+
"ci-test": "NODE_ENV=production jest --runInBand",
10+
"ci-test-lite": "NODE_ENV=production CARD=lite jest --runInBand",
11+
"ci-test-report": "NODE_ENV=production jest --no-bail --runInBand --reporters=default --reporters=jest-junit --testResultsProcessor=jest-junit",
712
"build:types": "tsc --emitDeclarationOnly",
813
"build:ts": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
914
"build": "npm run build:types && npm run build:ts",
10-
"publish-single": "rm -rf node_modules && npm install && npm run build && npm publish",
11-
"test": "NODE_ENV=development jest --runInBand",
12-
"ci-test": "NODE_ENV=production jest --runInBand",
13-
"ci-test-report": "NODE_ENV=production jest --no-bail --runInBand --reporters=default --reporters=jest-junit --testResultsProcessor=jest-junit"
15+
"publish-single": "rm -rf node_modules && npm install && npm run build && npm publish"
1416
},
1517
"keywords": [
1618
"icx",
@@ -24,10 +26,8 @@
2426
],
2527
"author": "coolwallet-team",
2628
"license": "ISC",
27-
"peerDependencies": {
28-
"@coolwallet/core": "^1.1.31"
29-
},
3029
"dependencies": {
30+
"@coolwallet/core": "^2.0.0-beta.20",
3131
"elliptic": "^6.5.1",
3232
"icon-sdk-js": "0.0.18",
3333
"js-sha3": "^0.8.0"
@@ -37,8 +37,7 @@
3737
"@babel/core": "^7.7.0",
3838
"@babel/plugin-proposal-class-properties": "^7.7.4",
3939
"@babel/preset-env": "^7.7.1",
40-
"@babel/preset-typescript": "^7.7.7",
41-
"@coolwallet/core": "^1.1.31"
40+
"@babel/preset-typescript": "^7.7.7"
4241
},
4342
"repository": {
4443
"type": "git",

packages/coin-icx/src/config/params.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export const TRANSFER = {
66
'304502204b1911ba2d52c9fb0c87552fc47b31f25e9ae13ab4887d14d467bd3eb84baa26022100dc36343d426045e3f5f0d1223e4ed0d0eab3e6ab33a5d7bbb9de09870f478604'.padStart(
77
144,
88
'0'
9-
)
9+
),
1010
};
1111

12-
13-
export const COIN_TYPE = '8000004A'
12+
export const COIN_TYPE = '8000004A';

packages/coin-icx/src/sign.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { apdu, tx } from '@coolwallet/core';
1+
import { tx } from '@coolwallet/core';
22
import * as txUtil from './utils/transactionUtil';
33
import * as scriptUtil from './utils/scriptUtil';
44
import * as types from './config/types';
5+
import { SignatureType } from '@coolwallet/core/lib/transaction/type';
56

67
/**
78
* Sign ICON Transaction
@@ -17,22 +18,21 @@ export default async function signTransaction(
1718
const { script, argument } = await scriptUtil.getScriptAndArguments(addressIndex, transaction);
1819

1920
const sendScript = async () => {
20-
await apdu.tx.sendScript(transport, script);
21+
await tx.command.sendScript(transport, script);
2122
};
2223
preActions.push(sendScript);
2324

2425
const sendArgument = async () => {
25-
return await apdu.tx.executeScript(transport, appId, appPrivateKey, argument);
26+
return tx.command.executeScript(transport, appId, appPrivateKey, argument);
2627
};
2728

28-
const canonicalSignature = await tx.flow.getSingleSignatureFromCoolWallet(
29+
const canonicalSignature = await tx.flow.getSingleSignatureFromCoolWalletV2(
2930
transport,
3031
preActions,
3132
sendArgument,
32-
false,
33+
SignatureType.Canonical,
3334
confirmCB,
34-
authorizedCB,
35-
true
35+
authorizedCB
3636
);
3737
const txObject = await txUtil.generateRawTx(canonicalSignature, transaction, publicKey);
3838
return txObject;
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { utils } from '@coolwallet/core';
2-
import * as params from "../config/params";
3-
import * as stringUtil from "./stringUtil";
2+
import * as params from '../config/params';
3+
import * as stringUtil from './stringUtil';
44

5+
const getTransferArgument = (transaction: any) => {
6+
const tx = JSON.parse(transaction);
7+
const argument =
8+
stringUtil.handleHex(tx.from).slice(2) +
9+
stringUtil.handleHex(tx.to).slice(2) +
10+
stringUtil.handleHex(tx.value).padStart(20, '0') +
11+
stringUtil.handleHex(tx.timestamp).padStart(20, '0') +
12+
stringUtil.handleHex(tx.nid.toString(16)).padStart(4, '0');
13+
return argument;
14+
};
515

616
/**
717
* @param {number} addressIndex
@@ -11,24 +21,11 @@ export const getScriptAndArguments = async (addressIndex: number, transaction: o
1121
// const addressIdxHex = "00".concat(addressIndex.toString(16).padStart(6, "0"));
1222
// const SEPath = `15328000002C${params.COIN_TYPE}8000000000000000${addressIdxHex}`;
1323
const SEPath = `15${await utils.getPath(params.COIN_TYPE, addressIndex)}`;
14-
let script;
15-
let argument;
16-
script = params.TRANSFER.script + params.TRANSFER.signature;
17-
argument = getTransferArgument(transaction);
24+
const script = params.TRANSFER.script + params.TRANSFER.signature;
25+
const argument = getTransferArgument(transaction);
1826

1927
return {
2028
script,
2129
argument: SEPath + argument,
2230
};
2331
};
24-
25-
const getTransferArgument = (transaction: any) => {
26-
const tx = JSON.parse(transaction);
27-
const argument =
28-
stringUtil.handleHex(tx.from).slice(2) +
29-
stringUtil.handleHex(tx.to).slice(2) +
30-
stringUtil.handleHex(tx.value).padStart(20, "0") +
31-
stringUtil.handleHex(tx.timestamp).padStart(20, "0") +
32-
stringUtil.handleHex(tx.nid.toString(16)).padStart(4, "0");
33-
return argument;
34-
};

packages/coin-icx/src/utils/stringUtil.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
export const handleHex = (hex: string) => {
42
const prefixRemoved = hex.slice(0, 2) === '0x' ? hex.slice(2) : hex;
53
return prefixRemoved.length % 2 !== 0 ? `0${prefixRemoved}` : prefixRemoved;

packages/coin-icx/src/utils/transactionUtil.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import * as stringUtil from "./stringUtil";
1+
import * as stringUtil from './stringUtil';
22
const { sha3_256 } = require('js-sha3');
33
const elliptic = require('elliptic');
44
const ec = new elliptic.ec('secp256k1');
55

6-
76
export const pubKeyToAddress = (compressedPubkey: string): string => {
87
const keyPair = ec.keyFromPublic(compressedPubkey, 'hex');
98
let publicKey = keyPair.getPublic(false, 'hex').substr(2);
@@ -13,7 +12,6 @@ export const pubKeyToAddress = (compressedPubkey: string): string => {
1312
return address;
1413
};
1514

16-
1715
export const generateRawTx = async (
1816
canonicalSignature: any,
1917
payload: string | object,
@@ -45,12 +43,10 @@ export function generateHashKey(obj: any): string {
4543
return result;
4644
}
4745

48-
4946
function objTraverse(obj: any) {
5047
let result = '';
5148
result += '{';
52-
let keys;
53-
keys = Object.keys(obj);
49+
const keys = Object.keys(obj);
5450
keys.sort();
5551
for (let i = 0; i < keys.length; i++) {
5652
const key = keys[i];
@@ -129,11 +125,7 @@ function escapeString(value: string) {
129125
return newString;
130126
}
131127

132-
const generateFullCanonicalSig = (
133-
canonicalSignature: any,
134-
phraseToSign: string,
135-
compressedPubkey: string
136-
) => {
128+
const generateFullCanonicalSig = (canonicalSignature: any, phraseToSign: string, compressedPubkey: string) => {
137129
const keyPair = ec.keyFromPublic(compressedPubkey, 'hex');
138130

139131
const hashcode = sha3_256.update(phraseToSign).hex();
@@ -155,4 +147,3 @@ const generateFullCanonicalSig = (
155147

156148
return r + s + v;
157149
};
158-

0 commit comments

Comments
 (0)