-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(billing): implement sign-tx endpoint
as a part of this: - refactor some common deps within the billing module - add custom exceptions and global error handler refs #247
- Loading branch information
1 parent
deca03b
commit bf2acb9
Showing
24 changed files
with
365 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
apps/api/src/billing/controllers/wallet/wallet.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./wallet/wallet.router"; | ||
export * from "@src/billing/routes/create-wallet/create-wallet.router"; | ||
export * from "@src/billing/routes/sign-tx/sign-tx.router"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { createRoute, OpenAPIHono } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
import { z } from "zod"; | ||
|
||
import { WalletController } from "@src/billing/controllers/wallet/wallet.controller"; | ||
|
||
export const SignTxInputSchema = z.object({ | ||
userId: z.string(), | ||
messages: z | ||
.array( | ||
z.object({ | ||
typeUrl: z.string(), | ||
value: z.object({}) | ||
}) | ||
) | ||
.min(1) | ||
.openapi({}), | ||
fee: z.object({ | ||
amount: z.array( | ||
z.object({ | ||
denom: z.string(), | ||
amount: z.string() | ||
}) | ||
), | ||
gas: z.string(), | ||
granter: z.string().optional(), | ||
payer: z.string().optional() | ||
}) | ||
}); | ||
|
||
export const SignTxOutputSchema = z.string(); | ||
export type SignTxInput = z.infer<typeof SignTxInputSchema>; | ||
export type SignTxOutput = z.infer<typeof SignTxOutputSchema>; | ||
|
||
const route = createRoute({ | ||
method: "post", | ||
path: "/v1/sign-tx", | ||
summary: "Signs a transaction via a user managed wallet", | ||
tags: ["Wallets"], | ||
request: { | ||
body: { | ||
content: { | ||
"application/json": { | ||
schema: SignTxInputSchema | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
200: { | ||
description: "Returns a signed transaction", | ||
content: { | ||
"application/json": { | ||
schema: SignTxOutputSchema | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
export const signTxRouter = new OpenAPIHono(); | ||
|
||
signTxRouter.openapi(route, async function routeSignTx(c) { | ||
const payload = await container.resolve(WalletController).signTx(c.req.valid("json")); | ||
return c.json(payload, 200); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "@src/billing/services/wallet/wallet.service"; | ||
export * from "@src/billing/services/managed-user-wallet/managed-user-wallet.service"; | ||
export * from "./rpc-message-service/rpc-message.service"; | ||
export * from "./wallet-initializer/wallet-initializer.service"; | ||
export * from "./master-wallet/master-wallet.service"; | ||
export * from "@src/billing/services/master-signing-client/master-signing-client.service"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
apps/api/src/billing/services/master-signing-client/master-signing-client.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { StdFee } from "@cosmjs/amino"; | ||
import type { EncodeObject } from "@cosmjs/proto-signing"; | ||
import { SigningStargateClient } from "@cosmjs/stargate"; | ||
import type { SignerData } from "@cosmjs/stargate/build/signingstargateclient"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { MasterWalletService } from "@src/billing/services/master-wallet/master-wallet.service"; | ||
|
||
@singleton() | ||
export class MasterSigningClientService { | ||
private readonly clientAsPromised: Promise<SigningStargateClient>; | ||
|
||
constructor( | ||
@InjectBillingConfig() private readonly config: BillingConfig, | ||
private readonly masterWalletService: MasterWalletService | ||
) { | ||
this.clientAsPromised = SigningStargateClient.connectWithSigner(this.config.RPC_NODE_ENDPOINT, this.masterWalletService); | ||
} | ||
|
||
async signAndBroadcast(signerAddress: string, messages: readonly EncodeObject[], fee: StdFee | "auto" | number, memo?: string) { | ||
return (await this.clientAsPromised).signAndBroadcast(signerAddress, messages, fee, memo); | ||
} | ||
|
||
async sign(signerAddress: string, messages: readonly EncodeObject[], fee: StdFee, memo: string, explicitSignerData?: SignerData) { | ||
return (await this.clientAsPromised).sign(signerAddress, messages, fee, memo, explicitSignerData); | ||
} | ||
|
||
async simulate(signerAddress: string, messages: readonly EncodeObject[], memo: string) { | ||
return (await this.clientAsPromised).simulate(signerAddress, messages, memo); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/api/src/billing/services/master-wallet/master-wallet.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; | ||
import { OfflineDirectSigner } from "@cosmjs/proto-signing/build/signer"; | ||
import { SignDoc } from "cosmjs-types/cosmos/tx/v1beta1/tx"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
|
||
@singleton() | ||
export class MasterWalletService implements OfflineDirectSigner { | ||
private readonly PREFIX = "akash"; | ||
|
||
private readonly instanceAsPromised: Promise<DirectSecp256k1HdWallet>; | ||
|
||
constructor(@InjectBillingConfig() private readonly config: BillingConfig) { | ||
this.instanceAsPromised = DirectSecp256k1HdWallet.fromMnemonic(this.config.MASTER_WALLET_MNEMONIC, { prefix: this.PREFIX }); | ||
} | ||
|
||
async getAccounts() { | ||
return (await this.instanceAsPromised).getAccounts(); | ||
} | ||
|
||
async signDirect(signerAddress: string, signDoc: SignDoc) { | ||
return (await this.instanceAsPromised).signDirect(signerAddress, signDoc); | ||
} | ||
|
||
async getFirstAddress() { | ||
const accounts = await this.getAccounts(); | ||
return accounts[0].address; | ||
} | ||
} |
Oops, something went wrong.