@idos-network/kwil-infra 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +47 -0
- package/.turbo/turbo-test.log +126 -0
- package/.turbo/turbo-typecheck.log +5 -0
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/actions/index.d.mts +2 -0
- package/dist/actions/index.mjs +1371 -0
- package/dist/actions/index.mjs.map +1 -0
- package/dist/facesign/index.d.mts +2 -0
- package/dist/facesign/index.mjs +2 -0
- package/dist/facesign-signer-ADd4EASX.d.mts +29 -0
- package/dist/facesign-signer-ADd4EASX.d.mts.map +1 -0
- package/dist/facesign-signer-BY9Nti9F.mjs +244 -0
- package/dist/facesign-signer-BY9Nti9F.mjs.map +1 -0
- package/dist/index-Ci6QhKJo.d.mts +644 -0
- package/dist/index-Ci6QhKJo.d.mts.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +429 -0
- package/dist/index.mjs.map +1 -0
- package/dist/signature-verification/index.d.mts +14 -0
- package/dist/signature-verification/index.d.mts.map +1 -0
- package/dist/signature-verification/index.mjs +140 -0
- package/dist/signature-verification/index.mjs.map +1 -0
- package/dist/xrp/utils.d.mts +1165 -0
- package/dist/xrp/utils.d.mts.map +1 -0
- package/dist/xrp/utils.mjs +105 -0
- package/dist/xrp/utils.mjs.map +1 -0
- package/dist/xrpl-credentials/index.d.mts +179 -0
- package/dist/xrpl-credentials/index.d.mts.map +1 -0
- package/dist/xrpl-credentials/index.mjs +224 -0
- package/dist/xrpl-credentials/index.mjs.map +1 -0
- package/package.json +93 -0
- package/src/actions/actions.ts +2197 -0
- package/src/actions/index.ts +55 -0
- package/src/create-kwil-client.ts +144 -0
- package/src/create-kwil-signer.ts +270 -0
- package/src/facesign/facesign-signer.ts +327 -0
- package/src/facesign/index.ts +2 -0
- package/src/index.ts +7 -0
- package/src/near/create-near-wallet-kwil-signer.ts +245 -0
- package/src/near/get-config.ts +43 -0
- package/src/near/nep413.ts +62 -0
- package/src/near/signature-verification.ts +85 -0
- package/src/signature-verification/evm.ts +33 -0
- package/src/signature-verification/facesign.ts +19 -0
- package/src/signature-verification/index.test.ts +110 -0
- package/src/signature-verification/index.ts +72 -0
- package/src/signature-verification/stellar.ts +31 -0
- package/src/xrp/signature-verification.ts +18 -0
- package/src/xrp/signer.ts +43 -0
- package/src/xrp/utils.ts +178 -0
- package/src/xrpl-credentials/accept.ts +143 -0
- package/src/xrpl-credentials/create.ts +199 -0
- package/src/xrpl-credentials/index.ts +12 -0
- package/tsconfig.json +33 -0
- package/tsdown.config.ts +13 -0
- package/vitest.config.ts +6 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { KwilActionClient } from "..";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type AddUserAsInserterInput,
|
|
5
|
+
type AddWalletInput,
|
|
6
|
+
addWallet,
|
|
7
|
+
type DwgMessageInput,
|
|
8
|
+
type GetAccessGrantsGrantedInput,
|
|
9
|
+
type GetAccessGrantsGrantedOutput,
|
|
10
|
+
type GetAttributesOutput,
|
|
11
|
+
type GetCredentialSharedOutput,
|
|
12
|
+
type GetCredentialsOutput,
|
|
13
|
+
getAccessGrantsGranted,
|
|
14
|
+
removeWallet,
|
|
15
|
+
type UpsertWalletAsInserterInput,
|
|
16
|
+
} from "./actions";
|
|
17
|
+
|
|
18
|
+
export * from "./actions";
|
|
19
|
+
|
|
20
|
+
// Name aliases for a better developer experience
|
|
21
|
+
export type idOSUser = AddUserAsInserterInput;
|
|
22
|
+
export type idOSGrant = GetAccessGrantsGrantedOutput;
|
|
23
|
+
export type idOSCredential = GetCredentialSharedOutput;
|
|
24
|
+
export type idOSCredentialListItem = GetCredentialsOutput;
|
|
25
|
+
export type idOSUserAttribute = GetAttributesOutput;
|
|
26
|
+
export type idOSWallet = UpsertWalletAsInserterInput;
|
|
27
|
+
export type idOSDelegatedWriteGrant = DwgMessageInput;
|
|
28
|
+
|
|
29
|
+
export async function addWallets(
|
|
30
|
+
kwilClient: KwilActionClient,
|
|
31
|
+
wallets: AddWalletInput[],
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
await Promise.all(wallets.map((wallet) => addWallet(kwilClient, wallet)));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function removeWallets(kwilClient: KwilActionClient, ids: string[]): Promise<void> {
|
|
37
|
+
await Promise.all(ids.map((id) => removeWallet(kwilClient, { id })));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const GET_GRANTS_DEFAULT_RECORDS_PER_PAGE = 10;
|
|
41
|
+
|
|
42
|
+
export async function getGrants(
|
|
43
|
+
kwilClient: KwilActionClient,
|
|
44
|
+
params: Partial<GetAccessGrantsGrantedInput> = {
|
|
45
|
+
page: 1,
|
|
46
|
+
size: GET_GRANTS_DEFAULT_RECORDS_PER_PAGE,
|
|
47
|
+
user_id: null,
|
|
48
|
+
},
|
|
49
|
+
): Promise<idOSGrant[]> {
|
|
50
|
+
return getAccessGrantsGranted(kwilClient, {
|
|
51
|
+
page: params.page ?? 1,
|
|
52
|
+
size: params.size ?? 10,
|
|
53
|
+
user_id: params.user_id ?? null,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { Config } from "@idos-network/kwil-js/dist/api_client/config";
|
|
2
|
+
import type {
|
|
3
|
+
ActionBody,
|
|
4
|
+
CallBody,
|
|
5
|
+
PositionalParams,
|
|
6
|
+
} from "@idos-network/kwil-js/dist/core/action";
|
|
7
|
+
import type { DataInfo } from "@idos-network/kwil-js/dist/core/database";
|
|
8
|
+
|
|
9
|
+
import { type KwilSigner, NodeKwil, WebKwil } from "@idos-network/kwil-js";
|
|
10
|
+
import invariant from "tiny-invariant";
|
|
11
|
+
|
|
12
|
+
import type { ActionSchemaElement } from "./actions";
|
|
13
|
+
|
|
14
|
+
import { actionSchema } from "./actions";
|
|
15
|
+
|
|
16
|
+
type CreateKwilClientParams = {
|
|
17
|
+
chainId?: string;
|
|
18
|
+
nodeUrl: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type KwilActions = typeof actionSchema;
|
|
22
|
+
type ActionName = keyof KwilActions;
|
|
23
|
+
|
|
24
|
+
type AllKwilCallAction = {
|
|
25
|
+
[Name in ActionName]: {
|
|
26
|
+
name: Name;
|
|
27
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- This is fine
|
|
28
|
+
inputs: Record<KwilActions[Name][number]["name"], any>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type KwilCallActionRequestParams = AllKwilCallAction[ActionName];
|
|
33
|
+
|
|
34
|
+
type AllKwilExecuteAction = {
|
|
35
|
+
[Name in ActionName]: AllKwilCallAction[Name] & { description: string };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type KwilExecuteActionRequestParams = AllKwilExecuteAction[ActionName];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A client for interacting with kwil with type-safe abstractions for `call` and `execute`.
|
|
42
|
+
* Has utility methods for creating actions and setting a signer.
|
|
43
|
+
*/
|
|
44
|
+
export class KwilActionClient {
|
|
45
|
+
signer?: KwilSigner;
|
|
46
|
+
readonly client: NodeKwil | WebKwil;
|
|
47
|
+
|
|
48
|
+
constructor(client: NodeKwil | WebKwil) {
|
|
49
|
+
this.client = client;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#createActionInputs(actionName: string, params: Record<string, unknown> = {}): PositionalParams {
|
|
53
|
+
if (!params || !Object.keys(params).length) return [];
|
|
54
|
+
|
|
55
|
+
const args = (actionSchema as Record<string, readonly ActionSchemaElement[]>)[actionName];
|
|
56
|
+
|
|
57
|
+
return args.map(({ name }) => {
|
|
58
|
+
const value = params[name];
|
|
59
|
+
// Handle falsy values appropriately
|
|
60
|
+
if (value === "" || value === 0) return value;
|
|
61
|
+
return value ?? null;
|
|
62
|
+
}) as PositionalParams;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#actionTypes(actionName: string): DataInfo[] {
|
|
66
|
+
const args = actionSchema[actionName];
|
|
67
|
+
return args.map((arg) => arg.type);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Calls an action on the kwil nodes. This similar to `GET` like request.
|
|
72
|
+
*/
|
|
73
|
+
async call<T = unknown>(
|
|
74
|
+
params: KwilCallActionRequestParams,
|
|
75
|
+
signer: KwilSigner | undefined = this.signer,
|
|
76
|
+
): Promise<T> {
|
|
77
|
+
const action: CallBody = {
|
|
78
|
+
name: params.name,
|
|
79
|
+
namespace: "main",
|
|
80
|
+
inputs: this.#createActionInputs(params.name, params.inputs),
|
|
81
|
+
types: this.#actionTypes(params.name),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const response = await this.client.call(action as CallBody, signer);
|
|
85
|
+
return response?.data?.result as T;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Executes an action on the kwil nodes. This similar to `POST` like request.
|
|
90
|
+
*/
|
|
91
|
+
async execute(
|
|
92
|
+
params: KwilExecuteActionRequestParams,
|
|
93
|
+
signer: KwilSigner | undefined = this.signer,
|
|
94
|
+
synchronous = true,
|
|
95
|
+
): Promise<string | undefined> {
|
|
96
|
+
invariant(signer, "Signer is not set, you must set it before executing an action");
|
|
97
|
+
const action: ActionBody = {
|
|
98
|
+
name: params.name,
|
|
99
|
+
namespace: "main",
|
|
100
|
+
description: params.description,
|
|
101
|
+
inputs: [this.#createActionInputs(params.name, params.inputs)],
|
|
102
|
+
types: this.#actionTypes(params.name),
|
|
103
|
+
};
|
|
104
|
+
const response = await this.client.execute(action, signer, synchronous);
|
|
105
|
+
return response.data?.tx_hash;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setSigner(signer: KwilSigner | undefined): void {
|
|
109
|
+
this.signer = signer;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Creates action inputs from the given parameters that are used in the action body.
|
|
114
|
+
*/
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const DEFAULT_TIMEOUT = 30_000;
|
|
118
|
+
|
|
119
|
+
const createKwilClient =
|
|
120
|
+
(Cls: new (opts: Config) => NodeKwil | WebKwil) =>
|
|
121
|
+
async ({ nodeUrl: kwilProvider, chainId }: CreateKwilClientParams): Promise<KwilActionClient> => {
|
|
122
|
+
const _kwil = new Cls({ kwilProvider, chainId: "" });
|
|
123
|
+
// oxlint-disable-next-line no-param-reassign -- Default value fallback
|
|
124
|
+
chainId ||= (await _kwil.chainInfo({ disableWarning: true })).data?.chain_id;
|
|
125
|
+
invariant(chainId, "Can't discover `chainId`. You must pass it explicitly.");
|
|
126
|
+
|
|
127
|
+
const client = new KwilActionClient(
|
|
128
|
+
new Cls({ kwilProvider, chainId, timeout: DEFAULT_TIMEOUT }),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return client;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Create a kwil client for node.js environment
|
|
136
|
+
*/
|
|
137
|
+
export const createNodeKwilClient: (params: CreateKwilClientParams) => Promise<KwilActionClient> =
|
|
138
|
+
createKwilClient(NodeKwil);
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Create a kwil client for browser environment
|
|
142
|
+
*/
|
|
143
|
+
export const createWebKwilClient: (params: CreateKwilClientParams) => Promise<KwilActionClient> =
|
|
144
|
+
createKwilClient(WebKwil);
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import type { Store } from "@idos-network/utils/store";
|
|
2
|
+
import type { Wallet as NearWallet } from "@near-wallet-selector/core";
|
|
3
|
+
import type { Keypair as StellarKeypair } from "@stellar/stellar-sdk";
|
|
4
|
+
import type { Wallet as EthersWallet, JsonRpcSigner } from "ethers";
|
|
5
|
+
import type { KeyPair as NearKeyPair } from "near-api-js";
|
|
6
|
+
import type { KeyPair as XrpKeyPair } from "ripple-keypairs/src/types";
|
|
7
|
+
|
|
8
|
+
import { KwilSigner } from "@idos-network/kwil-js";
|
|
9
|
+
import { bs58Encode, hexDecode, hexEncode } from "@idos-network/utils/codecs";
|
|
10
|
+
import nacl from "tweetnacl";
|
|
11
|
+
|
|
12
|
+
import type { WalletType } from "./actions";
|
|
13
|
+
import type { KwilActionClient } from "./create-kwil-client";
|
|
14
|
+
|
|
15
|
+
import { FaceSignSignerProvider } from "./facesign/facesign-signer";
|
|
16
|
+
import {
|
|
17
|
+
createNearWalletKwilSigner,
|
|
18
|
+
implicitAddressFromPublicKey,
|
|
19
|
+
looksLikeNearWallet,
|
|
20
|
+
} from "./near/create-near-wallet-kwil-signer";
|
|
21
|
+
import { kwilNep413Signer } from "./near/nep413";
|
|
22
|
+
import { createXrpKwilSigner } from "./xrp/signer";
|
|
23
|
+
import { getXrpPublicKey, looksLikeXrpWallet } from "./xrp/utils";
|
|
24
|
+
|
|
25
|
+
export { KwilSigner } from "@idos-network/kwil-js";
|
|
26
|
+
export type Wallet =
|
|
27
|
+
| EthersWallet
|
|
28
|
+
| JsonRpcSigner
|
|
29
|
+
| NearWallet
|
|
30
|
+
| CustomKwilSigner
|
|
31
|
+
| FaceSignSignerProvider;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Helper function to check if the given object is a `nacl.SignKeyPair`.
|
|
35
|
+
*/
|
|
36
|
+
function isNaclSignKeyPair(object: unknown): object is nacl.SignKeyPair {
|
|
37
|
+
return (
|
|
38
|
+
object !== null &&
|
|
39
|
+
typeof object === "object" &&
|
|
40
|
+
"publicKey" in object &&
|
|
41
|
+
object.publicKey instanceof Uint8Array &&
|
|
42
|
+
object.publicKey.length === nacl.sign.publicKeyLength &&
|
|
43
|
+
"secretKey" in object &&
|
|
44
|
+
object.secretKey instanceof Uint8Array &&
|
|
45
|
+
object.secretKey.length === nacl.sign.secretKeyLength
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Helper function to check if the given object is a NEAR KeyPair.
|
|
51
|
+
*/
|
|
52
|
+
function isNearKeyPair(object: unknown): object is NearKeyPair {
|
|
53
|
+
return (
|
|
54
|
+
object !== null &&
|
|
55
|
+
typeof object === "object" &&
|
|
56
|
+
"getPublicKey" in object &&
|
|
57
|
+
"sign" in object &&
|
|
58
|
+
typeof object.getPublicKey === "function" &&
|
|
59
|
+
typeof object.sign === "function"
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Helper function to check if the given object is a Stellar Keypair.
|
|
65
|
+
*/
|
|
66
|
+
function isStellarKeyPair(object: unknown): object is StellarKeypair {
|
|
67
|
+
return (
|
|
68
|
+
object !== null &&
|
|
69
|
+
typeof object === "object" &&
|
|
70
|
+
"publicKey" in object &&
|
|
71
|
+
"sign" in object &&
|
|
72
|
+
"canSign" in object &&
|
|
73
|
+
typeof object.publicKey === "function" &&
|
|
74
|
+
typeof object.sign === "function" &&
|
|
75
|
+
typeof object.canSign === "function"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface CustomKwilSigner extends KwilSigner {
|
|
80
|
+
publicAddress: string;
|
|
81
|
+
signatureType: string;
|
|
82
|
+
publicKey: string;
|
|
83
|
+
walletType: string;
|
|
84
|
+
signMessage: (message: Uint8Array | string) => Promise<Uint8Array>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isCustomKwilSigner(object: unknown): object is CustomKwilSigner {
|
|
88
|
+
return (
|
|
89
|
+
object !== null &&
|
|
90
|
+
typeof object === "object" &&
|
|
91
|
+
"publicAddress" in object &&
|
|
92
|
+
"signatureType" in object &&
|
|
93
|
+
"publicKey" in object &&
|
|
94
|
+
"signMessage" in object
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Helper function to check if the given object is a XRP KeyPair (Server key pairs only).
|
|
100
|
+
*/
|
|
101
|
+
function isXrplKeyPair(object: unknown): object is XrpKeyPair {
|
|
102
|
+
return !!object && typeof object === "object" && "privateKey" in object && "publicKey" in object;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type KwilSignerType =
|
|
106
|
+
| NearKeyPair
|
|
107
|
+
| EthersWallet
|
|
108
|
+
| nacl.SignKeyPair
|
|
109
|
+
| JsonRpcSigner
|
|
110
|
+
| StellarKeypair
|
|
111
|
+
| XrpKeyPair;
|
|
112
|
+
|
|
113
|
+
export type SignerAddress = string;
|
|
114
|
+
export type SignerPublicKey = string | undefined;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Creates a `KwilSigner` and its associated `SignerAddress`.
|
|
118
|
+
*
|
|
119
|
+
* This function is explicitly marked as being for backend use only because it doesn't reset
|
|
120
|
+
* the KGW cookie when logging out and re-logging in with a different wallet.
|
|
121
|
+
*/
|
|
122
|
+
export async function createServerKwilSigner(
|
|
123
|
+
signer: KwilSignerType,
|
|
124
|
+
): Promise<[KwilSigner, SignerAddress]> {
|
|
125
|
+
if (isNaclSignKeyPair(signer)) {
|
|
126
|
+
return [
|
|
127
|
+
new KwilSigner(
|
|
128
|
+
async (msg: Uint8Array) => nacl.sign.detached(msg, signer.secretKey),
|
|
129
|
+
signer.publicKey,
|
|
130
|
+
"ed25519",
|
|
131
|
+
),
|
|
132
|
+
implicitAddressFromPublicKey(bs58Encode(signer.publicKey)),
|
|
133
|
+
];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (isNearKeyPair(signer)) {
|
|
137
|
+
const publicKey = implicitAddressFromPublicKey(signer.getPublicKey().toString());
|
|
138
|
+
return [
|
|
139
|
+
new KwilSigner(kwilNep413Signer("idos-issuer")(signer), publicKey, "nep413"),
|
|
140
|
+
publicKey,
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (isStellarKeyPair(signer)) {
|
|
145
|
+
const publicKeyString = signer.publicKey();
|
|
146
|
+
const rawPublicKey = signer.rawPublicKey();
|
|
147
|
+
return [
|
|
148
|
+
new KwilSigner(
|
|
149
|
+
async (msg: Uint8Array) => signer.sign(Buffer.from(msg)),
|
|
150
|
+
rawPublicKey,
|
|
151
|
+
"ed25519",
|
|
152
|
+
),
|
|
153
|
+
publicKeyString,
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (isXrplKeyPair(signer)) {
|
|
158
|
+
let xrpKeypair: typeof import("ripple-keypairs");
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
xrpKeypair = await import("ripple-keypairs");
|
|
162
|
+
} catch (e) {
|
|
163
|
+
throw new Error("Can't load ripple-keypairs", { cause: e });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return [
|
|
167
|
+
new KwilSigner(
|
|
168
|
+
async (msg: Uint8Array) => hexDecode(xrpKeypair.sign(hexEncode(msg), signer.privateKey)),
|
|
169
|
+
signer.publicKey,
|
|
170
|
+
"xrpl",
|
|
171
|
+
),
|
|
172
|
+
signer.publicKey,
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if ("address" in signer) {
|
|
177
|
+
return [new KwilSigner(signer, signer.address), signer.address];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Force the check that `signer` is never.
|
|
181
|
+
// If these lines start complaining, that means we're missing an `if` above.
|
|
182
|
+
return ((_: never) => {
|
|
183
|
+
throw new Error("Invalid `signer` type");
|
|
184
|
+
})(signer);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function createClientKwilSigner(
|
|
188
|
+
store: Store,
|
|
189
|
+
kwilClient: KwilActionClient,
|
|
190
|
+
wallet: Wallet,
|
|
191
|
+
): Promise<[KwilSigner, SignerAddress, SignerPublicKey, WalletType]> {
|
|
192
|
+
if ("connect" in wallet && "address" in wallet) {
|
|
193
|
+
// oxlint-disable-next-line no-param-reassign -- Narrowing the type on purpose
|
|
194
|
+
wallet = wallet as unknown as JsonRpcSigner;
|
|
195
|
+
const currentAddress = await wallet.getAddress();
|
|
196
|
+
|
|
197
|
+
const storedAddress = await store.get<string>("signer-address");
|
|
198
|
+
|
|
199
|
+
if (storedAddress !== currentAddress) {
|
|
200
|
+
// To avoid re-using the old signer's kgw cookie.
|
|
201
|
+
// When kwil-js supports multi cookies, we can remove this.
|
|
202
|
+
store.set("signer-address", currentAddress);
|
|
203
|
+
try {
|
|
204
|
+
await kwilClient.client.auth.logoutKGW();
|
|
205
|
+
} catch (error) {
|
|
206
|
+
console.log("error logoutKGW", error);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return [new KwilSigner(wallet, currentAddress), currentAddress, undefined, "EVM"];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (looksLikeNearWallet(wallet)) {
|
|
214
|
+
const accounts = await wallet.getAccounts();
|
|
215
|
+
const { kwilSigner, publicKey } = await createNearWalletKwilSigner(
|
|
216
|
+
wallet,
|
|
217
|
+
accounts[0].accountId,
|
|
218
|
+
store,
|
|
219
|
+
kwilClient,
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
return [kwilSigner, accounts[0].accountId, publicKey, "NEAR"];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (looksLikeXrpWallet(wallet)) {
|
|
226
|
+
const { address: currentAddress, publicKey: walletPublicKey } = (await getXrpPublicKey(
|
|
227
|
+
wallet,
|
|
228
|
+
)) as { address: string; publicKey: string };
|
|
229
|
+
if (!currentAddress) {
|
|
230
|
+
throw new Error("Failed to get XRP address");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return [
|
|
234
|
+
await createXrpKwilSigner(wallet, currentAddress, store, kwilClient, walletPublicKey),
|
|
235
|
+
currentAddress,
|
|
236
|
+
walletPublicKey,
|
|
237
|
+
"XRPL",
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (isCustomKwilSigner(wallet) || wallet instanceof FaceSignSignerProvider) {
|
|
242
|
+
const storedAddress = await store.get<string>("signer-address");
|
|
243
|
+
|
|
244
|
+
if (storedAddress !== wallet.publicAddress) {
|
|
245
|
+
store.set("signer-address", wallet.publicAddress);
|
|
246
|
+
try {
|
|
247
|
+
await kwilClient.client.auth.logoutKGW();
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.log("error logoutKGW", error);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return [
|
|
254
|
+
new KwilSigner(
|
|
255
|
+
async (msg: Uint8Array) => wallet.signMessage(msg),
|
|
256
|
+
wallet.publicAddress,
|
|
257
|
+
"ed25519",
|
|
258
|
+
),
|
|
259
|
+
wallet.publicAddress,
|
|
260
|
+
wallet.publicKey,
|
|
261
|
+
wallet.walletType as WalletType,
|
|
262
|
+
];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Force the check that `signer` is `never`.
|
|
266
|
+
// If these lines start complaining, that means we're missing an `if` above.
|
|
267
|
+
return ((_: never) => {
|
|
268
|
+
throw new Error("Invalid `signer` type");
|
|
269
|
+
})(wallet);
|
|
270
|
+
}
|