Skip to content

Commit 59527cc

Browse files
author
nguyen-zung
committed
update js for wasm and add more tests
1 parent 543bd5f commit 59527cc

File tree

4 files changed

+351
-34
lines changed

4 files changed

+351
-34
lines changed

wasmjs/dist/src/index.js

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export class WalletCoreWrapper {
3030
return this.AnySigner.sign(input, coin);
3131
}
3232
preImageHashes(coin, txInput) {
33-
console.log('core package', (coin), (txInput));
3433
const result = this.TransactionCompiler.preImageHashes(coin, txInput);
35-
console.log('RESULT', result);
34+
console.log('RESULT', result, result.length);
3635
return result;
3736
}
3837
compileWithSignatures(coinType, txInputData, signatures, publicKeys) {
@@ -47,7 +46,7 @@ export class WalletCoreWrapper {
4746
items.forEach((i) => vec.add(i));
4847
return vec;
4948
}
50-
createAptosInputFromJson(jsonStr) {
49+
buildAptosUnsignedTx(jsonStr) {
5150
const req = JSON.parse(jsonStr);
5251
const input = {
5352
chainId: req.chainId,
@@ -61,29 +60,14 @@ export class WalletCoreWrapper {
6160
amount: Long.fromString(String(req.amount)),
6261
}),
6362
};
64-
return this.TW.Aptos.Proto.SigningInput.encode(input);
63+
return this.TW.Aptos.Proto.SigningInput.encode(input).finish();
6564
}
66-
buildAptosUnsignedMessageRaw(txInput) {
67-
// console.log('======', this.CoinType.aptos.value, w.finish())
68-
// const txInput = this.marshalInput(w)
65+
buildAptosUnsignedMessage(txInput) {
6966
const preimage = this.preImageHashes(this.CoinType.aptos, txInput);
70-
console.log('preimage in buildAptosUnsignedMessage:', preimage);
7167
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
72-
console.log(out, this.CoinType.aptos.value, txInput);
73-
return out.dataHash;
74-
// return undefined
68+
return out.data;
7569
}
76-
buildAptosUnsignedMessage(coin, w) {
77-
// console.log('======', this.CoinType.aptos.value, w.finish())
78-
const txInput = this.marshalInput(w);
79-
const preimage = this.preImageHashes(coin, txInput);
80-
console.log('preimage in buildAptosUnsignedMessage:', preimage);
81-
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
82-
console.log(out, this.CoinType.aptos.value, txInput);
83-
return out.dataHash;
84-
// return undefined
85-
}
86-
createCardanoInputFromJson(jsonStr) {
70+
buildCardanoUnsignedTx(jsonStr) {
8771
const req = JSON.parse(jsonStr);
8872
const transfer = this.TW.Cardano.Proto.Transfer.create({
8973
toAddress: req.toAddress,
@@ -105,7 +89,101 @@ export class WalletCoreWrapper {
10589
utxos: utxos,
10690
ttl: new Long(req.ttl),
10791
};
108-
return this.TW.Cardano.Proto.SigningInput.encode(input);
92+
return this.TW.Cardano.Proto.SigningInput.encode(input).finish();
93+
}
94+
buildCardanoUnsignedMessage(txInput) {
95+
const preimage = this.preImageHashes(this.CoinType.cardano, txInput);
96+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
97+
return out.dataHash;
98+
}
99+
buildCosmosUnsignedTx(jsonStr) {
100+
const req = JSON.parse(jsonStr);
101+
console.log(req);
102+
const message = this.TW.Cosmos.Proto.Message.create({
103+
sendCoinsMessage: this.TW.Cosmos.Proto.Message.Send.create({
104+
fromAddress: req.fromAddress,
105+
toAddress: req.toAddress,
106+
amounts: [
107+
this.TW.Cosmos.Proto.Amount.create({
108+
denom: req.denom,
109+
amount: req.amount,
110+
}),
111+
]
112+
})
113+
});
114+
const input = {
115+
chainId: req.chainId,
116+
accountNumber: req.accountNumber,
117+
sequence: req.sequence,
118+
messages: [message],
119+
memo: req.memo,
120+
publicKey: this.HexCoding.decode(req.publicKey),
121+
fee: this.TW.Cosmos.Proto.Fee.create({
122+
gas: req.gas,
123+
amounts: [
124+
this.TW.Cosmos.Proto.Amount.create({
125+
denom: req.gasDenom,
126+
amount: req.gasAmount,
127+
})
128+
]
129+
})
130+
};
131+
return this.TW.Cosmos.Proto.SigningInput.encode(input).finish();
132+
}
133+
buildCosmosUnsignedMessage(txInput) {
134+
const preimage = this.preImageHashes(this.CoinType.cosmos, txInput);
135+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
136+
return out.dataHash;
137+
}
138+
buildNearUnsignedTx(jsonStr) {
139+
const req = JSON.parse(jsonStr);
140+
console.log(req);
141+
const action = this.TW.NEAR.Proto.Action.create({
142+
transfer: this.TW.NEAR.Proto.Transfer.create({
143+
deposit: this.HexCoding.decode(req.transferAmount)
144+
})
145+
});
146+
const input = {
147+
signerId: req.signerId,
148+
nonce: Long.fromString(String(req.nonce)),
149+
receiverId: req.receiverId,
150+
blockHash: this.HexCoding.decode(req.blockHash),
151+
actions: [action],
152+
publicKey: this.HexCoding.decode(req.publicKey),
153+
};
154+
console.log(input);
155+
return this.TW.NEAR.Proto.SigningInput.encode(input).finish();
156+
}
157+
buildNearUnsignedMessage(txInput) {
158+
const preimage = this.preImageHashes(this.CoinType.near, txInput);
159+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
160+
return out.dataHash;
161+
}
162+
buildStellarUnsignedTx(jsonStr) {
163+
const req = JSON.parse(jsonStr);
164+
console.log(req);
165+
const opPayment = TW.Stellar.Proto.OperationPayment.create({
166+
destination: req.destination,
167+
amount: Long.fromString(String(req.amount)),
168+
});
169+
const memoText = this.TW.Stellar.Proto.MemoText.create({
170+
text: req.memoText
171+
});
172+
const input = {
173+
fee: req.fee,
174+
account: req.account,
175+
passphrase: 'Public Global Stellar Network ; September 2015',
176+
sequence: Long.fromString(String(req.sequence)),
177+
opPayment: opPayment,
178+
memoText: memoText
179+
};
180+
console.log(input);
181+
return this.TW.Stellar.Proto.SigningInput.encode(input).finish();
182+
}
183+
buildStellarUnsignedMessage(txInput) {
184+
const preimage = this.preImageHashes(this.CoinType.stellar, txInput);
185+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
186+
return out.dataHash;
109187
}
110188
marshalInput(input) {
111189
return input.finish();

wasmjs/dist/test/tw.test.js

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ describe("Aptos", () => {
1818
toAddress: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30",
1919
amount: 1000
2020
});
21-
const inputWriter = wallet.createAptosInputFromJson(jsonString);
22-
const inputBytes = wallet.marshalInput(inputWriter);
21+
const inputBytes = wallet.buildAptosUnsignedTx(jsonString);
2322
const preimageHash = wallet.preImageHashes(wallet.CoinType.aptos, inputBytes);
24-
console.log('preimagehash in test:', preimageHash);
2523
expect(preimageHash.length).toBeGreaterThan(0);
26-
const sigHash = wallet.buildAptosUnsignedMessageRaw(inputBytes);
27-
console.log('sigHash:', wallet.encodeHex(sigHash));
24+
let sigHash = wallet.buildAptosUnsignedMessage(inputBytes);
25+
console.log('sigHash raw:', (sigHash));
2826
const sigHex = '5707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01';
2927
const pubHex = 'ea526ba1710343d953461ff68641f1b7df5f23b9042ffa2d2a798d3adb3f3d6c';
3028
const signingOutput = wallet.compileWithSignatures(wallet.CoinType.aptos, inputBytes, [sigHex], [pubHex]);
@@ -54,10 +52,11 @@ describe("Aptos", () => {
5452
describe("Cardano", () => {
5553
it("compile signature", async () => {
5654
const jsonString = '{\"address\":\"\",\"toAddress\":\"addr1q90uh2eawrdc9vaemftgd50l28yrh9lqxtjjh4z6dnn0u7ggasexxdyyk9f05atygnjlccsjsggtc87hhqjna32fpv5qeq96ls\",\"changeAddress\":\"addr1qx55ymlqemndq8gluv40v58pu76a2tp4mzjnyx8n6zrp2vtzrs43a0057y0edkn8lh9su8vh5lnhs4npv6l9tuvncv8swc7t08\",\"amount\":3000000,\"ttl\":190000000,\"utxos\":[{\"tx_hash\":\"gxblAH1h+5BlLKu0EUGXKji1vGCVTWAs+ENHaqP2f2M=\",\"output_index\":0,\"address\":\"Ae2tdPwUPEZ6vkqxSjJxaQYmDxHf5DTnxtZ67pFLJGTb9LTnCGkDP6ca3f8\",\"amount\":2500000},{\"tx_hash\":\"4pOSxZyQP++5BXMFh9IsrovaML2Nmu7D7KCCrndnWUY=\",\"output_index\":0,\"address\":\"Ae2tdPwUPEZ6vkqxSjJxaQYmDxHf5DTnxtZ67pFLJGTb9LTnCGkDP6ca3f8\",\"amount\":1700000}]}';
57-
const inputWriter = wallet.createCardanoInputFromJson(jsonString);
58-
const inputBytes = wallet.marshalInput(inputWriter);
55+
const inputBytes = wallet.buildCardanoUnsignedTx(jsonString);
5956
const preimageHash = wallet.preImageHashes(wallet.CoinType.cardano, inputBytes);
6057
expect(preimageHash.length).toBeGreaterThan(0);
58+
let sigHash = wallet.buildCardanoUnsignedMessage(inputBytes);
59+
console.log('sigHash raw:', (sigHash));
6160
const sigHex = '6a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d09';
6261
const pubHex = 'd163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41ea7f484aa383806735c46fd769c679ee41f8952952036a6e2338ada940b8a91f40b5aaa6103dc10842894a1eeefc5447b9bcb9bcf227d77e57be195d17bc03263d46f19d0fbf75afb0b9a24e31d533f4fd74cee3b56e162568e8defe37123afc4';
6362
const signingOutput = wallet.compileWithSignatures(wallet.CoinType.cardano, inputBytes, [sigHex], [pubHex]);
@@ -67,3 +66,56 @@ describe("Cardano", () => {
6766
expect(txencoded).to.equal("0x83a400828258208316e5007d61fb90652cabb41141972a38b5bc60954d602cf843476aa3f67f6300825820e29392c59c903fefb905730587d22cae8bda30bd8d9aeec3eca082ae77675946000182825839015fcbab3d70db82b3b9da5686d1ff51c83b97e032e52bd45a6ce6fe7908ec32633484b152fa756444e5fc62128210bc1fd7b8253ec5490b281a002dc6c082583901a9426fe0cee6d01d1fe32af650e1e7b5d52c35d8a53218f3d0861531621c2b1ebdf4f11f96da67fdcb0e1d97a7e778566166be55f193c30f1a000f9ec1021a0002b0bf031a0b532b80a20081825820d163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41e58406a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d090281845820d163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41e58406a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d095820a7f484aa383806735c46fd769c679ee41f8952952036a6e2338ada940b8a91f441a0f6");
6867
});
6968
});
69+
describe("Cosmos", () => {
70+
it("compile signature", async () => {
71+
const jsonString = "{\"chainId\":\"cosmoshub-4\",\"fromAddress\":\"cosmos1mky69cn8ektwy0845vec9upsdphktxt03gkwlx\",\"toAddress\":\"cosmos18s0hdnsllgcclweu9aymw4ngktr2k0rkygdzdp\",\"amount\":\"400000\",\"denom\":\"uatom\",\"memo\":\"\",\"gas\":200000,\"gasAmount\":\"1000\",\"gasDenom\":\"uatom\",\"publicKey\":\"02ecef5ce437a302c67f95468de4b31f36e911f467d7e6a52b41c1e13e1d563649\",\"accountNumber\":546179,\"sequence\":0}";
72+
const inputBytes = wallet.buildCosmosUnsignedTx(jsonString);
73+
console.log('Inputbytes:', inputBytes);
74+
const preimageHash = wallet.preImageHashes(wallet.CoinType.cosmos, inputBytes);
75+
expect(preimageHash.length).toBeGreaterThan(0);
76+
let sigHash = wallet.buildCosmosUnsignedMessage(inputBytes);
77+
console.log('sigHash raw:', wallet.encodeHex(sigHash));
78+
const sigHex = 'afbd513a776f4fdf470ef7f9675f21ae9d630fc4d635d8dbaa0dc0a716434cd07e02510765d4673dfa880825bae8e67cb367396ff6b976fc6b19a31fc95e8097';
79+
const pubHex = '02ecef5ce437a302c67f95468de4b31f36e911f467d7e6a52b41c1e13e1d563649';
80+
const signingOutput = wallet.compileWithSignatures(wallet.CoinType.cosmos, inputBytes, [sigHex], [pubHex]);
81+
const decoded = wallet.TW.Cosmos.Proto.SigningOutput.decode(signingOutput);
82+
console.log(decoded);
83+
expect(decoded.json.length).toBeGreaterThan(0);
84+
});
85+
});
86+
describe("NEAR", () => {
87+
it("compile signature", async () => {
88+
const jsonStr = "{\"signerId\":\"test.near\",\"nonce\":1,\"receiverId\":\"whatever.near\",\"blockHash\":\"0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6\",\"publicKey\":\"917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d\",\"transferAmount\":\"01000000000000000000000000000000\"}";
89+
const inputBytes = wallet.buildNearUnsignedTx(jsonStr);
90+
console.log('Inputbytes:', inputBytes.length);
91+
console.log('Inputbytes:', wallet.encodeHex(inputBytes));
92+
const preimageHash = wallet.preImageHashes(wallet.CoinType.near, inputBytes);
93+
expect(preimageHash.length).toBeGreaterThan(0);
94+
console.log('preimageHash raw:', wallet.encodeHex(preimageHash));
95+
let sigHash = wallet.buildNearUnsignedMessage(inputBytes);
96+
console.log('sigHash raw:', wallet.encodeHex(sigHash));
97+
const sigHex = '969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01';
98+
const pubHex = '917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d';
99+
const signingOutput = wallet.compileWithSignatures(wallet.CoinType.near, inputBytes, [sigHex], [pubHex]);
100+
const decoded = wallet.TW.NEAR.Proto.SigningOutput.decode(signingOutput);
101+
expect(decoded.signedTransaction.length).toBeGreaterThan(0);
102+
});
103+
});
104+
describe("Stellar", () => {
105+
it("compile signature", async () => {
106+
const jsonStr = "{\"signerId\":\"test.near\",\"nonce\":1,\"receiverId\":\"whatever.near\",\"blockHash\":\"0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6\",\"publicKey\":\"917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d\",\"transferAmount\":\"01000000000000000000000000000000\"}";
107+
const inputBytes = wallet.buildNearUnsignedTx(jsonStr);
108+
console.log('Inputbytes:', inputBytes.length);
109+
console.log('Inputbytes:', wallet.encodeHex(inputBytes));
110+
const preimageHash = wallet.preImageHashes(wallet.CoinType.near, inputBytes);
111+
expect(preimageHash.length).toBeGreaterThan(0);
112+
console.log('preimageHash raw:', wallet.encodeHex(preimageHash));
113+
let sigHash = wallet.buildNearUnsignedMessage(inputBytes);
114+
console.log('sigHash raw:', wallet.encodeHex(sigHash));
115+
const sigHex = '969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01';
116+
const pubHex = '917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d';
117+
const signingOutput = wallet.compileWithSignatures(wallet.CoinType.near, inputBytes, [sigHex], [pubHex]);
118+
const decoded = wallet.TW.NEAR.Proto.SigningOutput.decode(signingOutput);
119+
expect(decoded.signedTransaction.length).toBeGreaterThan(0);
120+
});
121+
});

wasmjs/src/index.ts

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ export class WalletCoreWrapper {
5353
}
5454

5555
preImageHashes(coin: InstanceType<WalletCore["CoinType"]>, txInput: Uint8Array): Uint8Array {
56-
console.log('core package', (coin), (txInput));
5756
const result = this.TransactionCompiler.preImageHashes(coin, txInput);
58-
console.log('RESULT', result)
5957
return result
6058
}
6159

@@ -101,7 +99,7 @@ export class WalletCoreWrapper {
10199
return this.TW.Aptos.Proto.SigningInput.encode(input).finish();
102100
}
103101

104-
buildAptosUnsignedMessage(txInput: Uint8Array): Uint8Array<ArrayBufferLike> {
102+
buildAptosUnsignedMessage(txInput: Uint8Array): Uint8Array {
105103
const preimage = this.preImageHashes(this.CoinType.aptos, txInput);
106104
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage)
107105
return out.data
@@ -137,12 +135,106 @@ export class WalletCoreWrapper {
137135
return this.TW.Cardano.Proto.SigningInput.encode(input).finish();
138136
}
139137

140-
buildCardanoUnsignedMessage(txInput: Uint8Array): Uint8Array<ArrayBufferLike> {
138+
buildCardanoUnsignedMessage(txInput: Uint8Array): Uint8Array {
141139
const preimage = this.preImageHashes(this.CoinType.cardano, txInput);
142140
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage)
143141
return out.dataHash
144142
}
145143

144+
buildCosmosUnsignedTx(jsonStr: string): Uint8Array {
145+
const req = JSON.parse(jsonStr);
146+
147+
const message = this.TW.Cosmos.Proto.Message.create({
148+
sendCoinsMessage: this.TW.Cosmos.Proto.Message.Send.create({
149+
fromAddress: req.fromAddress,
150+
toAddress: req.toAddress,
151+
amounts: [
152+
this.TW.Cosmos.Proto.Amount.create({
153+
denom: req.denom,
154+
amount: req.amount,
155+
}),
156+
]
157+
})
158+
});
159+
160+
const input = {
161+
chainId: req.chainId,
162+
accountNumber: req.accountNumber,
163+
sequence: req.sequence,
164+
messages: [message],
165+
memo: req.memo,
166+
publicKey: this.HexCoding.decode(req.publicKey),
167+
fee: this.TW.Cosmos.Proto.Fee.create({
168+
gas: req.gas,
169+
amounts: [
170+
this.TW.Cosmos.Proto.Amount.create({
171+
denom: req.gasDenom,
172+
amount: req.gasAmount,
173+
})
174+
]
175+
})
176+
};
177+
return this.TW.Cosmos.Proto.SigningInput.encode(input).finish();
178+
}
179+
180+
buildCosmosUnsignedMessage(txInput: Uint8Array): Uint8Array {
181+
const preimage = this.preImageHashes(this.CoinType.cosmos, txInput);
182+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage)
183+
return out.dataHash
184+
}
185+
186+
buildNearUnsignedTx(jsonStr: string): Uint8Array {
187+
const req = JSON.parse(jsonStr);
188+
189+
const action = this.TW.NEAR.Proto.Action.create({
190+
transfer: this.TW.NEAR.Proto.Transfer.create({
191+
deposit: this.HexCoding.decode(req.transferAmount)
192+
})
193+
})
194+
195+
const input = {
196+
signerId: req.signerId,
197+
nonce: Long.fromString(String(req.nonce)),
198+
receiverId: req.receiverId,
199+
blockHash: this.HexCoding.decode(req.blockHash),
200+
actions: [action],
201+
publicKey: this.HexCoding.decode(req.publicKey),
202+
};
203+
return this.TW.NEAR.Proto.SigningInput.encode(input).finish();
204+
}
205+
206+
buildNearUnsignedMessage(txInput: Uint8Array): Uint8Array {
207+
const preimage = this.preImageHashes(this.CoinType.near, txInput);
208+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage)
209+
return out.dataHash
210+
}
211+
212+
buildStellarUnsignedTx(jsonStr: string): Uint8Array {
213+
const req = JSON.parse(jsonStr);
214+
215+
const opPayment = TW.Stellar.Proto.OperationPayment.create({
216+
destination: req.destination,
217+
amount: Long.fromString(String(req.amount)),
218+
})
219+
const memoText = this.TW.Stellar.Proto.MemoText.create({
220+
text: req.memoText
221+
})
222+
const input = {
223+
fee : req.fee,
224+
account: req.account,
225+
passphrase: 'Public Global Stellar Network ; September 2015',
226+
sequence: Long.fromString(String(req.sequence)),
227+
opPayment: opPayment,
228+
memoText: memoText
229+
};
230+
return this.TW.Stellar.Proto.SigningInput.encode(input).finish();
231+
}
232+
233+
buildStellarUnsignedMessage(txInput: Uint8Array): Uint8Array {
234+
const preimage = this.preImageHashes(this.CoinType.stellar, txInput);
235+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage)
236+
return out.dataHash
237+
}
146238

147239
marshalInput(input: protobuf.Writer): Uint8Array {
148240
return input.finish();

0 commit comments

Comments
 (0)