Skip to content

Commit 07d3ec8

Browse files
authored
Merge pull request #10 from Cramiumlabs/feat/wasm
Feat/wasm
2 parents a20cb74 + 59527cc commit 07d3ec8

File tree

18 files changed

+3326
-19
lines changed

18 files changed

+3326
-19
lines changed

wasm/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ import * as KeyStore from "./src/keystore";
1010
declare function load(): Promise<WalletCore>;
1111

1212
export const initWasm: typeof load = Loader;
13-
export { TW, WalletCore, KeyStore };
13+
export { TW, WalletCore, KeyStore};

wasm/package-lock.json

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

wasm/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@trustwallet/wallet-core",
3-
"version": "1.0.0",
2+
"name": "craminiumlab-wallet-core",
3+
"version": "1.0.3",
44
"description": "wallet core wasm and protobuf models",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

wasm/tests/Blockchain/Aptos.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Long = require("long");
1010

1111
describe("Aptos", () => {
1212
it("test sign aptos", () => {
13-
const { PrivateKey, HexCoding, AnySigner, AnyAddress, CoinType } = globalThis.core;
13+
const { PrivateKey, HexCoding, AnySigner, CoinType, TransactionCompiler } = globalThis.core;
1414
const txDataInput = TW.Aptos.Proto.SigningInput.create({
1515
chainId: 33,
1616
sender: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30",
@@ -29,10 +29,16 @@ describe("Aptos", () => {
2929
).data(),
3030
});
3131
const input = TW.Aptos.Proto.SigningInput.encode(txDataInput).finish();
32+
console.log('Aptos input:', input.length)
33+
console.log(TransactionCompiler)
34+
const hash = TransactionCompiler.preImageHashes(CoinType.aptos, input)
35+
console.log('hash:', hash)
36+
console.log('type:', CoinType.aptos.value)
3237
const outputData = AnySigner.sign(input, CoinType.aptos);
3338
const output = TW.Aptos.Proto.SigningOutput.decode(outputData);
3439
assert.equal(HexCoding.encode(output.encoded), "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada00000000210020ea526ba1710343d953461ff68641f1b7df5f23b9042ffa2d2a798d3adb3f3d6c405707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01")
3540
assert.equal(HexCoding.encode(output.authenticator!.signature), "0x5707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01")
3641
assert.equal(HexCoding.encode(output.rawTxn), "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada0000000021")
42+
console.log(globalThis.walletCore)
3743
});
3844
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {TW} from "../../dist";
2+
import {assert} from "chai";
3+
import Long = require("long");

wasm/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
"node_modules",
2121
"./tests/**/*.ts"
2222
],
23-
}
23+
}

wasmjs/dist/src/index.js

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import * as Core from "craminiumlab-wallet-core";
2+
import { TW } from "craminiumlab-wallet-core";
3+
import { initWasm } from "craminiumlab-wallet-core";
4+
import Long from "long";
5+
export { initWasm };
6+
export class WalletCoreWrapper {
7+
constructor(core, _tw = TW) {
8+
const { CoinType, HexCoding, AnySigner, TransactionCompiler, DataVector, PrivateKey } = core;
9+
this.CoinType = CoinType;
10+
this.HexCoding = HexCoding;
11+
this.AnySigner = AnySigner;
12+
this.TransactionCompiler = TransactionCompiler;
13+
this.DataVector = DataVector;
14+
this.PrivateKey = PrivateKey;
15+
this.TW = _tw;
16+
}
17+
static async init() {
18+
const core = await Core.initWasm();
19+
return new WalletCoreWrapper(core);
20+
}
21+
encodeHex(data) {
22+
return this.HexCoding.encode(data);
23+
}
24+
createWithData(hexStr) {
25+
const data = this.HexCoding.decode(hexStr);
26+
const privateKey = this.PrivateKey.createWithData(data);
27+
return privateKey.data();
28+
}
29+
anySign(input, coin) {
30+
return this.AnySigner.sign(input, coin);
31+
}
32+
preImageHashes(coin, txInput) {
33+
const result = this.TransactionCompiler.preImageHashes(coin, txInput);
34+
console.log('RESULT', result, result.length);
35+
return result;
36+
}
37+
compileWithSignatures(coinType, txInputData, signatures, publicKeys) {
38+
const sigs = signatures.map((s) => this.HexCoding.decode(s));
39+
const pubs = publicKeys.map((p) => this.HexCoding.decode(p));
40+
const sigVec = this.makeDataVector(sigs);
41+
const pubVec = this.makeDataVector(pubs);
42+
return this.TransactionCompiler.compileWithSignatures(coinType, txInputData, sigVec, pubVec);
43+
}
44+
makeDataVector(items) {
45+
const vec = this.DataVector.create();
46+
items.forEach((i) => vec.add(i));
47+
return vec;
48+
}
49+
buildAptosUnsignedTx(jsonStr) {
50+
const req = JSON.parse(jsonStr);
51+
const input = {
52+
chainId: req.chainId,
53+
sender: req.sender,
54+
sequenceNumber: Long.fromString(String(req.sequenceNumber)),
55+
expirationTimestampSecs: Long.fromString(String(req.ttl)),
56+
gasUnitPrice: Long.fromString(String(req.gasUnitPrice)),
57+
maxGasAmount: Long.fromString(String(req.maxGasAmount)),
58+
transfer: this.TW.Aptos.Proto.TransferMessage.create({
59+
to: req.toAddress,
60+
amount: Long.fromString(String(req.amount)),
61+
}),
62+
};
63+
return this.TW.Aptos.Proto.SigningInput.encode(input).finish();
64+
}
65+
buildAptosUnsignedMessage(txInput) {
66+
const preimage = this.preImageHashes(this.CoinType.aptos, txInput);
67+
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
68+
return out.data;
69+
}
70+
buildCardanoUnsignedTx(jsonStr) {
71+
const req = JSON.parse(jsonStr);
72+
const transfer = this.TW.Cardano.Proto.Transfer.create({
73+
toAddress: req.toAddress,
74+
changeAddress: req.changeAddress,
75+
amount: req.amount,
76+
useMaxAmount: false,
77+
});
78+
const utxos = req.utxos.map((utxo) => this.TW.Cardano.Proto.TxInput.create({
79+
outPoint: this.TW.Cardano.Proto.OutPoint.create({
80+
txHash: utxo.tx_hash,
81+
outputIndex: utxo.output_index,
82+
}),
83+
address: utxo.address,
84+
amount: utxo.amount,
85+
tokenAmount: null
86+
}));
87+
const input = {
88+
transferMessage: transfer,
89+
utxos: utxos,
90+
ttl: new Long(req.ttl),
91+
};
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;
187+
}
188+
marshalInput(input) {
189+
return input.finish();
190+
}
191+
}

0 commit comments

Comments
 (0)