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

Eth secp256k1 auth for ethermint #27

Merged
merged 12 commits into from
Aug 2, 2024
Merged
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
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/libs/interchainjs",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests in Interchainjs eth token",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--config",
"./jest.starship.config.js",
"--verbose",
"--bail",
"starship/__tests__/token.test.ts"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/networks/ethermint",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests in Interchainjs eth gov",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--config",
"./jest.starship.config.js",
"--verbose",
"--bail",
"starship/__tests__/gov.test.ts"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/networks/ethermint",
"internalConsoleOptions": "neverOpen"
}
]
}
2 changes: 1 addition & 1 deletion libs/interchainjs/src/cosmwasm-stargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class CosmWasmSigningClient extends SigningClient {
options: SignerOptions = {}
): Promise<CosmWasmSigningClient> {
const signingClient = new CosmWasmSigningClient(
new RpcClient(endpoint, undefined, options.prefix),
new RpcClient(endpoint, options.prefix),
signer,
options
);
Expand Down
21 changes: 12 additions & 9 deletions libs/interchainjs/src/signing-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AminoSigner } from '@interchainjs/cosmos/amino';
import { DirectSigner } from '@interchainjs/cosmos/direct';
import { RpcClient } from '@interchainjs/cosmos/query/rpc';
import {
AccountData,
AminoConverter,
Encoder,
isICosmosAccount,
Expand All @@ -19,7 +18,13 @@ import { PubKey as Secp256k1PubKey } from '@interchainjs/cosmos-types/cosmos/cry
import { TxBody, TxRaw } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { Any } from '@interchainjs/cosmos-types/google/protobuf/any';
import { TxRpc } from '@interchainjs/cosmos-types/types';
import { HttpEndpoint, IKey, Price, StdFee } from '@interchainjs/types';
import {
AccountData,
HttpEndpoint,
IKey,
Price,
StdFee,
} from '@interchainjs/types';
import { fromBase64 } from '@interchainjs/utils';

import {
Expand Down Expand Up @@ -85,7 +90,7 @@ export class SigningClient {
options: SignerOptions = {}
): Promise<SigningClient> {
const signingClient = new SigningClient(
new RpcClient(endpoint, undefined, options.prefix),
new RpcClient(endpoint, options.prefix),
signer,
options
);
Expand Down Expand Up @@ -133,8 +138,6 @@ export class SigningClient {
this.directSigners[await signer.getAddress()] = signer;
}
}

this.queryClient.setHashedPubkey(firstPubkey);
}

private async getAccountData(address: string): Promise<AccountData> {
Expand Down Expand Up @@ -183,12 +186,12 @@ export class SigningClient {
return await this.queryClient.getChainId();
}

async getAccountNumber() {
return await this.queryClient.getAccountNumber();
async getAccountNumber(address: string) {
return await this.queryClient.getAccountNumber(address);
}

async getSequence() {
return await this.queryClient.getSequence();
async getSequence(address: string) {
return await this.queryClient.getSequence(address);
}

getSinger(signerAddress: string) {
Expand Down
2 changes: 1 addition & 1 deletion libs/interchainjs/src/stargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class StargateSigningClient extends SigningClient {
options: SignerOptions = {}
): Promise<StargateSigningClient> {
const signingClient = new StargateSigningClient(
new RpcClient(endpoint, undefined, options.prefix),
new RpcClient(endpoint, options.prefix),
signer,
options
);
Expand Down
2 changes: 0 additions & 2 deletions libs/interchainjs/src/types/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

export type Algo = 'secp256k1' | 'ed25519' | 'sr25519';

export type Bech32Address = string;

export interface Pubkey {
Expand Down
3 changes: 1 addition & 2 deletions libs/interchainjs/starship/configs/config.local.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

name: interchainjs
version: v0.2.6
version: v0.2.12

chains:
- id: osmosis-1
Expand Down
2 changes: 1 addition & 1 deletion libs/interchainjs/starship/configs/config.workflow.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: interchainjs
version: v0.2.6
version: v0.2.12

chains:
- id: osmosis-1
Expand Down
2 changes: 1 addition & 1 deletion libs/interchainjs/starship/configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: interchainjs
version: v0.2.6
version: v0.2.12

chains:
- id: osmosis-1
Expand Down
9 changes: 9 additions & 0 deletions networks/cosmos/src/amino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BaseCosmosTxBuilderContext } from './base/builder-context';
import { AminoSigBuilder, AminoTxBuilder } from './builder/amino-tx-builder';
import {
AminoConverter,
CosmosAccount,
CosmosAminoDoc,
CosmosAminoSigner,
Encoder,
Expand Down Expand Up @@ -82,6 +83,14 @@ export class AminoSigner
return new AminoTxBuilder(new BaseCosmosTxBuilderContext(this));
}

async getAccount() {
return new CosmosAccount(
await this.getPrefix(),
this.auth,
this.config.publicKey.isCompressed
);
}

static async fromWallet(
signer: OfflineAminoSigner,
encoders: Encoder[],
Expand Down
31 changes: 24 additions & 7 deletions networks/cosmos/src/base/base-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BaseSigner,
BroadcastOptions,
HttpEndpoint,
IAccount,
IKey,
isDocAuth,
SignDocResponse,
Expand Down Expand Up @@ -66,6 +67,7 @@ export abstract class CosmosBaseSigner<SignDoc>
readonly _encodePublicKey: (key: IKey) => EncodedMessage;
readonly parseAccount: (encodedAccount: EncodedMessage) => BaseAccount;
prefix?: string;
account?: IAccount;
declare txBuilder: BaseCosmosTxBuilder<SignDoc>;

constructor(
Expand Down Expand Up @@ -98,6 +100,18 @@ export abstract class CosmosBaseSigner<SignDoc>
this.encoders.push(...encoders);
};

getPrefix = async () => {
if (this.prefix) {
return this.prefix;
}

if (this.queryClient) {
return this.queryClient.getPrefix();
}

throw new Error("Can't get prefix because no queryClient is set");
};

getEncoder = (typeUrl: string) => {
const encoder = this.encoders.find(
(encoder) => encoder.typeUrl === typeUrl
Expand All @@ -111,15 +125,17 @@ export abstract class CosmosBaseSigner<SignDoc>
};

async getAddress() {
return await this.queryClient.getAddress();
if (!this.account) {
this.account = await this.getAccount();
}

return this.account.address;
}

abstract getAccount(): Promise<IAccount>;

setEndpoint(endpoint: string | HttpEndpoint) {
this._queryClient = new RpcClient(
endpoint,
this.publicKeyHash,
this.prefix
);
this._queryClient = new RpcClient(endpoint, this.prefix);
(this._queryClient as RpcClient).setAccountParser(this.parseAccount);
}

Expand Down Expand Up @@ -192,7 +208,8 @@ export abstract class CosmosBaseSigner<SignDoc>
});
const { signerInfo } = await this.txBuilder.buildSignerInfo(
this.encodedPublicKey,
options?.sequence ?? (await this.queryClient.getSequence()),
options?.sequence ??
(await this.queryClient.getSequence(await this.getAddress())),
options?.signMode ?? SignMode.SIGN_MODE_DIRECT
);

Expand Down
5 changes: 4 additions & 1 deletion networks/cosmos/src/base/tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export abstract class BaseCosmosTxBuilder<SignDoc>
});
const { signerInfo } = await this.buildSignerInfo(
this.ctx.signer.encodedPublicKey,
options?.sequence ?? (await this.ctx.signer.queryClient.getSequence()),
options?.sequence ??
(await this.ctx.signer.queryClient.getSequence(
await this.ctx.signer.getAddress()
)),
this.signMode
);

Expand Down
9 changes: 7 additions & 2 deletions networks/cosmos/src/builder/amino-tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ export class AminoTxBuilder extends BaseCosmosTxBuilder<CosmosAminoDoc> {
options?.chainId ?? (await this.ctx.signer.queryClient.getChainId()),
account_number: (
options?.accountNumber ??
(await this.ctx.signer.queryClient.getAccountNumber())
(await this.ctx.signer.queryClient.getAccountNumber(
await this.ctx.signer.getAddress()
))
).toString(),
sequence: (
options?.sequence ?? (await this.ctx.signer.queryClient.getSequence())
options?.sequence ??
(await this.ctx.signer.queryClient.getSequence(
await this.ctx.signer.getAddress()
))
).toString(),
fee,
msgs: toAminoMsgs(messages, this.ctx.signer.getConverterFromTypeUrl),
Expand Down
4 changes: 3 additions & 1 deletion networks/cosmos/src/builder/direct-tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class DirectTxBuilder extends BaseCosmosTxBuilder<CosmosDirectDoc> {
options?.chainId ?? (await this.ctx.signer.queryClient.getChainId()),
accountNumber:
options?.accountNumber ??
(await this.ctx.signer.queryClient.getAccountNumber()),
(await this.ctx.signer.queryClient.getAccountNumber(
await this.ctx.signer.getAddress()
)),
});
return signDoc;
}
Expand Down
14 changes: 1 addition & 13 deletions networks/cosmos/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Secp256k1Signature } from '@interchainjs/auth/secp256k1';
import {
BaseAccount,
ModuleAccount,
Expand Down Expand Up @@ -44,18 +43,6 @@ export const defaultSignerConfig: SignerConfig = {
return hashed;
},
},
signature: {
fromCompact: (key: Key, algo: string) => {
switch (algo) {
case 'secp256k1':
return Secp256k1Signature.fromCompact(key);
case 'ed25519':
throw new Error('Not implemented yet');
default:
throw new Error(`Unidentified algorithm: ${algo}`);
}
},
},
};

export const defaultPublicKeyEncoder = (key: IKey): EncodedMessage => {
Expand Down Expand Up @@ -89,6 +76,7 @@ export const defaultAccountParser = (
`No corresponding account found for account type ${encodedAccount.typeUrl}.`
);
}

const decoder = toDecoder(codec);
const account = decoder.fromPartial(decoder.decode(encodedAccount.value));
const baseAccount =
Expand Down
9 changes: 9 additions & 0 deletions networks/cosmos/src/direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseCosmosTxBuilder, CosmosBaseSigner, CosmosDocSigner } from './base';
import { BaseCosmosTxBuilderContext } from './base/builder-context';
import { DirectSigBuilder, DirectTxBuilder } from './builder/direct-tx-builder';
import {
CosmosAccount,
CosmosDirectDoc,
CosmosDirectSigner,
Encoder,
Expand All @@ -28,6 +29,14 @@ export class DirectSignerBase extends CosmosBaseSigner<CosmosDirectDoc> {
super(auth, encoders, endpoint, options);
}

async getAccount() {
return new CosmosAccount(
await this.getPrefix(),
this.auth,
this.config.publicKey.isCompressed
);
}

getTxBuilder(): BaseCosmosTxBuilder<CosmosDirectDoc> {
return new DirectTxBuilder(new BaseCosmosTxBuilderContext(this));
}
Expand Down
Loading
Loading