@metamask/keyring-snap-client
Version:
MetaMask Keyring Snap clients
87 lines • 3.78 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _KeyringClient_sender;
import { CreateAccountsResponseStruct, DeleteAccountResponseStruct, GetAccountResponseStruct, GetAccountsResponseStruct, SubmitRequestResponseStruct, KeyringRpcMethod, ExportAccountResponseStruct } from "@metamask/keyring-api/v2";
import { strictMask } from "@metamask/keyring-utils";
import { assert } from "@metamask/superstruct";
import { v4 as uuid } from "uuid";
export class KeyringClient {
/**
* Create a new instance of `KeyringClient`.
*
* @param sender - The `Sender` instance to use to send requests to the snap.
*/
constructor(sender) {
_KeyringClient_sender.set(this, void 0);
__classPrivateFieldSet(this, _KeyringClient_sender, sender, "f");
}
/**
* Send a request to the Snap and return the response.
*
* @param request - A partial JSON-RPC request (method and params).
* @returns A promise that resolves to the response to the request.
*/
async send(request) {
return __classPrivateFieldGet(this, _KeyringClient_sender, "f").send({
...request,
});
}
async getAccounts() {
return strictMask(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.GetAccounts,
}), GetAccountsResponseStruct);
}
async getAccount(id) {
return strictMask(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.GetAccount,
params: { id },
}), GetAccountResponseStruct);
}
async createAccounts(params) {
return strictMask(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.CreateAccounts,
params,
}), CreateAccountsResponseStruct);
}
async exportAccount(id, options) {
return strictMask(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.ExportAccount,
params: { id, ...(options && { options }) },
}), ExportAccountResponseStruct);
}
async deleteAccount(id) {
assert(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.DeleteAccount,
params: { id },
}), DeleteAccountResponseStruct);
}
async submitRequest(request) {
return strictMask(await this.send({
jsonrpc: '2.0',
id: uuid(),
method: KeyringRpcMethod.SubmitRequest,
params: request,
}), SubmitRequestResponseStruct);
}
}
_KeyringClient_sender = new WeakMap();
//# sourceMappingURL=KeyringClient.mjs.map