From e92166d5253954966db7c67fb42dd61a40cb0443 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Mon, 16 Oct 2023 12:05:28 -0300 Subject: [PATCH 01/12] Fix account icons on send and verify screen --- packages/extension/src/providers/kadena/libs/blockies.ts | 6 +++++- .../ui/send-transaction/components/send-address-input.vue | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/extension/src/providers/kadena/libs/blockies.ts b/packages/extension/src/providers/kadena/libs/blockies.ts index 953475487..1540d5751 100644 --- a/packages/extension/src/providers/kadena/libs/blockies.ts +++ b/packages/extension/src/providers/kadena/libs/blockies.ts @@ -97,11 +97,15 @@ type options = { spotcolor?: string; }; +const formatAddress = (address: string): string => { + return address.replace("k:", "0x").toLowerCase(); +}; + const createIcon = (address: string, opts?: options): string => { opts = opts || {}; const size = opts.size || 8; const scale = opts.scale || 4; - const seed = address.toLowerCase(); + const seed = formatAddress(address); seedrand(seed); const color = opts.color || createColor(); const bgcolor = opts.bgcolor || createColor(); diff --git a/packages/extension/src/providers/kadena/ui/send-transaction/components/send-address-input.vue b/packages/extension/src/providers/kadena/ui/send-transaction/components/send-address-input.vue index 6e0a48df6..363727d83 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/components/send-address-input.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/components/send-address-input.vue @@ -84,7 +84,7 @@ const address = computed({ }); const isAddress = computed(() => { - return true; + return !!props.value; }); const changeFocus = (val: FocusEvent) => { From 67bbae6878a73a4542b91a43c78e7d99f763d5e4 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Mon, 16 Oct 2023 15:47:47 -0300 Subject: [PATCH 02/12] Do not accept 0 amount on send; Fix gass fee when sending max amount --- .../kadena/ui/send-transaction/index.vue | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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 4684f01a7..b1987fd30 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -217,6 +217,11 @@ const validateFields = async () => { ) ); + if (rawAmount.lten(0)) { + hasEnough.value = false; + return; + } + const localTransaction = await selectedAsset.value.buildTransaction!( addressTo.value, props.accountInfo.selectedAccount, @@ -229,14 +234,21 @@ const validateFields = async () => { localTransaction ); - const partialFee = transactionResult.gas; - const rawFee = toBN(partialFee?.toString() ?? "0"); + const gasLimit = transactionResult.metaData?.publicMeta?.gasLimit; + const gasPrice = transactionResult.metaData?.publicMeta?.gasPrice; + const gasFee = gasLimit && gasPrice ? gasLimit * gasPrice : 0; + + const rawFee = toBN( + toBase(gasFee.toString(), selectedAsset.value.decimals!) + ); const rawBalance = toBN(selectedAsset.value.balance!); + if ( sendMax.value && selectedAsset.value.name === accountAssets.value[0].name ) { rawAmount = rawAmount.sub(rawFee); + if (rawAmount.gtn(0)) { amount.value = fromBase( rawAmount.toString(), @@ -244,7 +256,8 @@ const validateFields = async () => { ); } } - if (rawAmount.ltn(0) || rawAmount.add(rawFee).gt(rawBalance)) { + + if (rawAmount.add(rawFee).gt(rawBalance)) { hasEnough.value = false; } else { hasEnough.value = true; @@ -252,7 +265,7 @@ const validateFields = async () => { const nativeAsset = accountAssets.value[0]; const txFeeHuman = fromBase( - partialFee?.toString() ?? "", + rawFee?.toString() ?? "", nativeAsset.decimals! ); From af582665e6404b17e14e6d7e1485690ee2d8f607 Mon Sep 17 00:00:00 2001 From: Glaicon Date: Mon, 16 Oct 2023 14:57:05 -0400 Subject: [PATCH 03/12] update address validation --- .../providers/kadena/ui/send-transaction/index.vue | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 4684f01a7..37852b1b9 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -119,6 +119,7 @@ import KadenaAPI from "@/providers/kadena/libs/api"; import getUiPath from "@/libs/utils/get-ui-path"; import { ProviderName } from "@/types/provider"; import Browser from "webextension-polyfill"; +import { computedAsync } from "@vueuse/core"; const props = defineProps({ network: { @@ -385,7 +386,7 @@ const setSendMax = (max: boolean) => { } }; -const isDisabled = computed(() => { +const isDisabled = computedAsync(async () => { let isDisabled = true; let addressIsValid = false; @@ -396,6 +397,15 @@ const isDisabled = computed(() => { addressIsValid = false; } + const to = props.network.displayAddress(addressTo.value); + const accountDetail = await accountAssets.value[0].getAccountDetails( + to, + props.network + ); + if (accountDetail.error) { + addressIsValid = false; + } + if ( amount.value !== undefined && amount.value !== "" && From da950331c0bbb500b02d06c281146c1c752701b3 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Mon, 16 Oct 2023 16:54:29 -0300 Subject: [PATCH 04/12] Fix amount calculation when set max is selected --- .../src/providers/kadena/ui/send-transaction/index.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a0f726233..3c1832d35 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -248,7 +248,7 @@ const validateFields = async () => { sendMax.value && selectedAsset.value.name === accountAssets.value[0].name ) { - rawAmount = rawAmount.sub(rawFee); + rawAmount = rawBalance.sub(rawFee); if (rawAmount.gtn(0)) { amount.value = fromBase( From 48e2277b1256119789d652b8101904518cff56e9 Mon Sep 17 00:00:00 2001 From: Glaicon Date: Mon, 16 Oct 2023 17:06:15 -0400 Subject: [PATCH 05/12] WIP - Fix send transaction page --- .../kadena/ui/send-transaction/index.vue | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) 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 3c1832d35..2246b20a7 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -119,7 +119,6 @@ import KadenaAPI from "@/providers/kadena/libs/api"; import getUiPath from "@/libs/utils/get-ui-path"; import { ProviderName } from "@/types/provider"; import Browser from "webextension-polyfill"; -import { computedAsync } from "@vueuse/core"; const props = defineProps({ network: { @@ -158,6 +157,7 @@ const selectedAsset = ref>( ); const hasEnough = ref(false); const sendMax = ref(false); +const addressToIsValid = ref(false); const selected: string = route.params.id as string; const isLoadingAssets = ref(true); @@ -278,6 +278,19 @@ const validateFields = async () => { nativeSymbol: nativeAsset.symbol ?? "", nativeValue: txFeeHuman.toString(), }; + + if (addressTo.value.startsWith("k:") && addressTo.value.length == 66) { + addressToIsValid.value = true; + } else { + const to = props.network.displayAddress(addressTo.value); + const accountDetail = await accountAssets.value[0].getAccountDetails( + to, + props.network + ); + if (accountDetail.error) { + addressToIsValid.value = false; + } + } } }; watch([selectedAsset, addressTo], validateFields); @@ -399,7 +412,7 @@ const setSendMax = (max: boolean) => { } }; -const isDisabled = computedAsync(async () => { +const isDisabled = computed(() => { let isDisabled = true; let addressIsValid = false; @@ -410,20 +423,12 @@ const isDisabled = computedAsync(async () => { addressIsValid = false; } - const to = props.network.displayAddress(addressTo.value); - const accountDetail = await accountAssets.value[0].getAccountDetails( - to, - props.network - ); - if (accountDetail.error) { - addressIsValid = false; - } - if ( amount.value !== undefined && amount.value !== "" && hasEnough.value && addressIsValid && + addressToIsValid.value && !edWarn.value && edWarn.value !== undefined ) From 3ce68418d26bbcaa18066c82456cc0e9758a5ff1 Mon Sep 17 00:00:00 2001 From: Glaicon Date: Tue, 17 Oct 2023 08:48:06 -0400 Subject: [PATCH 06/12] update send transaction page --- .../src/providers/kadena/ui/send-transaction/index.vue | 2 ++ 1 file changed, 2 insertions(+) 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 2246b20a7..9ad7ce573 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -289,6 +289,8 @@ const validateFields = async () => { ); if (accountDetail.error) { addressToIsValid.value = false; + } else { + addressToIsValid.value = true; } } } From fcd25d1cef4940f6e9aaf990c7264595c452e071 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Tue, 17 Oct 2023 09:52:33 -0300 Subject: [PATCH 07/12] Disable import account for Kadena networks --- .../src/providers/kadena/types/kadena-network.ts | 1 + packages/extension/src/types/base-network.ts | 3 +++ packages/extension/src/ui/action/views/accounts/index.vue | 8 ++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/extension/src/providers/kadena/types/kadena-network.ts b/packages/extension/src/providers/kadena/types/kadena-network.ts index 45d9ba644..cc31f9a8b 100644 --- a/packages/extension/src/providers/kadena/types/kadena-network.ts +++ b/packages/extension/src/providers/kadena/types/kadena-network.ts @@ -61,6 +61,7 @@ export class KadenaNetwork extends BaseNetwork { signer: [SignerType.ed25519kda], provider: ProviderName.kadena, api, + importAccount: false, ...options, }; diff --git a/packages/extension/src/types/base-network.ts b/packages/extension/src/types/base-network.ts index dfcc2efe3..fc9ad4cc8 100644 --- a/packages/extension/src/types/base-network.ts +++ b/packages/extension/src/types/base-network.ts @@ -33,6 +33,7 @@ export interface BaseNetworkOptions { | Promise | Promise; customTokens?: boolean; + importAccount?: boolean; } export abstract class BaseNetwork { @@ -60,6 +61,7 @@ export abstract class BaseNetwork { | Promise | Promise; public customTokens: boolean; + public importAccount: boolean; constructor(options: BaseNetworkOptions) { this.name = options.name; @@ -80,6 +82,7 @@ export abstract class BaseNetwork { this.decimals = options.decimals; this.api = options.api; this.customTokens = options.customTokens ?? false; + this.importAccount = options.importAccount ?? true; this.coingeckoPlatform = options.coingeckoPlatform; this.currencyNameLong = options.currencyNameLong; } diff --git a/packages/extension/src/ui/action/views/accounts/index.vue b/packages/extension/src/ui/action/views/accounts/index.vue index bc782856f..0e045ed95 100644 --- a/packages/extension/src/ui/action/views/accounts/index.vue +++ b/packages/extension/src/ui/action/views/accounts/index.vue @@ -60,7 +60,11 @@ Add hardware wallet account - + Import account from another wallet @@ -91,7 +95,7 @@ /> Date: Tue, 17 Oct 2023 10:03:14 -0300 Subject: [PATCH 08/12] Fix address validation when account starts with k: --- .../src/providers/kadena/ui/send-transaction/index.vue | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 9ad7ce573..5bb6495c3 100644 --- a/packages/extension/src/providers/kadena/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/kadena/ui/send-transaction/index.vue @@ -279,10 +279,11 @@ const validateFields = async () => { nativeValue: txFeeHuman.toString(), }; - if (addressTo.value.startsWith("k:") && addressTo.value.length == 66) { + const to = props.network.displayAddress(addressTo.value); + + if (to.startsWith("k:") && to.length == 66) { addressToIsValid.value = true; } else { - const to = props.network.displayAddress(addressTo.value); const accountDetail = await accountAssets.value[0].getAccountDetails( to, props.network From ab75906a3fc271e763ab6292798b3a42c86030a5 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Wed, 18 Oct 2023 09:26:26 -0300 Subject: [PATCH 09/12] Fixing base path and changing unit tests --- .../providers/kadena/types/kadena-network.ts | 2 +- packages/signers/kadena/src/libs/ed25519.ts | 6 ++--- .../signers/kadena/tests/generate.test.ts | 24 ++++++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/extension/src/providers/kadena/types/kadena-network.ts b/packages/extension/src/providers/kadena/types/kadena-network.ts index cc31f9a8b..343ed27dc 100644 --- a/packages/extension/src/providers/kadena/types/kadena-network.ts +++ b/packages/extension/src/providers/kadena/types/kadena-network.ts @@ -56,7 +56,7 @@ export class KadenaNetwork extends BaseNetwork { }; const baseOptions: BaseNetworkOptions = { - basePath: "m/44'/626'/0'", + basePath: "m/44'/626'/0'/0", identicon: createIcon, signer: [SignerType.ed25519kda], provider: ProviderName.kadena, diff --git a/packages/signers/kadena/src/libs/ed25519.ts b/packages/signers/kadena/src/libs/ed25519.ts index 8670c82c4..8fe08db13 100644 --- a/packages/signers/kadena/src/libs/ed25519.ts +++ b/packages/signers/kadena/src/libs/ed25519.ts @@ -58,9 +58,9 @@ const isValidPath = (path: string): boolean => { }; const derivePath = (path: Path, seed: Hex, offset = HARDENED_OFFSET): Keys => { - if (!isValidPath(path)) { - throw new Error("Invalid derivation path"); - } + // if (!isValidPath(path)) { + // throw new Error("Invalid derivation path"); + // } const { key, chainCode } = getMasterKeyFromSeed(seed); const segments = path diff --git a/packages/signers/kadena/tests/generate.test.ts b/packages/signers/kadena/tests/generate.test.ts index 0402dc199..50be6d4df 100644 --- a/packages/signers/kadena/tests/generate.test.ts +++ b/packages/signers/kadena/tests/generate.test.ts @@ -3,34 +3,40 @@ import Signer from "../src"; describe("Kadena address generate", () => { const MNEMONIC = - "clip coffee brain token leader kiss around main finger network avoid west"; + "favorite service senior cluster chicken shift square endorse casual kidney doll exhibit"; - it("should generate Kadena addresses correctly", async () => { + it.only("should generate Kadena addresses correctly", async () => { // Arrange const kadenaSigner = new Signer(); // Act & Assert let keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0"); expect(keypair.address).equals( - "0xe84affbb41a62d74020bc4841ea206aba7734f9e0d30fb688a4a84fe2d30e291" + "0x7a3931f9379b5b63c76d73e3d79efbc890f56cfb22f1505ad457b7d0fb025f6d" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/1"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/0"); expect(keypair.address).equals( - "0x2ddd388820dfd8ddafa37a69926e0b5e57d29daa3adab2ede8a390f984038283" + "0x46e43dc208df46dfcc0662777f23dd8853079e3e0e8e2a31abc816cfbf55ea55" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/2"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/1"); expect(keypair.address).equals( - "0x16dcf13ed0261406c63561ef32853e4b417fe1c197a164b6385718c596261918" + "0x07a9263d75d8debb5dcaebf679d09d1291111ebdc4eaaee73992dc0401e10e04" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/3"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/2"); expect(keypair.address).equals( - "0xaf29ee6f585381b0109eef8ba51f55fbe9cca31c83375efdb8d72588be3995fc" + "0xc1937f9eca305cdd048ad5785aaf1ad168b04d565250f6cd6e550c6d957e00b3" + ); + + // Act & Assert + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/3"); + expect(keypair.address).equals( + "0x0d7f186afcd0759cd509de3741d609d3465aa65d74d0393410ad4a137252295d" ); }); }); From 45c1cbe2facc2d13e6abf77e0002036ed538c704 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Wed, 18 Oct 2023 16:19:09 -0300 Subject: [PATCH 10/12] Changing Kadena derivation path to a valid ED25519 value --- .../src/providers/kadena/types/kadena-network.ts | 2 +- packages/keyring/tests/generate.test.ts | 4 ++-- packages/signers/kadena/src/libs/ed25519.ts | 6 +++--- packages/signers/kadena/tests/generate.test.ts | 10 +++++----- packages/signers/kadena/tests/sign.test.ts | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/extension/src/providers/kadena/types/kadena-network.ts b/packages/extension/src/providers/kadena/types/kadena-network.ts index 343ed27dc..cabdf4eb9 100644 --- a/packages/extension/src/providers/kadena/types/kadena-network.ts +++ b/packages/extension/src/providers/kadena/types/kadena-network.ts @@ -56,7 +56,7 @@ export class KadenaNetwork extends BaseNetwork { }; const baseOptions: BaseNetworkOptions = { - basePath: "m/44'/626'/0'/0", + basePath: "m/44'/626'/0'/0'", identicon: createIcon, signer: [SignerType.ed25519kda], provider: ProviderName.kadena, diff --git a/packages/keyring/tests/generate.test.ts b/packages/keyring/tests/generate.test.ts index 8624569ab..3862fd1ee 100644 --- a/packages/keyring/tests/generate.test.ts +++ b/packages/keyring/tests/generate.test.ts @@ -16,7 +16,7 @@ describe("Keyring create tests", () => { const keyring = new KeyRing(storage); await keyring.init(password, { mnemonic: MNEMONIC }); const keyAdd: KeyRecordAdd = { - basePath: "m/44'/626'/0'", + basePath: "m/44'/626'/0'/0'", signerType: SignerType.ed25519kda, name: "0index", walletType: WalletType.mnemonic, @@ -26,7 +26,7 @@ describe("Keyring create tests", () => { expect(pair.signerType).equals(SignerType.ed25519kda); expect(pair.pathIndex).equals(0); expect(pair.address).equals( - "0x3379098c10716e2ba981a65129e0e7c4b7f11d944412d3ab1001f49114f9d24d" + "0xd75b971cc4846f543caa92e9bf07bf2f4cee87774f4ba8db2fde5c3dacaed03f" ); keyring.lock(); }).timeout(20000); diff --git a/packages/signers/kadena/src/libs/ed25519.ts b/packages/signers/kadena/src/libs/ed25519.ts index 8fe08db13..8670c82c4 100644 --- a/packages/signers/kadena/src/libs/ed25519.ts +++ b/packages/signers/kadena/src/libs/ed25519.ts @@ -58,9 +58,9 @@ const isValidPath = (path: string): boolean => { }; const derivePath = (path: Path, seed: Hex, offset = HARDENED_OFFSET): Keys => { - // if (!isValidPath(path)) { - // throw new Error("Invalid derivation path"); - // } + if (!isValidPath(path)) { + throw new Error("Invalid derivation path"); + } const { key, chainCode } = getMasterKeyFromSeed(seed); const segments = path diff --git a/packages/signers/kadena/tests/generate.test.ts b/packages/signers/kadena/tests/generate.test.ts index 50be6d4df..5f14675e1 100644 --- a/packages/signers/kadena/tests/generate.test.ts +++ b/packages/signers/kadena/tests/generate.test.ts @@ -5,7 +5,7 @@ describe("Kadena address generate", () => { const MNEMONIC = "favorite service senior cluster chicken shift square endorse casual kidney doll exhibit"; - it.only("should generate Kadena addresses correctly", async () => { + it("should generate Kadena addresses correctly", async () => { // Arrange const kadenaSigner = new Signer(); @@ -16,25 +16,25 @@ describe("Kadena address generate", () => { ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/0"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/0"); expect(keypair.address).equals( "0x46e43dc208df46dfcc0662777f23dd8853079e3e0e8e2a31abc816cfbf55ea55" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/1"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/1"); expect(keypair.address).equals( "0x07a9263d75d8debb5dcaebf679d09d1291111ebdc4eaaee73992dc0401e10e04" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/2"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/2"); expect(keypair.address).equals( "0xc1937f9eca305cdd048ad5785aaf1ad168b04d565250f6cd6e550c6d957e00b3" ); // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0/3"); + keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/3"); expect(keypair.address).equals( "0x0d7f186afcd0759cd509de3741d609d3465aa65d74d0393410ad4a137252295d" ); diff --git a/packages/signers/kadena/tests/sign.test.ts b/packages/signers/kadena/tests/sign.test.ts index 0567fe8c6..a1b011946 100644 --- a/packages/signers/kadena/tests/sign.test.ts +++ b/packages/signers/kadena/tests/sign.test.ts @@ -11,17 +11,17 @@ describe("Kadena signing", () => { "Everything should be made as simple as possible, but not simpler."; const msgHash = bufferToHex(blake2AsU8a(msg)); const signature = - "0xed96b2e3e21e021f3b3e0b39b93585705dbbc53a9cf940365f2ea61f71bdd8a68a3272bfc6e79d5f5b89cc32d85a9aba01ce04173038ede70c8d8da8f7cb4506"; + "0x5c23909b5b05854e79410777abd0ce3145c47defb5e7a32a887c617a28cb63c52ad22ecbc72dc87118eb7854fe3115fe1b47490364b2a240ce400cdc516ff70e"; 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"; + "0xef8e48c609cd3f47852265785cc024900ee057eb766384292799f0cda38bf07839cff8aacd79581958f721098edb278ef92d112cdf1b623c69fde55b853af602"; it("it should sign correctly", async () => { // Arrange const kadenaSigner = new Signer(); - const keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/1"); + const keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/1"); // Act const signResult = await kadenaSigner.sign(msgHash, keypair); @@ -33,7 +33,7 @@ describe("Kadena signing", () => { 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 keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/0"); const txMsgHash = bufferToHex(blake2AsU8a(txMsg)); // Act const signResult = await kadenaSigner.sign(txMsgHash, keypair); @@ -44,7 +44,7 @@ describe("Kadena signing", () => { it("it should verify correctly", async () => { // Arrange const kadenaSigner = new Signer(); - const keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/1"); + const keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/1"); // Act const verifyResult = await kadenaSigner.verify( From 9dd90fc778299dea4f16326ec7fdb8b53a103b77 Mon Sep 17 00:00:00 2001 From: Jonathan Bertoldi Date: Wed, 18 Oct 2023 17:17:25 -0300 Subject: [PATCH 11/12] fix derivation path indexing for kadena signer new path. --- packages/signers/kadena/src/index.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/signers/kadena/src/index.ts b/packages/signers/kadena/src/index.ts index f0ceb0db4..cc101b4ab 100644 --- a/packages/signers/kadena/src/index.ts +++ b/packages/signers/kadena/src/index.ts @@ -1,19 +1,15 @@ -import { SignerInterface, KeyPair } from "@enkryptcom/types"; +import { KeyPair, SignerInterface } from "@enkryptcom/types"; +import { bufferToHex, hexToBuffer } from "@enkryptcom/utils"; import { mnemonicToSeedSync } from "bip39"; import { sign as tweetSign } from "tweetnacl"; -import { bufferToHex, hexToBuffer } from "@enkryptcom/utils"; import { derivePath } from "./libs/ed25519"; class Signer implements SignerInterface { async generate(mnemonic: string, derivationPath = ""): Promise { const seed = bufferToHex(mnemonicToSeedSync(mnemonic), true); - const dPathSegments = derivationPath.split("/"); - const indexVal = Number(dPathSegments.pop()); - const keys = derivePath( - dPathSegments.join("/"), - seed, - 0x80000000 + indexVal - ); + const dPathSegments = `${derivationPath}'`.split("/"); + + const keys = derivePath(dPathSegments.join("/"), seed); const keyPair = tweetSign.keyPair.fromSeed(keys.key); return { address: bufferToHex(keyPair.publicKey), From 7ae1a68d26d7c6bfd0833f61ea0cd7381e55ce69 Mon Sep 17 00:00:00 2001 From: Andre Borges Medeiros Date: Wed, 18 Oct 2023 17:50:19 -0300 Subject: [PATCH 12/12] Fixing unit tests for new derivation path indexing --- packages/keyring/tests/generate.test.ts | 4 ++-- packages/signers/kadena/tests/generate.test.ts | 16 +++++----------- packages/signers/kadena/tests/sign.test.ts | 4 ++-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/keyring/tests/generate.test.ts b/packages/keyring/tests/generate.test.ts index 3862fd1ee..adc031aca 100644 --- a/packages/keyring/tests/generate.test.ts +++ b/packages/keyring/tests/generate.test.ts @@ -16,7 +16,7 @@ describe("Keyring create tests", () => { const keyring = new KeyRing(storage); await keyring.init(password, { mnemonic: MNEMONIC }); const keyAdd: KeyRecordAdd = { - basePath: "m/44'/626'/0'/0'", + basePath: "m/44'/626'/0'/0'/0'", signerType: SignerType.ed25519kda, name: "0index", walletType: WalletType.mnemonic, @@ -26,7 +26,7 @@ describe("Keyring create tests", () => { expect(pair.signerType).equals(SignerType.ed25519kda); expect(pair.pathIndex).equals(0); expect(pair.address).equals( - "0xd75b971cc4846f543caa92e9bf07bf2f4cee87774f4ba8db2fde5c3dacaed03f" + "0x7359492db65e4e6487134cd68d5620e011965ce50c84f38e613c7bdb47c2bfa3" ); keyring.lock(); }).timeout(20000); diff --git a/packages/signers/kadena/tests/generate.test.ts b/packages/signers/kadena/tests/generate.test.ts index 5f14675e1..62ddbbb72 100644 --- a/packages/signers/kadena/tests/generate.test.ts +++ b/packages/signers/kadena/tests/generate.test.ts @@ -10,33 +10,27 @@ describe("Kadena address generate", () => { const kadenaSigner = new Signer(); // Act & Assert - let keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0"); + let keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/0"); expect(keypair.address).equals( - "0x7a3931f9379b5b63c76d73e3d79efbc890f56cfb22f1505ad457b7d0fb025f6d" - ); - - // Act & Assert - keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/0"); - expect(keypair.address).equals( - "0x46e43dc208df46dfcc0662777f23dd8853079e3e0e8e2a31abc816cfbf55ea55" + "0x40a9305bd53a921c44cf19dc9bac4e5d73465fc6a46343ab313defe6b0bfb0a3" ); // Act & Assert keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/1"); expect(keypair.address).equals( - "0x07a9263d75d8debb5dcaebf679d09d1291111ebdc4eaaee73992dc0401e10e04" + "0x50d824cb62578b1fcf8e4afb122e98884d5f04070950b93a102e1ba1e1f3d1bb" ); // Act & Assert keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/2"); expect(keypair.address).equals( - "0xc1937f9eca305cdd048ad5785aaf1ad168b04d565250f6cd6e550c6d957e00b3" + "0x287b9cdbd0894fbff67c664b4cc1e7da9eca9b03ef94fd5baa2cfabe2cd3c6a5" ); // Act & Assert keypair = await kadenaSigner.generate(MNEMONIC, "m/44'/626'/0'/0'/3"); expect(keypair.address).equals( - "0x0d7f186afcd0759cd509de3741d609d3465aa65d74d0393410ad4a137252295d" + "0xe00a31c57aabe95554ad71c700c723153c0dd67b072b569916209138e419c122" ); }); }); diff --git a/packages/signers/kadena/tests/sign.test.ts b/packages/signers/kadena/tests/sign.test.ts index a1b011946..ddf8920e4 100644 --- a/packages/signers/kadena/tests/sign.test.ts +++ b/packages/signers/kadena/tests/sign.test.ts @@ -11,13 +11,13 @@ describe("Kadena signing", () => { "Everything should be made as simple as possible, but not simpler."; const msgHash = bufferToHex(blake2AsU8a(msg)); const signature = - "0x5c23909b5b05854e79410777abd0ce3145c47defb5e7a32a887c617a28cb63c52ad22ecbc72dc87118eb7854fe3115fe1b47490364b2a240ce400cdc516ff70e"; + "0x8fd01eccc203d17cbffe54393954c213fb087dd6e62cf3c50bc5635346a83d9fae80c214ecb20bab092a0eca10408223e6e1007f597a3d4bfb525d68a0573a05"; 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 = - "0xef8e48c609cd3f47852265785cc024900ee057eb766384292799f0cda38bf07839cff8aacd79581958f721098edb278ef92d112cdf1b623c69fde55b853af602"; + "0xe929eecf16d77016646a95448fd24de3183488a5e4ab7ae0b1fcb5971fd6e3a524a6cc2879b2369e40f827b816355dbaad09b173442742c39ba487c68199a302"; it("it should sign correctly", async () => { // Arrange const kadenaSigner = new Signer();