diff --git a/wasmjs/dist/index.d.ts b/wasmjs/dist/index.d.ts new file mode 100644 index 00000000000..2668f587d48 --- /dev/null +++ b/wasmjs/dist/index.d.ts @@ -0,0 +1,34 @@ +import * as Core from "craminiumlab-wallet-core"; +import { TW } from "craminiumlab-wallet-core"; +import { WalletCore, initWasm } from "craminiumlab-wallet-core"; +export { initWasm }; +export declare class WalletCoreWrapper { + CoinType: WalletCore["CoinType"]; + HexCoding: WalletCore["HexCoding"]; + AnySigner: WalletCore["AnySigner"]; + TransactionCompiler: WalletCore["TransactionCompiler"]; + DataVector: WalletCore["DataVector"]; + PrivateKey: WalletCore["PrivateKey"]; + TW: typeof TW; + constructor(core: WalletCore, _tw?: typeof Core.TW); + static init(): Promise; + encodeHex(data: Uint8Array): string; + createWithData(hexStr: string): Uint8Array; + anySign(input: Uint8Array, coin: InstanceType): Uint8Array; + preImageHashes(coin: InstanceType, txInput: Uint8Array): Uint8Array; + compileWithSignatures(coinType: InstanceType, txInputData: Uint8Array, signatures: string[], publicKeys: string[]): Uint8Array; + makeDataVector(items: Uint8Array[]): ReturnType; + buildAptosUnsignedTx(jsonStr: string): Uint8Array; + buildAptosUnsignedMessage(txInput: Uint8Array): Uint8Array; + buildCardanoUnsignedTx(jsonStr: string): Uint8Array; + buildCardanoUnsignedMessage(txInput: Uint8Array): Uint8Array; + buildCosmosUnsignedTx(jsonStr: string): Uint8Array; + buildCosmosUnsignedMessage(txInput: Uint8Array): Uint8Array; + buildNearUnsignedTx(jsonStr: string): Uint8Array; + buildNearUnsignedMessage(txInput: Uint8Array): Uint8Array; + buildStellarUnsignedTx(jsonStr: string): Uint8Array; + buildStellarUnsignedMessage(txInput: Uint8Array): Uint8Array; + buildEVMUnsignedTx(jsonStr: string): Uint8Array; + buildEVMUnsignedMessage(txInput: Uint8Array): Uint8Array; + marshalInput(input: protobuf.Writer): Uint8Array; +} diff --git a/wasmjs/dist/src/index.js b/wasmjs/dist/index.js similarity index 88% rename from wasmjs/dist/src/index.js rename to wasmjs/dist/index.js index d1eee748ae8..150f35be687 100644 --- a/wasmjs/dist/src/index.js +++ b/wasmjs/dist/index.js @@ -31,7 +31,6 @@ export class WalletCoreWrapper { } preImageHashes(coin, txInput) { const result = this.TransactionCompiler.preImageHashes(coin, txInput); - console.log('RESULT', result, result.length); return result; } compileWithSignatures(coinType, txInputData, signatures, publicKeys) { @@ -98,7 +97,6 @@ export class WalletCoreWrapper { } 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, @@ -137,7 +135,6 @@ export class WalletCoreWrapper { } 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) @@ -151,7 +148,6 @@ export class WalletCoreWrapper { actions: [action], publicKey: this.HexCoding.decode(req.publicKey), }; - console.log(input); return this.TW.NEAR.Proto.SigningInput.encode(input).finish(); } buildNearUnsignedMessage(txInput) { @@ -161,7 +157,6 @@ export class WalletCoreWrapper { } 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)), @@ -177,7 +172,6 @@ export class WalletCoreWrapper { opPayment: opPayment, memoText: memoText }; - console.log(input); return this.TW.Stellar.Proto.SigningInput.encode(input).finish(); } buildStellarUnsignedMessage(txInput) { @@ -185,6 +179,30 @@ export class WalletCoreWrapper { const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); return out.dataHash; } + buildEVMUnsignedTx(jsonStr) { + const req = JSON.parse(jsonStr); + 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 + }; + return this.TW.Stellar.Proto.SigningInput.encode(input).finish(); + } + buildEVMUnsignedMessage(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(); } diff --git a/wasmjs/dist/test/tw.test.js b/wasmjs/dist/test/tw.test.js deleted file mode 100644 index cdd6d6cc794..00000000000 --- a/wasmjs/dist/test/tw.test.js +++ /dev/null @@ -1,121 +0,0 @@ -import { describe, it, expect, beforeAll } from 'vitest'; -import { WalletCoreWrapper, initWasm } from "../src"; -import Long from "long"; -let wallet; -beforeAll(async () => { - const core = await initWasm(); - wallet = await new WalletCoreWrapper(core); -}); -describe("Aptos", () => { - it("compile signature", async () => { - const jsonString = JSON.stringify({ - chainId: 33, - sender: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30", - sequenceNumber: 99, - ttl: 3664390082, - maxGasAmount: 3296766, - gasUnitPrice: 100, - toAddress: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30", - amount: 1000 - }); - const inputBytes = wallet.buildAptosUnsignedTx(jsonString); - const preimageHash = wallet.preImageHashes(wallet.CoinType.aptos, inputBytes); - expect(preimageHash.length).toBeGreaterThan(0); - let sigHash = wallet.buildAptosUnsignedMessage(inputBytes); - console.log('sigHash raw:', (sigHash)); - const sigHex = '5707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01'; - const pubHex = 'ea526ba1710343d953461ff68641f1b7df5f23b9042ffa2d2a798d3adb3f3d6c'; - const signingOutput = wallet.compileWithSignatures(wallet.CoinType.aptos, inputBytes, [sigHex], [pubHex]); - const decoded = wallet.TW.Aptos.Proto.SigningOutput.decode(signingOutput); - expect(decoded.encoded.length).toBeGreaterThan(0); - }); - it("test sign aptos", async () => { - const txInput = wallet.TW.Aptos.Proto.SigningInput.create({ - chainId: 33, - sender: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30", - transfer: wallet.TW.Aptos.Proto.TransferMessage.create({ - amount: new Long(1000), - to: "0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30" - }), - sequenceNumber: new Long(99), - expirationTimestampSecs: new Long(3664390082), - gasUnitPrice: new Long(100), - maxGasAmount: new Long(3296766), - privateKey: wallet.createWithData("0x5d996aa76b3212142792d9130796cd2e11e3c445a93118c08414df4f66bc60ec") - }); - const inputBytes = wallet.TW.Aptos.Proto.SigningInput.encode(txInput).finish(); - const signed = wallet.anySign(inputBytes, wallet.CoinType.aptos); - const decoded = wallet.TW.Aptos.Proto.SigningOutput.decode(signed); - expect(wallet.encodeHex(decoded.encoded)).to.equal("0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3063000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e74087472616e7366657200022007968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f3008e803000000000000fe4d3200000000006400000000000000c2276ada00000000210020ea526ba1710343d953461ff68641f1b7df5f23b9042ffa2d2a798d3adb3f3d6c405707246db31e2335edc4316a7a656a11691d1d1647f6e864d1ab12f43428aaaf806cf02120d0b608cdd89c5c904af7b137432aacdd60cc53f9fad7bd33578e01"); - }); -}); -describe("Cardano", () => { - it("compile signature", async () => { - 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}]}'; - const inputBytes = wallet.buildCardanoUnsignedTx(jsonString); - const preimageHash = wallet.preImageHashes(wallet.CoinType.cardano, inputBytes); - expect(preimageHash.length).toBeGreaterThan(0); - let sigHash = wallet.buildCardanoUnsignedMessage(inputBytes); - console.log('sigHash raw:', (sigHash)); - const sigHex = '6a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d09'; - const pubHex = 'd163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41ea7f484aa383806735c46fd769c679ee41f8952952036a6e2338ada940b8a91f40b5aaa6103dc10842894a1eeefc5447b9bcb9bcf227d77e57be195d17bc03263d46f19d0fbf75afb0b9a24e31d533f4fd74cee3b56e162568e8defe37123afc4'; - const signingOutput = wallet.compileWithSignatures(wallet.CoinType.cardano, inputBytes, [sigHex], [pubHex]); - const decoded = wallet.TW.Cardano.Proto.SigningOutput.decode(signingOutput); - expect(decoded.encoded.length).toBeGreaterThan(0); - const txencoded = wallet.encodeHex(decoded.encoded); - expect(txencoded).to.equal("0x83a400828258208316e5007d61fb90652cabb41141972a38b5bc60954d602cf843476aa3f67f6300825820e29392c59c903fefb905730587d22cae8bda30bd8d9aeec3eca082ae77675946000182825839015fcbab3d70db82b3b9da5686d1ff51c83b97e032e52bd45a6ce6fe7908ec32633484b152fa756444e5fc62128210bc1fd7b8253ec5490b281a002dc6c082583901a9426fe0cee6d01d1fe32af650e1e7b5d52c35d8a53218f3d0861531621c2b1ebdf4f11f96da67fdcb0e1d97a7e778566166be55f193c30f1a000f9ec1021a0002b0bf031a0b532b80a20081825820d163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41e58406a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d090281845820d163c8c4f0be7c22cd3a1152abb013c855ea614b92201497a568c5d93ceeb41e58406a23ab9267867fbf021c1cb2232bc83d2cdd663d651d22d59b6cddbca5cb106d4db99da50672f69a2309ca8a329a3f9576438afe4538b013de4591a6dfcd4d095820a7f484aa383806735c46fd769c679ee41f8952952036a6e2338ada940b8a91f441a0f6"); - }); -}); -describe("Cosmos", () => { - it("compile signature", async () => { - 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}"; - const inputBytes = wallet.buildCosmosUnsignedTx(jsonString); - console.log('Inputbytes:', inputBytes); - const preimageHash = wallet.preImageHashes(wallet.CoinType.cosmos, inputBytes); - expect(preimageHash.length).toBeGreaterThan(0); - let sigHash = wallet.buildCosmosUnsignedMessage(inputBytes); - console.log('sigHash raw:', wallet.encodeHex(sigHash)); - const sigHex = 'afbd513a776f4fdf470ef7f9675f21ae9d630fc4d635d8dbaa0dc0a716434cd07e02510765d4673dfa880825bae8e67cb367396ff6b976fc6b19a31fc95e8097'; - const pubHex = '02ecef5ce437a302c67f95468de4b31f36e911f467d7e6a52b41c1e13e1d563649'; - const signingOutput = wallet.compileWithSignatures(wallet.CoinType.cosmos, inputBytes, [sigHex], [pubHex]); - const decoded = wallet.TW.Cosmos.Proto.SigningOutput.decode(signingOutput); - console.log(decoded); - expect(decoded.json.length).toBeGreaterThan(0); - }); -}); -describe("NEAR", () => { - it("compile signature", async () => { - const jsonStr = "{\"signerId\":\"test.near\",\"nonce\":1,\"receiverId\":\"whatever.near\",\"blockHash\":\"0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6\",\"publicKey\":\"917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d\",\"transferAmount\":\"01000000000000000000000000000000\"}"; - const inputBytes = wallet.buildNearUnsignedTx(jsonStr); - console.log('Inputbytes:', inputBytes.length); - console.log('Inputbytes:', wallet.encodeHex(inputBytes)); - const preimageHash = wallet.preImageHashes(wallet.CoinType.near, inputBytes); - expect(preimageHash.length).toBeGreaterThan(0); - console.log('preimageHash raw:', wallet.encodeHex(preimageHash)); - let sigHash = wallet.buildNearUnsignedMessage(inputBytes); - console.log('sigHash raw:', wallet.encodeHex(sigHash)); - const sigHex = '969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01'; - const pubHex = '917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d'; - const signingOutput = wallet.compileWithSignatures(wallet.CoinType.near, inputBytes, [sigHex], [pubHex]); - const decoded = wallet.TW.NEAR.Proto.SigningOutput.decode(signingOutput); - expect(decoded.signedTransaction.length).toBeGreaterThan(0); - }); -}); -describe("Stellar", () => { - it("compile signature", async () => { - const jsonStr = "{\"signerId\":\"test.near\",\"nonce\":1,\"receiverId\":\"whatever.near\",\"blockHash\":\"0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6\",\"publicKey\":\"917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d\",\"transferAmount\":\"01000000000000000000000000000000\"}"; - const inputBytes = wallet.buildNearUnsignedTx(jsonStr); - console.log('Inputbytes:', inputBytes.length); - console.log('Inputbytes:', wallet.encodeHex(inputBytes)); - const preimageHash = wallet.preImageHashes(wallet.CoinType.near, inputBytes); - expect(preimageHash.length).toBeGreaterThan(0); - console.log('preimageHash raw:', wallet.encodeHex(preimageHash)); - let sigHash = wallet.buildNearUnsignedMessage(inputBytes); - console.log('sigHash raw:', wallet.encodeHex(sigHash)); - const sigHex = '969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01'; - const pubHex = '917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d'; - const signingOutput = wallet.compileWithSignatures(wallet.CoinType.near, inputBytes, [sigHex], [pubHex]); - const decoded = wallet.TW.NEAR.Proto.SigningOutput.decode(signingOutput); - expect(decoded.signedTransaction.length).toBeGreaterThan(0); - }); -}); diff --git a/wasmjs/package.json b/wasmjs/package.json index 653d969d461..eb780774577 100644 --- a/wasmjs/package.json +++ b/wasmjs/package.json @@ -1,8 +1,9 @@ { "name": "craminiumlab-wasmjs", - "version": "1.0.0", + "version": "1.0.4", "description": "", - "main": "src/index.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", "type": "module", "scripts": { "build": "tsc", diff --git a/wasmjs/src/index.ts b/wasmjs/src/index.ts index f1f5f23a091..e2dc35023be 100644 --- a/wasmjs/src/index.ts +++ b/wasmjs/src/index.ts @@ -239,6 +239,4 @@ export class WalletCoreWrapper { marshalInput(input: protobuf.Writer): Uint8Array { return input.finish(); } - - } \ No newline at end of file diff --git a/wasmjs/tsconfig.json b/wasmjs/tsconfig.json index 35147c5d66c..796293cc400 100644 --- a/wasmjs/tsconfig.json +++ b/wasmjs/tsconfig.json @@ -1,12 +1,13 @@ { "compilerOptions": { - "target": "ES2020", "module": "ESNext", - "moduleResolution": "Bundler", // or "NodeNext" + "target": "ES2020", + "moduleResolution": "node", + "outDir": "dist", + "declaration": true, "esModuleInterop": true, "strict": true, - "skipLibCheck": true, - "outDir": "dist", + "skipLibCheck": true }, - "include": ["src", "test"] + "include": ["src/**/*"] }