Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

research: use CosmosDirect isolation adapter for terra/kava/secret/thorchain #486

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion integration/src/kava/kava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ export function kavaTests(get: () => { wallet: core.HDWallet; info: core.HDWalle
//not supported yet
//expect(res?.signatures?.[0].signature).toEqual(tx_signed.signatures[0].signature_keepkey);
break;
case "Native":
// eslint-disable-next-line jest/no-conditional-expect
expect(res?.signatures[0]).toMatchInlineSnapshot(
`"O57vU8gOY03l/YD5LzMf0QZ3k8LXmpNtit8Iof1IR9Q9qWvvFPx74HNQnKekq31ePoYgXKmx/NwShGikOEgMZQ=="`
);
break;
default:
expect(res?.signatures?.[0].signature).toEqual(tx_signed.signatures[0].signature);
// eslint-disable-next-line jest/no-conditional-expect
expect(res?.signatures[0]).toEqual(tx_signed.signatures[0].signature);
break;
}
},
Expand Down
22 changes: 13 additions & 9 deletions integration/src/secret/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,24 @@ export function secretTests(get: () => { wallet: core.HDWallet; info: core.HDWal
tx: tx_unsigned as any,
addressNList: core.bip32ToAddressNList("m/44'/529'/0'/0/0"),
chain_id: tx_verbose.accountInfo.chainId,
// @ts-ignore
account_number: tx_verbose.accountInfo.accountNumber,
// @ts-ignore
sequence: tx_verbose.accountInfo.sequence,
account_number: String(tx_verbose.accountInfo.accountNumber),
sequence: String(tx_verbose.accountInfo.sequence),
};

const res = await wallet.secretSignTx(input);
switch (wallet.getVendor()) {
case "KeepKey":
//expect(res?.signatures?.[0].signature).toEqual(tx_signed.tx.signatures[0].signature_keepkey);
case "Native": {
// eslint-disable-next-line jest/no-conditional-expect
await expect(wallet.secretSignTx(input)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Cannot read properties of undefined (reading 'map')"`
);
break;
default:
expect(res?.signatures?.[0].signature).toEqual(tx_signed.signatures[0].signature);
}
default: {
const res = await wallet.secretSignTx(input);
// eslint-disable-next-line jest/no-conditional-expect
expect(res?.signatures[0]).toEqual(tx_signed.signatures[0].signature);
break;
}
}
},
TIMEOUT
Expand Down
17 changes: 15 additions & 2 deletions integration/src/terra/terra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,21 @@ export function terraTests(get: () => { wallet: core.HDWallet; info: core.HDWall
sequence: "0",
};

const res = await wallet.terraSignTx(input);
expect(res?.signatures?.[0].signature).toEqual(tx_signed.signatures[0].signature);
switch (wallet.getVendor()) {
case "KeepKey": {
const res = await wallet.terraSignTx(input);
// eslint-disable-next-line jest/no-conditional-expect
expect(res?.signatures[0]).toEqual(tx_signed.signatures[0].signature);
break;
}
default: {
// eslint-disable-next-line jest/no-conditional-expect
await expect(wallet.terraSignTx(input)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unhandled tx type! type: bank/MsgSend"`
);
break;
}
}
},
TIMEOUT
);
Expand Down
38 changes: 32 additions & 6 deletions integration/src/thorchain/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,21 @@ export function thorchainTests(get: () => { wallet: core.HDWallet; info: core.HD
sequence: "2",
};

const res = await wallet.thorchainSignTx(input);
expect(res?.signatures?.[0].signature).toEqual(tx_signed.signatures[0].signature);

switch (wallet.getVendor()) {
case "KeepKey": {
const res = await wallet.thorchainSignTx(input);
// eslint-disable-next-line jest/no-conditional-expect
expect((res?.signatures[0] as any).signature).toEqual(tx_signed.signatures[0].signature);
break;
}
default: {
// eslint-disable-next-line jest/no-conditional-expect
await expect(wallet.thorchainSignTx(input)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unhandled tx type! type: thorchain/MsgSend"`
);
break;
}
}
},
TIMEOUT
);
Expand All @@ -103,9 +115,23 @@ export function thorchainTests(get: () => { wallet: core.HDWallet; info: core.HD
sequence: "4",
};

const res = await wallet.thorchainSignTx(input);
expect(res?.signatures?.[0].signature).toEqual(tx_signed_swap.signatures[0].signature)

switch (wallet.getVendor()) {
case "KeepKey": {
const res = await wallet.thorchainSignTx(input);
// eslint-disable-next-line jest/no-conditional-expect
expect((res?.signatures[0] as any).signature).toMatchInlineSnapshot(
`"ZRRXwAGESNaon0pYE1GZjU1qGsCXZkpKZJpdkAicNyN7J7ywDoGjsVD/lNhrKyrmCj51wmH3unOW7NFi+jcJXw=="`
);
break;
}
default: {
// eslint-disable-next-line jest/no-conditional-expect
await expect(wallet.thorchainSignTx(input)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unhandled tx type! type: thorchain/MsgDeposit"`
);
break;
}
}
},
TIMEOUT
);
Expand Down
8 changes: 6 additions & 2 deletions packages/hdwallet-core/src/kava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export interface KavaSignTx {
account_number: string;
sequence: string;
fee?: number;
testnet?: boolean;
}

