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

refactor(wallet): fixes any types usage (#77) #78

Merged
merged 3 commits into from
Apr 22, 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
238 changes: 40 additions & 198 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@cosmjs/launchpad": "^0.27.0",
"@cosmjs/proto-signing": "^0.28.11",
"@cosmjs/stargate": "^0.28.0",
"@cosmjs/tendermint-rpc": "^0.25.4",
"@cosmjs/tendermint-rpc": "^0.28.11",
"asn1js": "^2.1.1",
"atob": "^2.1.2",
"axios": "^0.24.0",
Expand Down
4 changes: 2 additions & 2 deletions src/certificates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { create as create509, pems } from "./generate509";
import { SigningStargateClient } from "@cosmjs/stargate";
import { messages as stargateMessages } from "../stargate";
import { Message as stargateMessages } from "../stargate";
import { createStarGateMessage } from "../pbclient/pbclient";

import { QueryCertificatesRequest, QueryCertificatesResponse, CertificateFilter } from "@akashnetwork/akash-api/akash/cert/v1beta3";
Expand Down Expand Up @@ -34,7 +34,7 @@ export async function createCertificate(bech32Address: string) {
export async function revokeCertificate(owner: string, serial: string, client: SigningStargateClient) {
const message = createStarGateMessage(stargateMessages.MsgRevokeCertificate, {
id: {
owner: owner,
owner,
serial
}
});
Expand Down
34 changes: 17 additions & 17 deletions src/keplr/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { getAkashTypeRegistry } from "../stargate";
import { defaultRegistryTypes, SigningStargateClient } from "@cosmjs/stargate";
import { Registry } from "@cosmjs/proto-signing";
import { OfflineDirectSigner, OfflineSigner, Registry } from "@cosmjs/proto-signing";
import { AminoTypes } from "@cosmjs/stargate";
import { Certificate } from "@akashnetwork/akash-api/akash/cert/v1beta2";
import { MsgCreateCertificate } from "../protobuf/akash/cert/v1beta1/cert";

interface Chain {
id: string;
}

export function getChains() {
return {
Expand All @@ -15,42 +20,37 @@ export function getChains() {
};
}

export function getSigner(chain: any) {
return (window as any).getOfflineSignerAuto(chain.id);
export function getSigner(chain: Chain) {
return window.getOfflineSignerAuto(chain.id);
}

export async function get(chain: any, signer: any, endPoint: string) {
const customAminoTypes: any = new AminoTypes({
export async function get(chain: Chain, signer: OfflineSigner | OfflineDirectSigner, endPoint: string) {
const customAminoTypes = new AminoTypes({
"/akash.cert.v1beta2.MsgCreateCertificate": {
aminoType: "cert/cert-create-certificate",
toAmino: ({ owner, cert, pubkey }: any) => {
const buf: any = Certificate.encode(
toAmino: ({ cert, pubkey }: MsgCreateCertificate) => {
const buf = Certificate.encode(
Certificate.fromPartial({
owner,
ygrishajev marked this conversation as resolved.
Show resolved Hide resolved
cert,
pubkey
} as any)
})
).finish();
const encoded = Buffer.from(buf);
return encoded.toString("base64");
},
fromAmino: ({ owner, cert, pubkey }: any) => {
fromAmino: ({ cert, pubkey }: MsgCreateCertificate) => {
return Certificate.fromPartial({
owner,
cert,
pubkey
} as any);
});
}
}
} as any);
});

const myRegistry = new Registry([...defaultRegistryTypes, ...getAkashTypeRegistry()]);

return await SigningStargateClient.connectWithSigner(endPoint, signer, {
bip44: {
ygrishajev marked this conversation as resolved.
Show resolved Hide resolved
coinType: "118"
},
registry: myRegistry,
aminoTypes: customAminoTypes
} as any);
});
}
11 changes: 8 additions & 3 deletions src/pbclient/pbclient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { faker } from "@faker-js/faker";

import { createAminoMessage } from "./pbclient";
import { messages } from "../stargate";
import { Message } from "../stargate";
import { AminoMsg } from "cosmwasm";

describe("createAminoMessage", () => {
it("creates an amino message", () => {
const message = faker.helpers.arrayElement(Object.values(messages));
const messageBody = "messageBody";
const message = faker.helpers.arrayElement(Object.values(Message));
const messageBody: AminoMsg = {
type: faker.string.alpha(10),
value: faker.string.alpha(10)
};
const result = createAminoMessage(message, messageBody);

expect(result).toEqual({
typeUrl: message,
value: messageBody
Expand Down
19 changes: 13 additions & 6 deletions src/pbclient/pbclient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { messages } from "../stargate";
import { Message } from "../stargate";
import { AminoMsg } from "cosmwasm";
import { MsgCreateCertificate, MsgRevokeCertificate } from "../protobuf/akash/cert/v1beta3/cert";

// dynamically determine max gas
const fee = {
const FEE = {
amount: [
{
denom: "uakt",
Expand All @@ -11,19 +12,25 @@ const fee = {
gas: "100000"
};

export function createAminoMessage(message: messages, messageBody: any) {
export function createAminoMessage(message: Message, messageBody: AminoMsg) {
return {
typeUrl: message,
value: messageBody
};
}

export function createStarGateMessage(message: messages, messageBody: any) {
type WithoutType<T> = Omit<T, "$type">;
type MessageTypes = {
[Message.MsgCreateCertificate]: WithoutType<MsgCreateCertificate>;
[Message.MsgRevokeCertificate]: Omit<WithoutType<MsgRevokeCertificate>, "id"> & { id: WithoutType<MsgRevokeCertificate["id"]> };
};

export function createStarGateMessage<T extends keyof MessageTypes>(message: T, messageBody: MessageTypes[T]) {
return {
message: {
typeUrl: message,
value: messageBody
},
fee: fee
fee: FEE
};
}
4 changes: 2 additions & 2 deletions src/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { createProtobufRpcClient, GasPrice, QueryClient, SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate";
import { getAkashTypeRegistry } from "../stargate";
import { OfflineSigner, Registry } from "@cosmjs/proto-signing";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";

export async function getRpc(endpoint: string) {
return getQueryClient(endpoint);
}

export async function getQueryClient(endpoint: string) {
const tmClient: any = await Tendermint34Client.connect(endpoint);
const tmClient = await Tendermint34Client.connect(endpoint);
const queryClient = new QueryClient(tmClient);
return createProtobufRpcClient(queryClient);
}
Expand Down
2 changes: 1 addition & 1 deletion src/stargate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getAkashTypeRegistry: () => [string, MessageType<UnknownMessage>][]
export const getTypeUrl: (type: MessageType) => string = type => `/${type.$type}`;

/* TODO: this should be generated from the proto files */
export enum messages {
export enum Message {
MsgCreateCertificate = "/akash.cert.v1beta3.MsgCreateCertificate",
MsgRevokeCertificate = "/akash.cert.v1beta3.MsgRevokeCertificate",
MsgCreateDeployment = "/akash.deployment.v1beta3.MsgCreateDeployment",
Expand Down
9 changes: 9 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OfflineDirectSigner, OfflineSigner } from "@cosmjs/proto-signing";

export {};

declare global {
interface Window {
getOfflineSignerAuto: (chainId: string) => Promise<OfflineSigner | OfflineDirectSigner>;
}
}
Loading