Add Wallet interface

This commit is contained in:
Thunnini 2022-12-12 14:25:47 +09:00
parent 4b866891ac
commit 4410691a45
85 changed files with 1701 additions and 4 deletions

833
.pnp.cjs generated

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -10,6 +10,7 @@
"lint": "next lint"
},
"dependencies": {
"@keplr-wallet/types": "^0.11.23",
"crypto": "^1.0.1",
"iron-session": "^6.3.1",
"next": "13.0.5",

2
wallets/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from "./types";
export * from "./keplr";

98
wallets/keplr.ts Normal file
View File

@ -0,0 +1,98 @@
import {
AminoSignResponse,
BroadcastMode,
ChainInfo,
Keplr,
StdSignDoc,
} from "@keplr-wallet/types";
import { Wallet } from "./types";
export const getKeplrFromWindow: () => Promise<
Keplr | undefined
> = async () => {
if (typeof window === "undefined") {
return undefined;
}
if (window.keplr) {
return window.keplr;
}
if (document.readyState === "complete") {
return window.keplr;
}
return new Promise((resolve) => {
const documentStateChange = (event: Event) => {
if (
event.target &&
(event.target as Document).readyState === "complete"
) {
resolve(window.keplr);
document.removeEventListener("readystatechange", documentStateChange);
}
};
document.addEventListener("readystatechange", documentStateChange);
});
};
export class KeplrWallet implements Wallet {
constructor(public readonly keplr: Keplr) {}
broadcastTxSync(chainId: string, tx: Uint8Array): Promise<Uint8Array> {
return this.keplr.sendTx(chainId, tx, BroadcastMode.Sync);
}
getChainInfosWithoutEndpoints(): Promise<
Omit<ChainInfo, "rest" | "rpc" | "nodeProvider">[]
> {
// TODO: Update @keplr-wallet/types
return (this.keplr as any).getChainInfosWithoutEndpoints();
}
getKey(chainId: string): Promise<{
readonly name: string;
readonly pubKey: Uint8Array;
readonly address: Uint8Array;
}> {
return this.keplr.getKey(chainId);
}
init(chainIds: string[]): Promise<void> {
return this.keplr.enable(chainIds);
}
signAmino(
chainId: string,
signer: string,
signDoc: StdSignDoc,
): Promise<AminoSignResponse> {
return this.keplr.signAmino(chainId, signer, signDoc);
}
signICNSAdr36(
chainId: string,
contractAddress: string,
owner: string,
username: string,
addressChainIds: string[],
): Promise<{
chainId: string;
bech32Prefix: string;
bech32Address: string;
addressHash: "cosmos" | "ethereum";
pubKey: Uint8Array;
signatureSalt: number;
signature: Uint8Array;
}> {
// TODO: Update @keplr-wallet/types
return (this.keplr as any).signICNSAdr36(
chainId,
contractAddress,
owner,
username,
addressChainIds,
);
}
}

37
wallets/types.ts Normal file
View File

@ -0,0 +1,37 @@
import { AminoSignResponse, ChainInfo, StdSignDoc } from "@keplr-wallet/types";
export interface Wallet {
init(chainIds: string[]): Promise<void>;
getChainInfosWithoutEndpoints(): Promise<
Omit<ChainInfo, "rest" | "rpc" | "nodeProvider">[]
>;
getKey(chainId: string): Promise<{
readonly name: string;
readonly pubKey: Uint8Array;
readonly address: Uint8Array;
}>;
signAmino(
chainId: string,
signer: string,
signDoc: StdSignDoc,
): Promise<AminoSignResponse>;
broadcastTxSync(chainId: string, tx: Uint8Array): Promise<Uint8Array>;
signICNSAdr36(
chainId: string,
contractAddress: string,
owner: string,
username: string,
addressChainIds: string[],
): Promise<{
chainId: string;
bech32Prefix: string;
bech32Address: string;
addressHash: "cosmos" | "ethereum";
pubKey: Uint8Array;
signatureSalt: number;
signature: Uint8Array;
}>;
}

6
window.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
import { Window as KeplrWindow } from "@keplr-wallet/types";
declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Window extends KeplrWindow {}
}

728
yarn.lock

File diff suppressed because it is too large Load Diff