Skip to content

Commit 85323fd

Browse files
committedJun 21, 2022
add eth sign test
1 parent 10d6650 commit 85323fd

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed
 

‎package.json

+13-10
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@testing-library/jest-dom": "^5.16.4",
7-
"@testing-library/react": "^13.2.0",
8-
"@testing-library/user-event": "^13.5.0",
9-
"@trustwallet/wallet-core": "^2.9.0",
10-
"@types/jest": "^27.5.0",
11-
"@types/node": "^16.11.34",
12-
"@types/react": "^18.0.9",
13-
"@types/react-dom": "^18.0.3",
6+
"buffer": "^6.0.3",
7+
"node-polyfill-webpack-plugin": "^1.1.4",
148
"fs": "npm:empty-npm-package@1.0.0",
159
"fs-extra": "^10.1.0",
16-
"node-polyfill-webpack-plugin": "^1.1.4",
1710
"react": "^18.1.0",
1811
"react-app-rewired": "^2.2.1",
1912
"react-dom": "^18.1.0",
2013
"react-scripts": "5.0.1",
2114
"typescript": "^4.6.4",
2215
"web-vitals": "^2.1.4"
2316
},
17+
"devDependencies": {
18+
"@testing-library/jest-dom": "^5.16.4",
19+
"@testing-library/react": "^13.2.0",
20+
"@testing-library/user-event": "^13.5.0",
21+
"@trustwallet/wallet-core": "^2.9.0",
22+
"@types/jest": "^27.5.0",
23+
"@types/node": "^16.11.34",
24+
"@types/react": "^18.0.9",
25+
"@types/react-dom": "^18.0.3"
26+
},
2427
"scripts": {
2528
"start": "react-app-rewired start",
2629
"build": "react-app-rewired build",
27-
"postinstall": "cp node_modules/@trustwallet/wallet-core/dist/lib/wallet-core.* public/static/js"
30+
"postinstall": "mkdir -pv public/static/js && cp node_modules/@trustwallet/wallet-core/dist/lib/wallet-core.* public/static/js"
2831
},
2932
"eslintConfig": {
3033
"extends": [

‎scripts/test.js

-12
This file was deleted.

‎src/App.tsx

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
11
import { useState } from 'react';
2+
import { Buffer } from 'buffer';
23

34
function App() {
45
const [mnemonic, setMnemonic] = useState('')
6+
const [rawTx, setRawTx] = useState('')
57

68
const handleCreateWallet = () => {
7-
const WalletCore = (window as any).Module;
8-
const wallet = new WalletCore.HDWallet.create(128, "");
9-
10-
console.log(wallet.mnemonic());
9+
const { HDWallet } = (window as any).Module;
10+
const wallet = new HDWallet.create(128, "");
11+
1112
setMnemonic(wallet.mnemonic());
1213

1314
wallet.delete();
1415
}
1516

17+
const testSignEthereumTx = () => {
18+
const TW = (window as any).TW;
19+
const { HexCoding, AnySigner, CoinType } = (window as any).Module;
20+
21+
const input = TW.Ethereum.Proto.SigningInput.create({
22+
toAddress: "0x6b175474e89094c44da98b954eedeac495271d0f",
23+
chainId: Buffer.from("01", "hex"),
24+
nonce: Buffer.from("00", "hex"),
25+
txMode: TW.Ethereum.Proto.TransactionMode.Enveloped,
26+
maxInclusionFeePerGas: Buffer.from("0077359400", "hex"),
27+
maxFeePerGas: Buffer.from("00b2d05e00", "hex"),
28+
gasLimit: Buffer.from("0130B9", "hex"),
29+
transaction: TW.Ethereum.Proto.Transaction.create({
30+
erc20Transfer: TW.Ethereum.Proto.Transaction.ERC20Transfer.create({
31+
to: "0x5322b34c88ed0691971bf52a7047448f0f4efc84",
32+
amount: Buffer.from("1bc16d674ec80000", "hex"),
33+
}),
34+
}),
35+
privateKey: HexCoding.decode(
36+
"0x608dcb1742bb3fb7aec002074e3420e4fab7d00cced79ccdac53ed5b27138151"
37+
),
38+
});
39+
40+
const encoded = TW.Ethereum.Proto.SigningInput.encode(input).finish();
41+
const outputData = AnySigner.sign(encoded, CoinType.ethereum);
42+
const output = TW.Ethereum.Proto.SigningOutput.decode(outputData);
43+
44+
setRawTx(HexCoding.encode(output.encoded));
45+
}
46+
1647
return (
1748
<div>
1849
<button onClick={handleCreateWallet}>Create Wallet</button>
1950
<div>Mnemonic: {mnemonic}</div>
51+
<button onClick={testSignEthereumTx}>Signing Ethereum tx</button>
52+
<div>Raw tx: {rawTx}</div>
2053
</div>
2154
);
2255
}

‎src/initWasm.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ export const initWasm = () => {
55
preRun: [],
66
postRun: [],
77
onRuntimeInitialized: () => {
8-
console.info('wallet-core is ready....');
8+
console.info('wallet-core wasm loaded');
99
resolve();
10-
},
11-
totalDependencies: 0,
12-
monitorRunDependencies: (t: string) => console.log('monitorRunDependencies: ', t)
10+
}
1311
};
1412

1513
(window as any).Module = init;
1614
const { TW } = require('@trustwallet/wallet-core');
17-
console.log(TW.Ethereum.Proto);
15+
(window as any).TW = TW;
1816
})
1917
}

0 commit comments

Comments
 (0)
Please sign in to comment.