UNPKG

@metamask/keyring-api

Version:
119 lines 3.57 kB
import { exactOptional, selectiveUnion, type } from "@metamask/keyring-utils"; import { boolean, intersection, literal, number, object, record, string } from "@metamask/superstruct"; import { isPlainObject, JsonStruct } from "@metamask/utils"; /** * Keyring account entropy valid types. */ export var KeyringAccountEntropyTypeOption; (function (KeyringAccountEntropyTypeOption) { /** * Indicates that the account was created from a mnemonic phrase. */ KeyringAccountEntropyTypeOption["Mnemonic"] = "mnemonic"; /** * Indicates that the account was imported from a private key. */ KeyringAccountEntropyTypeOption["PrivateKey"] = "private-key"; })(KeyringAccountEntropyTypeOption || (KeyringAccountEntropyTypeOption = {})); /** * Keyring account options struct for mnemonics (BIP-44). */ export const KeyringAccountEntropyMnemonicOptionsStruct = object({ /** * Indicates that the account was created from a mnemonic phrase. */ type: literal(`${KeyringAccountEntropyTypeOption.Mnemonic}`), /** * The ID of the entropy source. */ id: string(), // TODO: Define a struct for entropy source. /** * The BIP-44 derivation path used to derive the account. */ derivationPath: string(), /** * Index used to group accounts in the UI. * * Accounts sharing the same `groupIndex` are displayed together as a * multichain account group. */ groupIndex: number(), }); /** * Keyring account options struct for private keys. */ export const KeyringAccountEntropyPrivateKeyOptionsStruct = object({ /** * Indicates that the account was imported from a private key. */ type: literal(`${KeyringAccountEntropyTypeOption.PrivateKey}`), }); /** * Keyring account entropy options struct. */ export const KeyringAccountEntropyOptionsStruct = selectiveUnion((value) => { return isPlainObject(value) && value.type === KeyringAccountEntropyTypeOption.PrivateKey ? KeyringAccountEntropyPrivateKeyOptionsStruct : KeyringAccountEntropyMnemonicOptionsStruct; }); /** * Keyring options struct. This represents various options for a Keyring account object. * * See {@link KeyringAccountEntropyMnemonicOptionsStruct} and * {@link KeyringAccountEntropyPrivateKeyOptionsStruct}. * * @example * ```ts * { * entropy: { * type: 'mnemonic', * id: '01K0BX6VDR5DPDPGGNA8PZVBVB', * derivationPath: "m/44'/60'/0'/0/0", * groupIndex: 0, * }, * } * ``` * * @example * ```ts * { * entropy: { * type: 'private-key', * }, * exportable: true, * } * ``` * * @example * ```ts * { * some: { * untyped: 'options', * something: true, * }, * } * ``` */ export const KeyringAccountOptionsStruct = intersection([ // Non-Typed options (retro-compatibility): record(string(), JsonStruct), // Typed options. We use `type` instead of `object` here, to allow // extra properties. Also, since we use `record` + `intersection` we // are guaranteed that all field values will match the `JsonStruct`. // // READ THIS CAREFULLY: // Previous options that can be matched by this struct will be breaking // existing keyring account options. type({ /** * Entropy options. */ entropy: exactOptional(KeyringAccountEntropyOptionsStruct), /** * Indicates whether the account can be exported. */ exportable: exactOptional(boolean()), }), ]); //# sourceMappingURL=account-options.mjs.map