From 13a7df5b5516cdedacc29231dbfc556318edcf48 Mon Sep 17 00:00:00 2001 From: kvhnuke <10602065+kvhnuke@users.noreply.github.com> Date: Fri, 6 Oct 2023 15:51:50 -0700 Subject: [PATCH] fix: tx signing issue --- .../extension/src/providers/kadena/libs/api.ts | 5 +++-- .../providers/kadena/networks/kadena-testnet.ts | 1 + .../src/providers/kadena/networks/kadena.ts | 1 + .../src/providers/kadena/types/kadena-network.ts | 2 +- .../src/providers/kadena/types/kda-token.ts | 13 +++++++------ .../kadena/ui/send-transaction/index.vue | 4 ++-- packages/signers/kadena/tests/sign.test.ts | 16 ++++++++++++++++ 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/extension/src/providers/kadena/libs/api.ts b/packages/extension/src/providers/kadena/libs/api.ts index 59cae4244..32b4407d4 100644 --- a/packages/extension/src/providers/kadena/libs/api.ts +++ b/packages/extension/src/providers/kadena/libs/api.ts @@ -15,14 +15,15 @@ class API implements ProviderAPIInterface { networkId: string; chainId: string; apiHost: string; + displayAddress: (address: string) => string; constructor(node: string, options: KadenaNetworkOptions) { this.decimals = options.decimals; - this.node = node; this.networkId = options.kadenaApiOptions.networkId; this.chainId = options.kadenaApiOptions.chainId; this.apiHost = `${node}/${this.networkId}/chain/${this.chainId}/pact`; + this.displayAddress = options.displayAddress; } public get api() { @@ -42,7 +43,7 @@ class API implements ProviderAPIInterface { } async getBalance(address: string): Promise { - const balance = await this.getBalanceAPI(address); + const balance = await this.getBalanceAPI(this.displayAddress(address)); if (balance.result.error) { return toBase("0", this.decimals); diff --git a/packages/extension/src/providers/kadena/networks/kadena-testnet.ts b/packages/extension/src/providers/kadena/networks/kadena-testnet.ts index e421fd91e..bfe2dd411 100644 --- a/packages/extension/src/providers/kadena/networks/kadena-testnet.ts +++ b/packages/extension/src/providers/kadena/networks/kadena-testnet.ts @@ -22,6 +22,7 @@ const kadenaOptions: KadenaNetworkOptions = { chainId: "1", }, activityHandler: wrapActivityHandler(kadenaScanActivity), + displayAddress: (address: string) => address.replace("0x", "k:"), }; const kadenaTestnet = new KadenaNetwork(kadenaOptions); diff --git a/packages/extension/src/providers/kadena/networks/kadena.ts b/packages/extension/src/providers/kadena/networks/kadena.ts index 7a4f4ade9..bb8637b5e 100644 --- a/packages/extension/src/providers/kadena/networks/kadena.ts +++ b/packages/extension/src/providers/kadena/networks/kadena.ts @@ -24,6 +24,7 @@ const kadenaOptions: KadenaNetworkOptions = { coingeckoID: "kadena", coingeckoPlatform: CoingeckoPlatform.Kadena, activityHandler: wrapActivityHandler(kadenaScanActivity), + displayAddress: (address: string) => address.replace("0x", "k:"), }; const kadena = new KadenaNetwork(kadenaOptions); diff --git a/packages/extension/src/providers/kadena/types/kadena-network.ts b/packages/extension/src/providers/kadena/types/kadena-network.ts index 29abbda8c..45d9ba644 100644 --- a/packages/extension/src/providers/kadena/types/kadena-network.ts +++ b/packages/extension/src/providers/kadena/types/kadena-network.ts @@ -32,6 +32,7 @@ export interface KadenaNetworkOptions { prefix: number; node: string; kadenaApiOptions: KadenaApiOptions; + displayAddress: (address: string) => string; coingeckoID?: string; coingeckoPlatform?: CoingeckoPlatform; activityHandler: ( @@ -58,7 +59,6 @@ export class KadenaNetwork extends BaseNetwork { basePath: "m/44'/626'/0'", identicon: createIcon, signer: [SignerType.ed25519kda], - displayAddress: (address: string) => address.replace("0x", "k:"), provider: ProviderName.kadena, api, ...options, diff --git a/packages/extension/src/providers/kadena/types/kda-token.ts b/packages/extension/src/providers/kadena/types/kda-token.ts index de7d49f57..06195be90 100644 --- a/packages/extension/src/providers/kadena/types/kda-token.ts +++ b/packages/extension/src/providers/kadena/types/kda-token.ts @@ -43,13 +43,14 @@ export class KDAToken extends KDABaseToken { amount: string, network: KadenaNetwork ): Promise { + to = network.displayAddress(to); const accountDetails = await this.getAccountDetails(to, network); const keySetAccount = to.startsWith("k:") ? to.replace("k:", "") : to; const unsignedTransaction = Pact.builder .execution( - `(coin.transfer-create "${ + `(coin.transfer-create "${network.displayAddress( from.address - }" "${to}" (read-keyset "ks") ${parseFloat(amount).toFixed( + )}" "${to}" (read-keyset "ks") ${parseFloat(amount).toFixed( network.options.decimals )})` ) @@ -57,15 +58,15 @@ export class KDAToken extends KDABaseToken { keys: accountDetails.data?.guard.keys || [keySetAccount], pred: accountDetails.data?.guard.pred || "keys-all", }) - .addSigner(from.publicKey, (withCap: any) => [ - withCap("coin.TRANSFER", from.address, to, { + .addSigner(from.publicKey.replace("0x", ""), (withCap: any) => [ + withCap("coin.TRANSFER", network.displayAddress(from.address), to, { decimal: amount, }), withCap("coin.GAS"), ]) .setMeta({ chainId: network.options.kadenaApiOptions.chainId as ChainId, - senderAccount: from.address, + senderAccount: network.displayAddress(from.address), }) .setNetworkId(network.options.kadenaApiOptions.networkId) .createTransaction(); @@ -79,7 +80,7 @@ export class KDAToken extends KDABaseToken { else return { id: 0, - signature: res.result as string, + signature: res.result?.replace("0x", "") as string, }; }); diff --git a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue index 5155eb9b7..4684f01a7 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -293,8 +293,8 @@ const fetchTokens = async () => { nonZeroAssets.push(networkAssets[0]); } - selectedAsset.value = nonZeroAssets[0]; - accountAssets.value = nonZeroAssets; + selectedAsset.value = nonZeroAssets[0] as KDAToken; + accountAssets.value = nonZeroAssets as KDAToken[]; isLoadingAssets.value = false; }); diff --git a/packages/signers/kadena/tests/sign.test.ts b/packages/signers/kadena/tests/sign.test.ts index ebd38ba47..0567fe8c6 100644 --- a/packages/signers/kadena/tests/sign.test.ts +++ b/packages/signers/kadena/tests/sign.test.ts @@ -13,6 +13,11 @@ describe("Kadena signing", () => { const signature = "0xed96b2e3e21e021f3b3e0b39b93585705dbbc53a9cf940365f2ea61f71bdd8a68a3272bfc6e79d5f5b89cc32d85a9aba01ce04173038ede70c8d8da8f7cb4506"; + const txMsg = + '{"payload":{"exec":{"code":"(coin.transfer-create \\"k:e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291\\" \\"k:e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291\\" (read-keyset \\"ks\\") 0.000000000000)","data":{"ks":{"keys":["e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291"],"pred":"keys-all"}}}},"nonce":"kjs:nonce:1696630965601","signers":[{"pubKey":"e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291","scheme":"ED25519","clist":[{"name":"coin.TRANSFER","args":["k:e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291","k:e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291",{"decimal":"0"}]},{"name":"coin.GAS","args":[]}]}],"meta":{"gasLimit":2500,"gasPrice":1e-8,"sender":"k:e84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291","ttl":28800,"creationTime":1696630965,"chainId":"1"},"networkId":"testnet04"}'; + + const txMsgSig = + "0x8ee1c3cd94602b96a8ee59488c7ee7b0ff464b169513bd42872423219fa291c093bd791f62a11dd7aa00dfd3fa8e4b613e364ea9e4de4afe3d84ce3556673102"; it("it should sign correctly", async () => { // Arrange const kadenaSigner = new Signer(); @@ -25,6 +30,17 @@ describe("Kadena signing", () => { expect(signResult).equals(signature); }); + it("it should sign tx msgs correctly", async () => { + // Arrange + const kadenaSigner = new Signer(); + const keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0"); + const txMsgHash = bufferToHex(blake2AsU8a(txMsg)); + // Act + const signResult = await kadenaSigner.sign(txMsgHash, keypair); + // Assert + expect(signResult).equals(txMsgSig); + }); + it("it should verify correctly", async () => { // Arrange const kadenaSigner = new Signer();