Skip to content

Commit

Permalink
fix: tx signing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Oct 6, 2023
1 parent 04f4480 commit 13a7df5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
5 changes: 3 additions & 2 deletions packages/extension/src/providers/kadena/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -42,7 +43,7 @@ class API implements ProviderAPIInterface {
}

async getBalance(address: string): Promise<string> {
const balance = await this.getBalanceAPI(address);
const balance = await this.getBalanceAPI(this.displayAddress(address));

if (balance.result.error) {
return toBase("0", this.decimals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const kadenaOptions: KadenaNetworkOptions = {
chainId: "1",
},
activityHandler: wrapActivityHandler(kadenaScanActivity),
displayAddress: (address: string) => address.replace("0x", "k:"),
};

const kadenaTestnet = new KadenaNetwork(kadenaOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/extension/src/providers/kadena/networks/kadena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface KadenaNetworkOptions {
prefix: number;
node: string;
kadenaApiOptions: KadenaApiOptions;
displayAddress: (address: string) => string;
coingeckoID?: string;
coingeckoPlatform?: CoingeckoPlatform;
activityHandler: (
Expand All @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions packages/extension/src/providers/kadena/types/kda-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,30 @@ export class KDAToken extends KDABaseToken {
amount: string,
network: KadenaNetwork
): Promise<ICommand> {
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
)})`
)
.addData("ks", {
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();
Expand All @@ -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,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
16 changes: 16 additions & 0 deletions packages/signers/kadena/tests/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

1 comment on commit 13a7df5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.