|
| 1 | +import type { KeyringAccount } from '@metamask/keyring-api'; |
| 2 | +import { |
| 3 | + BtcAccountType, |
| 4 | + BtcMethod, |
| 5 | + BtcScope, |
| 6 | + EthAccountType, |
| 7 | + EthMethod, |
| 8 | + EthScope, |
| 9 | + SolScope, |
| 10 | +} from '@metamask/keyring-api'; |
| 11 | + |
| 12 | +import type { Bip44Account } from './bip44'; |
| 13 | +import type { AccountSelector } from './selector'; |
| 14 | +import { select, selectOne } from './selector'; |
| 15 | +import { |
| 16 | + MOCK_WALLET_1_BTC_P2TR_ACCOUNT, |
| 17 | + MOCK_WALLET_1_BTC_P2WPKH_ACCOUNT, |
| 18 | + MOCK_WALLET_1_EVM_ACCOUNT, |
| 19 | + MOCK_WALLET_1_SOL_ACCOUNT, |
| 20 | +} from '../mocks'; |
| 21 | + |
| 22 | +describe('selector', () => { |
| 23 | + const accounts = [ |
| 24 | + MOCK_WALLET_1_EVM_ACCOUNT, |
| 25 | + MOCK_WALLET_1_SOL_ACCOUNT, |
| 26 | + MOCK_WALLET_1_BTC_P2WPKH_ACCOUNT, |
| 27 | + MOCK_WALLET_1_BTC_P2TR_ACCOUNT, |
| 28 | + ]; |
| 29 | + |
| 30 | + describe('selectOne', () => { |
| 31 | + it.each([ |
| 32 | + { |
| 33 | + tc: 'using id', |
| 34 | + selector: { id: MOCK_WALLET_1_EVM_ACCOUNT.id }, |
| 35 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 36 | + }, |
| 37 | + { |
| 38 | + tc: 'using address', |
| 39 | + selector: { address: MOCK_WALLET_1_SOL_ACCOUNT.address }, |
| 40 | + expected: MOCK_WALLET_1_SOL_ACCOUNT, |
| 41 | + }, |
| 42 | + { |
| 43 | + tc: 'using type', |
| 44 | + selector: { type: MOCK_WALLET_1_EVM_ACCOUNT.type }, |
| 45 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 46 | + }, |
| 47 | + { |
| 48 | + tc: 'using scope', |
| 49 | + selector: { scopes: [SolScope.Mainnet] }, |
| 50 | + expected: MOCK_WALLET_1_SOL_ACCOUNT, |
| 51 | + }, |
| 52 | + { |
| 53 | + tc: 'using another scope (but still included in the list of account.scopes)', |
| 54 | + selector: { scopes: [SolScope.Testnet] }, |
| 55 | + expected: MOCK_WALLET_1_SOL_ACCOUNT, |
| 56 | + }, |
| 57 | + { |
| 58 | + tc: 'using specific EVM chain still matches with EVM EOA scopes', |
| 59 | + selector: { scopes: [EthScope.Testnet] }, |
| 60 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 61 | + }, |
| 62 | + { |
| 63 | + tc: 'using multiple scopes', |
| 64 | + selector: { scopes: [SolScope.Mainnet, SolScope.Testnet] }, |
| 65 | + expected: MOCK_WALLET_1_SOL_ACCOUNT, |
| 66 | + }, |
| 67 | + { |
| 68 | + tc: 'using method', |
| 69 | + selector: { methods: [EthMethod.SignTransaction] }, |
| 70 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 71 | + }, |
| 72 | + { |
| 73 | + tc: 'using another method', |
| 74 | + selector: { methods: [EthMethod.PersonalSign] }, |
| 75 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 76 | + }, |
| 77 | + { |
| 78 | + tc: 'using multiple methods', |
| 79 | + selector: { |
| 80 | + methods: [EthMethod.SignTransaction, EthMethod.PersonalSign], |
| 81 | + }, |
| 82 | + expected: MOCK_WALLET_1_EVM_ACCOUNT, |
| 83 | + }, |
| 84 | + ])( |
| 85 | + 'gets internal account from selector: $tc', |
| 86 | + async ({ selector, expected }) => { |
| 87 | + expect(selectOne(accounts, selector)).toStrictEqual(expected); |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + it.each([ |
| 92 | + { |
| 93 | + tc: 'using non-matching id', |
| 94 | + selector: { id: '66da96d7-8f24-4895-82d6-183d740c2da1' }, |
| 95 | + }, |
| 96 | + { |
| 97 | + tc: 'using non-matching address', |
| 98 | + selector: { address: 'unknown-address' }, |
| 99 | + }, |
| 100 | + { |
| 101 | + tc: 'using non-matching type', |
| 102 | + selector: { type: 'unknown-type' }, |
| 103 | + }, |
| 104 | + { |
| 105 | + tc: 'using non-matching scope', |
| 106 | + selector: { |
| 107 | + scopes: ['bip122:12a765e31ffd4059bada1e25190f6e98' /* Litecoin */], |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + tc: 'using non-matching method', |
| 112 | + selector: { methods: ['eth_unknownMethod'] }, |
| 113 | + }, |
| 114 | + ] as { |
| 115 | + tc: string; |
| 116 | + selector: AccountSelector<Bip44Account<KeyringAccount>>; |
| 117 | + }[])( |
| 118 | + 'gets undefined if not matching selector: $tc', |
| 119 | + async ({ selector }) => { |
| 120 | + expect(selectOne(accounts, selector)).toBeUndefined(); |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + it('matches account when using empty scopes', () => { |
| 125 | + const mockAccountWithNoScopes = { |
| 126 | + ...MOCK_WALLET_1_EVM_ACCOUNT, |
| 127 | + scopes: [], |
| 128 | + }; |
| 129 | + |
| 130 | + expect( |
| 131 | + selectOne([mockAccountWithNoScopes], { scopes: [] }), |
| 132 | + ).toStrictEqual(mockAccountWithNoScopes); |
| 133 | + }); |
| 134 | + |
| 135 | + it('matches account when using empty methods', () => { |
| 136 | + const mockAccountWithNoMethods = { |
| 137 | + ...MOCK_WALLET_1_EVM_ACCOUNT, |
| 138 | + methods: [], |
| 139 | + }; |
| 140 | + |
| 141 | + expect( |
| 142 | + selectOne([mockAccountWithNoMethods], { methods: [] }), |
| 143 | + ).toStrictEqual(mockAccountWithNoMethods); |
| 144 | + }); |
| 145 | + |
| 146 | + it('throws if multiple candidates are found', async () => { |
| 147 | + const selector = { |
| 148 | + scopes: [EthScope.Mainnet, SolScope.Mainnet], |
| 149 | + }; |
| 150 | + |
| 151 | + expect(() => selectOne(accounts, selector)).toThrow( |
| 152 | + 'Too many account candidates, expected 1, got: 2', |
| 153 | + ); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('select', () => { |
| 158 | + it.each([ |
| 159 | + { |
| 160 | + tc: 'using id', |
| 161 | + selector: { id: MOCK_WALLET_1_EVM_ACCOUNT.id }, |
| 162 | + expected: [MOCK_WALLET_1_EVM_ACCOUNT], |
| 163 | + }, |
| 164 | + { |
| 165 | + tc: 'using non-matching id', |
| 166 | + selector: { id: '66da96d7-8f24-4895-82d6-183d740c2da1' }, |
| 167 | + expected: [], |
| 168 | + }, |
| 169 | + { |
| 170 | + tc: 'using address', |
| 171 | + selector: { address: MOCK_WALLET_1_SOL_ACCOUNT.address }, |
| 172 | + expected: [MOCK_WALLET_1_SOL_ACCOUNT], |
| 173 | + }, |
| 174 | + { |
| 175 | + tc: 'using non-matching address', |
| 176 | + selector: { address: 'unknown-address' }, |
| 177 | + expected: [], |
| 178 | + }, |
| 179 | + { |
| 180 | + tc: 'using type', |
| 181 | + selector: { type: MOCK_WALLET_1_EVM_ACCOUNT.type }, |
| 182 | + expected: [MOCK_WALLET_1_EVM_ACCOUNT], |
| 183 | + }, |
| 184 | + { |
| 185 | + tc: 'using non-matching type', |
| 186 | + selector: { type: 'unknown-type' }, |
| 187 | + expected: [], |
| 188 | + }, |
| 189 | + { |
| 190 | + tc: 'using scope', |
| 191 | + selector: { scopes: [SolScope.Mainnet] }, |
| 192 | + expected: [MOCK_WALLET_1_SOL_ACCOUNT], |
| 193 | + }, |
| 194 | + { |
| 195 | + tc: 'using another scope (but still included in the list of account.scopes)', |
| 196 | + selector: { scopes: [SolScope.Testnet] }, |
| 197 | + expected: [MOCK_WALLET_1_SOL_ACCOUNT], |
| 198 | + }, |
| 199 | + { |
| 200 | + tc: 'using specific EVM chain still matches with EVM EOA scopes', |
| 201 | + selector: { scopes: [EthScope.Testnet] }, |
| 202 | + expected: [MOCK_WALLET_1_EVM_ACCOUNT], |
| 203 | + }, |
| 204 | + { |
| 205 | + tc: 'using multiple scopes', |
| 206 | + selector: { scopes: [BtcScope.Mainnet, BtcScope.Testnet] }, |
| 207 | + expected: [ |
| 208 | + MOCK_WALLET_1_BTC_P2WPKH_ACCOUNT, |
| 209 | + MOCK_WALLET_1_BTC_P2TR_ACCOUNT, |
| 210 | + ], |
| 211 | + }, |
| 212 | + { |
| 213 | + tc: 'using non-matching scopes', |
| 214 | + selector: { |
| 215 | + scopes: ['bip122:12a765e31ffd4059bada1e25190f6e98' /* Litecoin */], |
| 216 | + }, |
| 217 | + expected: [], |
| 218 | + }, |
| 219 | + { |
| 220 | + tc: 'using method', |
| 221 | + selector: { methods: [BtcMethod.SendBitcoin] }, |
| 222 | + expected: [ |
| 223 | + MOCK_WALLET_1_BTC_P2WPKH_ACCOUNT, |
| 224 | + MOCK_WALLET_1_BTC_P2TR_ACCOUNT, |
| 225 | + ], |
| 226 | + }, |
| 227 | + { |
| 228 | + tc: 'using multiple methods', |
| 229 | + selector: { |
| 230 | + methods: [EthMethod.SignTransaction, EthMethod.PersonalSign], |
| 231 | + }, |
| 232 | + expected: [MOCK_WALLET_1_EVM_ACCOUNT], |
| 233 | + }, |
| 234 | + { |
| 235 | + tc: 'using non-matching method', |
| 236 | + selector: { methods: ['eth_unknownMethod'] }, |
| 237 | + expected: [], |
| 238 | + }, |
| 239 | + { |
| 240 | + tc: 'using multiple selectors', |
| 241 | + selector: { |
| 242 | + type: EthAccountType.Eoa, |
| 243 | + methods: [EthMethod.SignTransaction, EthMethod.PersonalSign], |
| 244 | + }, |
| 245 | + expected: [MOCK_WALLET_1_EVM_ACCOUNT], |
| 246 | + }, |
| 247 | + { |
| 248 | + tc: 'using non-matching selectors', |
| 249 | + selector: { |
| 250 | + type: BtcAccountType.P2wpkh, |
| 251 | + methods: [EthMethod.SignTransaction, EthMethod.PersonalSign], |
| 252 | + }, |
| 253 | + expected: [], |
| 254 | + }, |
| 255 | + ] as { |
| 256 | + tc: string; |
| 257 | + selector: AccountSelector<Bip44Account<KeyringAccount>>; |
| 258 | + expected: Bip44Account<KeyringAccount>[]; |
| 259 | + }[])( |
| 260 | + 'selects internal accounts from selector: $tc', |
| 261 | + async ({ selector, expected }) => { |
| 262 | + expect(select(accounts, selector)).toStrictEqual(expected); |
| 263 | + }, |
| 264 | + ); |
| 265 | + }); |
| 266 | +}); |
0 commit comments