Skip to content
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 wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ import * as KeyStore from "./src/keystore";
declare function load(): Promise<WalletCore>;

export const initWasm: typeof load = Loader;
export { TW, WalletCore, KeyStore };
export { TW, WalletCore, KeyStore};
4 changes: 2 additions & 2 deletions wasm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trustwallet/wallet-core",
"version": "1.0.0",
"name": "craminiumlab-wallet-core",
"version": "1.0.3",
"description": "wallet core wasm and protobuf models",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 7 additions & 1 deletion wasm/tests/Blockchain/Aptos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Long = require("long");

describe("Aptos", () => {
it("test sign aptos", () => {
const { PrivateKey, HexCoding, AnySigner, AnyAddress, CoinType } = globalThis.core;
const { PrivateKey, HexCoding, AnySigner, CoinType, TransactionCompiler } = globalThis.core;
const txDataInput = TW.Aptos.Proto.SigningInput.create({
chainId: 33,
sender: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30",
Expand All @@ -29,10 +29,16 @@ describe("Aptos", () => {
).data(),
});
const input = TW.Aptos.Proto.SigningInput.encode(txDataInput).finish();
console.log('Aptos input:', input.length)
console.log(TransactionCompiler)
const hash = TransactionCompiler.preImageHashes(CoinType.aptos, input)
console.log('hash:', hash)
console.log('type:', CoinType.aptos.value)
const outputData = AnySigner.sign(input, CoinType.aptos);
const output = TW.Aptos.Proto.SigningOutput.decode(outputData);
assert.equal(HexCoding.encode(output.encoded), "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada00000000210020ea526ba1710343d953461ff68641f1b7df5f23b9042ffa2d2a798d3adb3f3d6c405707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01")
assert.equal(HexCoding.encode(output.authenticator!.signature), "0x5707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01")
assert.equal(HexCoding.encode(output.rawTxn), "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada0000000021")
console.log(globalThis.walletCore)
});
});
3 changes: 3 additions & 0 deletions wasm/tests/Blockchain/Cosmos.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {TW} from "../../dist";
import {assert} from "chai";
import Long = require("long");
2 changes: 1 addition & 1 deletion wasm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"node_modules",
"./tests/**/*.ts"
],
}
}
191 changes: 191 additions & 0 deletions wasmjs/dist/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import * as Core from "craminiumlab-wallet-core";
import { TW } from "craminiumlab-wallet-core";
import { initWasm } from "craminiumlab-wallet-core";
import Long from "long";
export { initWasm };
export class WalletCoreWrapper {
constructor(core, _tw = TW) {
const { CoinType, HexCoding, AnySigner, TransactionCompiler, DataVector, PrivateKey } = core;
this.CoinType = CoinType;
this.HexCoding = HexCoding;
this.AnySigner = AnySigner;
this.TransactionCompiler = TransactionCompiler;
this.DataVector = DataVector;
this.PrivateKey = PrivateKey;
this.TW = _tw;
}
static async init() {
const core = await Core.initWasm();
return new WalletCoreWrapper(core);
}
encodeHex(data) {
return this.HexCoding.encode(data);
}
createWithData(hexStr) {
const data = this.HexCoding.decode(hexStr);
const privateKey = this.PrivateKey.createWithData(data);
return privateKey.data();
}
anySign(input, coin) {
return this.AnySigner.sign(input, coin);
}
preImageHashes(coin, txInput) {
const result = this.TransactionCompiler.preImageHashes(coin, txInput);
console.log('RESULT', result, result.length);
return result;
}
compileWithSignatures(coinType, txInputData, signatures, publicKeys) {
const sigs = signatures.map((s) => this.HexCoding.decode(s));
const pubs = publicKeys.map((p) => this.HexCoding.decode(p));
const sigVec = this.makeDataVector(sigs);
const pubVec = this.makeDataVector(pubs);
return this.TransactionCompiler.compileWithSignatures(coinType, txInputData, sigVec, pubVec);
}
makeDataVector(items) {
const vec = this.DataVector.create();
items.forEach((i) => vec.add(i));
return vec;
}
buildAptosUnsignedTx(jsonStr) {
const req = JSON.parse(jsonStr);
const input = {
chainId: req.chainId,
sender: req.sender,
sequenceNumber: Long.fromString(String(req.sequenceNumber)),
expirationTimestampSecs: Long.fromString(String(req.ttl)),
gasUnitPrice: Long.fromString(String(req.gasUnitPrice)),
maxGasAmount: Long.fromString(String(req.maxGasAmount)),
transfer: this.TW.Aptos.Proto.TransferMessage.create({
to: req.toAddress,
amount: Long.fromString(String(req.amount)),
}),
};
return this.TW.Aptos.Proto.SigningInput.encode(input).finish();
}
buildAptosUnsignedMessage(txInput) {
const preimage = this.preImageHashes(this.CoinType.aptos, txInput);
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
return out.data;
}
buildCardanoUnsignedTx(jsonStr) {
const req = JSON.parse(jsonStr);
const transfer = this.TW.Cardano.Proto.Transfer.create({
toAddress: req.toAddress,
changeAddress: req.changeAddress,
amount: req.amount,
useMaxAmount: false,
});
const utxos = req.utxos.map((utxo) => this.TW.Cardano.Proto.TxInput.create({
outPoint: this.TW.Cardano.Proto.OutPoint.create({
txHash: utxo.tx_hash,
outputIndex: utxo.output_index,
}),
address: utxo.address,
amount: utxo.amount,
tokenAmount: null
}));
const input = {
transferMessage: transfer,
utxos: utxos,
ttl: new Long(req.ttl),
};
return this.TW.Cardano.Proto.SigningInput.encode(input).finish();
}
buildCardanoUnsignedMessage(txInput) {
const preimage = this.preImageHashes(this.CoinType.cardano, txInput);
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
return out.dataHash;
}
buildCosmosUnsignedTx(jsonStr) {
const req = JSON.parse(jsonStr);
console.log(req);
const message = this.TW.Cosmos.Proto.Message.create({
sendCoinsMessage: this.TW.Cosmos.Proto.Message.Send.create({
fromAddress: req.fromAddress,
toAddress: req.toAddress,
amounts: [
this.TW.Cosmos.Proto.Amount.create({
denom: req.denom,
amount: req.amount,
}),
]
})
});
const input = {
chainId: req.chainId,
accountNumber: req.accountNumber,
sequence: req.sequence,
messages: [message],
memo: req.memo,
publicKey: this.HexCoding.decode(req.publicKey),
fee: this.TW.Cosmos.Proto.Fee.create({
gas: req.gas,
amounts: [
this.TW.Cosmos.Proto.Amount.create({
denom: req.gasDenom,
amount: req.gasAmount,
})
]
})
};
return this.TW.Cosmos.Proto.SigningInput.encode(input).finish();
}
buildCosmosUnsignedMessage(txInput) {
const preimage = this.preImageHashes(this.CoinType.cosmos, txInput);
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
return out.dataHash;
}
buildNearUnsignedTx(jsonStr) {
const req = JSON.parse(jsonStr);
console.log(req);
const action = this.TW.NEAR.Proto.Action.create({
transfer: this.TW.NEAR.Proto.Transfer.create({
deposit: this.HexCoding.decode(req.transferAmount)
})
});
const input = {
signerId: req.signerId,
nonce: Long.fromString(String(req.nonce)),
receiverId: req.receiverId,
blockHash: this.HexCoding.decode(req.blockHash),
actions: [action],
publicKey: this.HexCoding.decode(req.publicKey),
};
console.log(input);
return this.TW.NEAR.Proto.SigningInput.encode(input).finish();
}
buildNearUnsignedMessage(txInput) {
const preimage = this.preImageHashes(this.CoinType.near, txInput);
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
return out.dataHash;
}
buildStellarUnsignedTx(jsonStr) {
const req = JSON.parse(jsonStr);
console.log(req);
const opPayment = TW.Stellar.Proto.OperationPayment.create({
destination: req.destination,
amount: Long.fromString(String(req.amount)),
});
const memoText = this.TW.Stellar.Proto.MemoText.create({
text: req.memoText
});
const input = {
fee: req.fee,
account: req.account,
passphrase: 'Public Global Stellar Network ; September 2015',
sequence: Long.fromString(String(req.sequence)),
opPayment: opPayment,
memoText: memoText
};
console.log(input);
return this.TW.Stellar.Proto.SigningInput.encode(input).finish();
}
buildStellarUnsignedMessage(txInput) {
const preimage = this.preImageHashes(this.CoinType.stellar, txInput);
const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage);
return out.dataHash;
}
marshalInput(input) {
return input.finish();
}
}
Loading
Loading