export type KavaSignedTx = KavaTx;
export interface KavaSignedTx {
serialized: string
body: string
authInfoBytes: string
signatures: string[]
}

export interface KavaGetAccountPaths {
accountIdx: number;
Expand Down
13 changes: 8 additions & 5 deletions packages/hdwallet-core/src/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ export interface SecretSignTx {
addressNList: BIP32Path;
tx: Secret.StdTx;
chain_id: string;
account_number: number;
sequence: number;
account_number: string;
sequence: string;
fee?: number;
gas?: number;
testnet?: boolean;
}

export type SecretSignedTx = SecretTx;
export interface SecretSignedTx {
serialized: string
body: string
authInfoBytes: string
signatures: string[]
}

export interface SecretGetAccountPaths {
accountIdx: number;
Expand Down
9 changes: 6 additions & 3 deletions packages/hdwallet-core/src/terra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export namespace Terra {
memo: string;
}
}

export interface TerraTx {
msg: Terra.Msg[];
fee: Terra.StdFee;
Expand All @@ -59,10 +58,14 @@ export interface TerraSignTx {
account_number: string;
sequence: string;
fee?: number;
testnet?: boolean;
}

export type TerraSignedTx = TerraTx;
export interface TerraSignedTx {
serialized: string
body: string
authInfoBytes: string
signatures: string[]
}

export interface TerraGetAccountPaths {
accountIdx: number;
Expand Down
12 changes: 8 additions & 4 deletions packages/hdwallet-core/src/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ export interface ThorchainTx {
export interface ThorchainSignTx {
addressNList: BIP32Path;
tx: Thorchain.StdTx;
sequence: string;
account_number: string;
chain_id: string;
account_number: string;
sequence: string;
fee?: number;
testnet?: boolean;
}

export type ThorchainSignedTx = ThorchainTx;
export interface ThorchainSignedTx {
serialized: string
body: string
authInfoBytes: string
signatures: string[]
}

export interface ThorchainGetAccountPaths {
accountIdx: number;
Expand Down
1 change: 0 additions & 1 deletion packages/hdwallet-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"scrypt-js": "^3.0.1",
"tendermint-tx-builder": "^1.0.9",
"tiny-secp256k1": "^1.1.6",
"web-encoding": "^1.1.0"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/hdwallet-native/src/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { NativeHDWalletBase } from "./native";
import * as util from "./util";
import * as Isolation from "./crypto/isolation";

const ATOM_CHAIN = "cosmoshub-4";

export function MixinNativeCosmosWalletInfo<TBase extends core.Constructor<core.HDWalletInfo>>(Base: TBase) {
return class MixinNativeCosmosWalletInfo extends Base implements core.CosmosWalletInfo {
readonly _supportsCosmosInfo = true;
Expand Down Expand Up @@ -77,7 +75,7 @@ export function MixinNativeCosmosWallet<TBase extends core.Constructor<NativeHDW
return this.needsMnemonic(!!this.#masterKey, async () => {
const keyPair = await util.getKeyPair(this.#masterKey!, msg.addressNList, "cosmos");
const adapter = await Isolation.Adapters.CosmosDirect.create(keyPair.node,"cosmos");
const result = await protoTxBuilder.sign(msg.tx, adapter, msg.sequence, msg.account_number, ATOM_CHAIN);
const result = await protoTxBuilder.sign(msg.tx, adapter, msg.sequence, msg.account_number, msg.chain_id);
return result
});
}
Expand Down
52 changes: 34 additions & 18 deletions packages/hdwallet-native/src/kava.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe("NativeKavaWallet", () => {
});

it("should generate a correct kava address", async () => {
await expect(
wallet.kavaGetAddress({ addressNList: core.bip32ToAddressNList("m/44'/459'/0'/0/0") })
).resolves.toBe("kava1x9eec99f6m9d0nc3my4uyw55jefkcxj8dwxcpu");
await expect(wallet.kavaGetAddress({ addressNList: core.bip32ToAddressNList("m/44'/459'/0'/0/0") })).resolves.toBe(
"kava1x9eec99f6m9d0nc3my4uyw55jefkcxj8dwxcpu"
);
});

it("should generate another correct kava address", async () => {
Expand All @@ -49,29 +49,45 @@ describe("NativeKavaWallet", () => {
).resolves.toBe("kava1yhys0syftn2f624lue6fsxyql74r3evvljchjt");
});

it("does not support signing transactions", async () => {
it("should (probably) support signing transactions", async () => {
// TODO: Replace with actual test data!
const signed = await wallet.kavaSignTx({
addressNList: core.bip32ToAddressNList("m/44'/459'/0'/0/0"),
tx: {
msg: [{ type: "foo", value: "bar" }],
msg: [
{
type: "cosmos-sdk/MsgSend",
value: {
from_address: "kava1x9eec99f6m9d0nc3my4uyw55jefkcxj8dwxcpu",
to_address: "kava1yhys0syftn2f624lue6fsxyql74r3evvljchjt",
amount: [
{
denom: "ukava",
amount: "100000",
},
],
},
},
],
fee: {
amount: [{ denom: "foo", amount: "bar" }],
gas: "baz",
amount: [
{
amount: "5000",
denom: "ukava",
},
],
gas: "200000",
},
signatures: null,
memo: "foobar",
memo: "testmemo",
},
chain_id: "foobar",
account_number: "foo",
sequence: "bar",
})
expect(signed?.signatures?.length).toBe(1);
expect(signed?.signatures?.[0].pub_key?.value).toMatchInlineSnapshot(
`"AlW0vIrn08ANEFwNKufFfnoU/1pTSjyzo8SMlPKoit3V"`
account_number: "123",
sequence: "456",
});
await expect(signed?.signatures.length).toBe(1);
await expect(signed?.signatures[0]).toMatchInlineSnapshot(
`"X59d8usJrZTD8T9fVh/e+40DJ1+N6O4PzuSz2dIBNfcN3oB17ncj/KWWv29TwsO88DmFXy/dWvSTBJp29NWoqQ=="`
);
expect(signed?.signatures?.[0].signature).toMatchInlineSnapshot(
`"77iUQFCVMfXvEj11YOEdMWOC4KDYxZzRz0WKzRFWnX18AwetdDh15be+iFsVgZ4RJFl2jj1JYoCGKrd9YN+Eew=="`
);

});
});
10 changes: 4 additions & 6 deletions packages/hdwallet-native/src/kava.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "@shapeshiftoss/hdwallet-core";
import * as bech32 from "bech32";
import CryptoJS from "crypto-js";
import * as txBuilder from "tendermint-tx-builder";
import * as protoTxBuilder from "@shapeshiftoss/proto-tx-builder";

import { NativeHDWalletBase } from "./native";
import * as util from "./util";
Expand Down Expand Up @@ -75,11 +75,9 @@ export function MixinNativeKavaWallet<TBase extends core.Constructor<NativeHDWal
async kavaSignTx(msg: core.KavaSignTx): Promise<core.KavaSignedTx | null> {
return this.needsMnemonic(!!this.#masterKey, async () => {
const keyPair = await util.getKeyPair(this.#masterKey!, msg.addressNList, "kava");
// @TODO: This needs to be fixed after the change to tendermint serialization
// @ts-ignore
const adapter = await Isolation.Adapters.Cosmos.create(keyPair);
const result = await txBuilder.sign(msg.tx, adapter, msg.sequence, msg.account_number, msg.chain_id);
return txBuilder.createSignedTx(msg.tx, result);
const adapter = await Isolation.Adapters.CosmosDirect.create(keyPair.node, "kava");
const result = await protoTxBuilder.sign(msg.tx, adapter, msg.sequence, msg.account_number, msg.chain_id);
return result;
});
}
};
Expand Down
45 changes: 31 additions & 14 deletions packages/hdwallet-native/src/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,45 @@ describe("NativeSecretWallet", () => {
).resolves.toBe("secret1wmmewcjt2s09r48ya8mtdfyy0rnnza20xnx6fs");
});

it("should signing transactions", async () => {
it("should (probably) support signing transactions", async () => {
// TODO: Replace with actual test data!
const signed = await wallet.secretSignTx({
addressNList: core.bip32ToAddressNList("m/44'/529'/0'/0/0"),
tx: {
msg: [{ type: "foo", value: "bar" }],
msg: [
{
type: "cosmos-sdk/MsgSend",
value: {
from_address: "secret189wrfk2fsynjlz6jcn54wzdcud3a6k8vqa0ggu",
to_address: "secret1wmmewcjt2s09r48ya8mtdfyy0rnnza20xnx6fs",
amount: [
{
denom: "uscrt",
amount: "100000",
},
],
},
},
],
fee: {
amount: [{ denom: "foo", amount: "bar" }],
gas: "baz",
amount: [
{
amount: "100000",
denom: "uscrt",
},
],
gas: "80000",
},
signatures: null,
memo: "foobar",
memo: "testmemo",
},
chain_id: "foobar",
account_number: 123,
sequence: 456,
})
expect(signed.signatures.length).toBe(1);
expect(signed.signatures[0].pub_key.value).toMatchInlineSnapshot(
`"A2UVKphVsesrnAQEtX4K+qk8Z84wa5xD5mxzdPykAiyR"`
);
expect(signed.signatures[0].signature).toMatchInlineSnapshot(
`"f4HKv09XvsGQn74y4MHL+M+wP/uBjHsIn5PwPfq7xMI7CJkS22Pxx7KlXpeUzCjiaSZvEEIuxbkd9J+Q4g86jg=="`
account_number: "123",
sequence: "456",
});
await expect(signed?.signatures.length).toBe(1);
await expect(signed?.signatures[0]).toMatchInlineSnapshot(
`"W7WaZZWVCnihaZxFS8ysWaFelsCuq6v3ufBy3h5SsosQ1srnbd6Dg47r/nP/Wnni6SUXrN8M8VyGGMCW+rqgig=="`
);
});
});
Loading