UNPKG

@metamask/multichain-account-service

Version:
372 lines 12.5 kB
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 _MockAccountBuilder_account; import { isBip44Account } from "@metamask/account-api"; import { BtcAccountType, BtcMethod, BtcScope, EthAccountType, EthMethod, EthScope, KeyringAccountEntropyTypeOption, SolAccountType, SolMethod, SolScope, TrxAccountType, TrxMethod, TrxScope, XlmAccountType, XlmMethod, XlmScope } from "@metamask/keyring-api"; import { KeyringTypes } from "@metamask/keyring-controller"; import { v4 as uuid } from "uuid"; export const ETH_EOA_METHODS = [ EthMethod.PersonalSign, EthMethod.Sign, EthMethod.SignTransaction, EthMethod.SignTypedDataV1, EthMethod.SignTypedDataV3, EthMethod.SignTypedDataV4, ]; const SOL_METHODS = Object.values(SolMethod); export const MOCK_SNAP_1 = { id: 'local:mock-snap-id-1', name: 'Mock Snap 1', enabled: true, manifest: { proposedName: 'Mock Snap 1', }, }; export const MOCK_SNAP_2 = { id: 'local:mock-snap-id-2', name: 'Mock Snap 2', enabled: true, manifest: { proposedName: 'Mock Snap 2', }, }; export const MOCK_ENTROPY_SOURCE_1 = 'mock-keyring-id-1'; export const MOCK_ENTROPY_SOURCE_2 = 'mock-keyring-id-2'; export const MOCK_MNEMONIC = 'abandon ability able about above absent absorb abstract absurd abuse access accident'; export const MOCK_HD_KEYRING_1 = { type: KeyringTypes.hd, metadata: { id: MOCK_ENTROPY_SOURCE_1, name: 'HD Keyring 1' }, accounts: ['0x123'], }; export const MOCK_HD_KEYRING_2 = { type: KeyringTypes.hd, metadata: { id: MOCK_ENTROPY_SOURCE_2, name: 'HD Keyring 2' }, accounts: ['0x456'], }; /** Used when tests need ensureReady to resolve (SnapAccountService waits for Snap keyring). */ export const MOCK_SNAP_KEYRING = { type: KeyringTypes.snap, metadata: { id: 'snap-keyring', name: 'Snap Keyring' }, accounts: [], }; export const MOCK_HD_ACCOUNT_1 = { id: 'mock-id-1', address: '0x123', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, id: MOCK_HD_KEYRING_1.metadata.id, groupIndex: 0, derivationPath: '', }, }, methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, scopes: [EthScope.Eoa], metadata: { name: 'Account 1', keyring: { type: KeyringTypes.hd }, importTime: 0, lastSelected: 0, nameLastUpdatedAt: 0, }, }; export const MOCK_HD_ACCOUNT_2 = { id: 'mock-id-2', address: '0x456', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: '', }, }, methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, scopes: [EthScope.Eoa], metadata: { name: 'Account 2', keyring: { type: KeyringTypes.hd }, importTime: 0, lastSelected: 0, nameLastUpdatedAt: 0, }, }; export const MOCK_SOL_ACCOUNT_1 = { id: 'mock-snap-id-1', address: 'aabbccdd', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, // NOTE: shares entropy with MOCK_HD_ACCOUNT_2 id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: '', }, }, methods: SOL_METHODS, type: SolAccountType.DataAccount, scopes: [SolScope.Mainnet, SolScope.Testnet, SolScope.Devnet], metadata: { name: 'Solana Account 1', keyring: { type: KeyringTypes.snap }, snap: MOCK_SNAP_1, importTime: 0, lastSelected: 0, }, }; const XLM_METHODS = Object.values(XlmMethod); export const MOCK_XLM_ACCOUNT_1 = { id: 'mock-snap-id-1', address: `G${'A'.repeat(55)}`, options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: `m/44'/148'/0'`, }, }, methods: XLM_METHODS, type: XlmAccountType.Account, scopes: [XlmScope.Pubnet, XlmScope.Testnet], metadata: { name: 'Stellar Account 1', keyring: { type: KeyringTypes.snap }, snap: MOCK_SNAP_1, importTime: 0, lastSelected: 0, }, }; export const MOCK_TRX_ACCOUNT_1 = { id: 'mock-snap-id-1', address: 'aabbccdd', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, // NOTE: shares entropy with MOCK_HD_ACCOUNT_2 id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: '', }, }, methods: [TrxMethod.SignMessageV2, TrxMethod.VerifyMessageV2], type: TrxAccountType.Eoa, scopes: [TrxScope.Mainnet], metadata: { name: 'Tron Account 1', keyring: { type: KeyringTypes.snap }, snap: MOCK_SNAP_1, importTime: 0, lastSelected: 0, }, }; export const MOCK_SOL_DISCOVERED_ACCOUNT_1 = { type: 'bip44', scopes: [SolScope.Mainnet], derivationPath: `m/44'/501'/0'/0'`, }; export const MOCK_TRX_DISCOVERED_ACCOUNT_1 = { type: 'bip44', scopes: [TrxScope.Mainnet], derivationPath: `m/44'/195'/0'/0'`, }; export const MOCK_XLM_DISCOVERED_ACCOUNT_1 = { type: 'bip44', scopes: [XlmScope.Pubnet], derivationPath: `m/44'/148'/0'`, }; export const MOCK_BTC_P2TR_DISCOVERED_ACCOUNT_1 = { type: 'bip44', scopes: [BtcScope.Mainnet], derivationPath: `m/44'/0'/0'/0'`, }; export const MOCK_BTC_P2WPKH_ACCOUNT_1 = { id: 'b0f030d8-e101-4b5a-a3dd-13f8ca8ec1db', type: BtcAccountType.P2wpkh, methods: Object.values(BtcMethod), address: 'bc1qx8ls07cy8j8nrluy2u0xwn7gh8fxg0rg4s8zze', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, // NOTE: shares entropy with MOCK_HD_ACCOUNT_2 id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: '', }, }, scopes: [BtcScope.Mainnet], metadata: { name: 'Bitcoin Native Segwit Account 1', importTime: 0, keyring: { type: KeyringTypes.snap, }, snap: { id: 'mock-btc-snap-id', }, }, }; export const MOCK_BTC_P2TR_ACCOUNT_1 = { id: 'a20c2e1a-6ff6-40ba-b8e0-ccdb6f9933bb', type: BtcAccountType.P2tr, methods: Object.values(BtcMethod), address: 'tb1p5cyxnuxmeuwuvkwfem96lxx9wex9kkf4mt9ll6q60jfsnrzqg4sszkqjnh', options: { entropy: { type: KeyringAccountEntropyTypeOption.Mnemonic, // NOTE: shares entropy with MOCK_HD_ACCOUNT_2 id: MOCK_HD_KEYRING_2.metadata.id, groupIndex: 0, derivationPath: '', }, }, scopes: [BtcScope.Testnet], metadata: { name: 'Bitcoin Taproot Account 1', importTime: 0, keyring: { type: KeyringTypes.snap, }, snap: { id: 'mock-btc-snap-id', }, }, }; export const MOCK_SNAP_ACCOUNT_1 = MOCK_SOL_ACCOUNT_1; export const MOCK_SNAP_ACCOUNT_2 = { id: 'mock-snap-id-2', address: '0x789', options: {}, methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, scopes: [EthScope.Eoa], metadata: { name: 'Snap Acc 2', keyring: { type: KeyringTypes.snap }, snap: MOCK_SNAP_2, importTime: 0, lastSelected: 0, }, }; export const MOCK_SNAP_ACCOUNT_3 = MOCK_BTC_P2WPKH_ACCOUNT_1; export const MOCK_SNAP_ACCOUNT_4 = MOCK_BTC_P2TR_ACCOUNT_1; export const MOCK_HARDWARE_ACCOUNT_1 = { id: 'mock-hardware-id-1', address: '0xABC', options: {}, methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, scopes: [EthScope.Eoa], metadata: { name: 'Hardware Acc 1', keyring: { type: KeyringTypes.ledger }, importTime: 0, lastSelected: 0, }, }; export function isInternalAccount(account) { // Meant to be used for testing, so we keep this simple. return Object.prototype.hasOwnProperty.call(account, 'metadata'); } export function asKeyringAccount(account) { const { id, type, address, scopes, options, methods } = account; return { id, type, address, scopes, options, methods, }; } export class MockAccountBuilder { constructor(account) { _MockAccountBuilder_account.set(this, void 0); // Make a deep-copy to avoid mutating the same ref. __classPrivateFieldSet(this, _MockAccountBuilder_account, JSON.parse(JSON.stringify(account)), "f"); } static from(account) { return new MockAccountBuilder(account); } withId(id) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").id = id; return this; } withUuid() { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").id = uuid(); return this; } withAddress(address) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").address = address; return this; } withAddressSuffix(suffix) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").address += suffix; return this; } withEntropySource(entropySource) { if (isBip44Account(__classPrivateFieldGet(this, _MockAccountBuilder_account, "f"))) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").options.entropy.id = entropySource; } return this; } withGroupIndex(groupIndex) { if (isBip44Account(__classPrivateFieldGet(this, _MockAccountBuilder_account, "f"))) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").options.entropy.groupIndex = groupIndex; } return this; } withSnapId(snapId) { if (isInternalAccount(__classPrivateFieldGet(this, _MockAccountBuilder_account, "f"))) { __classPrivateFieldGet(this, _MockAccountBuilder_account, "f").metadata.snap = { id: snapId, }; } return this; } get() { return __classPrivateFieldGet(this, _MockAccountBuilder_account, "f"); } } _MockAccountBuilder_account = new WeakMap(); export const MOCK_WALLET_1_ENTROPY_SOURCE = MOCK_ENTROPY_SOURCE_1; export const MOCK_WALLET_1_EVM_ACCOUNT = MockAccountBuilder.from(MOCK_HD_ACCOUNT_1) .withEntropySource(MOCK_WALLET_1_ENTROPY_SOURCE) .withGroupIndex(0) .get(); export const MOCK_WALLET_1_SOL_ACCOUNT = MockAccountBuilder.from(MOCK_SOL_ACCOUNT_1) .withEntropySource(MOCK_WALLET_1_ENTROPY_SOURCE) .withGroupIndex(0) .get(); export const MOCK_WALLET_1_BTC_P2WPKH_ACCOUNT = MockAccountBuilder.from(MOCK_BTC_P2WPKH_ACCOUNT_1) .withEntropySource(MOCK_WALLET_1_ENTROPY_SOURCE) .withGroupIndex(0) .get(); export const MOCK_WALLET_1_BTC_P2TR_ACCOUNT = MockAccountBuilder.from(MOCK_BTC_P2TR_ACCOUNT_1) .withEntropySource(MOCK_WALLET_1_ENTROPY_SOURCE) .withGroupIndex(0) .get(); export function mockAsInternalAccount(account) { return { ...account, metadata: { name: 'Mocked Account', importTime: Date.now(), keyring: { type: 'mock-keyring-type', }, }, }; } //# sourceMappingURL=accounts.mjs.map