|
| 1 | +import type { Caip25CaveatValue } from '../caip25Permission'; |
| 2 | +import { |
| 3 | + getEthAccounts, |
| 4 | + setEthAccounts, |
| 5 | +} from './caip-permission-adapter-eth-accounts'; |
| 6 | + |
| 7 | +describe('CAIP-25 eth_accounts adapters', () => { |
| 8 | + describe('getEthAccounts', () => { |
| 9 | + it('returns an empty array if the required scopes are empty', () => { |
| 10 | + const ethAccounts = getEthAccounts({ |
| 11 | + requiredScopes: {}, |
| 12 | + optionalScopes: {}, |
| 13 | + }); |
| 14 | + expect(ethAccounts).toStrictEqual([]); |
| 15 | + }); |
| 16 | + it('returns an empty array if the scope objects have no accounts', () => { |
| 17 | + const ethAccounts = getEthAccounts({ |
| 18 | + requiredScopes: { |
| 19 | + 'eip155:1': { methods: [], notifications: [], accounts: [] }, |
| 20 | + 'eip155:2': { methods: [], notifications: [], accounts: [] }, |
| 21 | + }, |
| 22 | + optionalScopes: {}, |
| 23 | + }); |
| 24 | + expect(ethAccounts).toStrictEqual([]); |
| 25 | + }); |
| 26 | + it('returns an empty array if the scope objects have no eth accounts', () => { |
| 27 | + const ethAccounts = getEthAccounts({ |
| 28 | + requiredScopes: { |
| 29 | + 'bip122:000000000019d6689c085ae165831e93': { |
| 30 | + methods: [], |
| 31 | + notifications: [], |
| 32 | + accounts: [ |
| 33 | + 'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', |
| 34 | + ], |
| 35 | + }, |
| 36 | + }, |
| 37 | + optionalScopes: {}, |
| 38 | + }); |
| 39 | + expect(ethAccounts).toStrictEqual([]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns the unique set of EIP155 accounts from the CAIP-25 caveat value', () => { |
| 43 | + const ethAccounts = getEthAccounts({ |
| 44 | + requiredScopes: { |
| 45 | + 'eip155:1': { |
| 46 | + methods: [], |
| 47 | + notifications: [], |
| 48 | + accounts: ['eip155:1:0x1', 'eip155:1:0x2'], |
| 49 | + }, |
| 50 | + 'eip155:5': { |
| 51 | + methods: [], |
| 52 | + notifications: [], |
| 53 | + accounts: ['eip155:5:0x2', 'eip155:1:0x3'], |
| 54 | + }, |
| 55 | + 'bip122:000000000019d6689c085ae165831e93': { |
| 56 | + methods: [], |
| 57 | + notifications: [], |
| 58 | + accounts: [ |
| 59 | + 'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', |
| 60 | + ], |
| 61 | + }, |
| 62 | + }, |
| 63 | + optionalScopes: { |
| 64 | + 'eip155:1': { |
| 65 | + methods: [], |
| 66 | + notifications: [], |
| 67 | + accounts: ['eip155:1:0x1', 'eip155:1:0x4'], |
| 68 | + }, |
| 69 | + 'eip155:10': { |
| 70 | + methods: [], |
| 71 | + notifications: [], |
| 72 | + accounts: [], |
| 73 | + }, |
| 74 | + 'eip155:100': { |
| 75 | + methods: [], |
| 76 | + notifications: [], |
| 77 | + accounts: ['eip155:100:0x100'], |
| 78 | + }, |
| 79 | + 'wallet:eip155': { |
| 80 | + methods: [], |
| 81 | + notifications: [], |
| 82 | + accounts: ['wallet:eip155:0x5'], |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(ethAccounts).toStrictEqual([ |
| 88 | + '0x1', |
| 89 | + '0x2', |
| 90 | + '0x4', |
| 91 | + '0x3', |
| 92 | + '0x100', |
| 93 | + '0x5', |
| 94 | + ]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('setEthAccounts', () => { |
| 99 | + it('returns a CAIP-25 caveat value with all EIP-155 scopeObject.accounts set to CAIP-10 account addresses formed from the accounts param', () => { |
| 100 | + const input: Caip25CaveatValue = { |
| 101 | + requiredScopes: { |
| 102 | + 'eip155:1': { |
| 103 | + methods: [], |
| 104 | + notifications: [], |
| 105 | + accounts: ['eip155:1:0x1', 'eip155:1:0x2'], |
| 106 | + }, |
| 107 | + 'eip155:5': { |
| 108 | + methods: [], |
| 109 | + notifications: [], |
| 110 | + accounts: ['eip155:5:0x2', 'eip155:1:0x3'], |
| 111 | + }, |
| 112 | + 'bip122:000000000019d6689c085ae165831e93': { |
| 113 | + methods: [], |
| 114 | + notifications: [], |
| 115 | + accounts: [ |
| 116 | + 'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', |
| 117 | + ], |
| 118 | + }, |
| 119 | + }, |
| 120 | + optionalScopes: { |
| 121 | + 'eip155:1': { |
| 122 | + methods: [], |
| 123 | + notifications: [], |
| 124 | + accounts: ['eip155:1:0x1', 'eip155:1:0x4'], |
| 125 | + }, |
| 126 | + 'eip155:10': { |
| 127 | + methods: [], |
| 128 | + notifications: [], |
| 129 | + accounts: [], |
| 130 | + }, |
| 131 | + 'eip155:100': { |
| 132 | + methods: [], |
| 133 | + notifications: [], |
| 134 | + accounts: ['eip155:100:0x100'], |
| 135 | + }, |
| 136 | + 'wallet:eip155': { |
| 137 | + methods: [], |
| 138 | + notifications: [], |
| 139 | + accounts: [], |
| 140 | + }, |
| 141 | + wallet: { |
| 142 | + methods: [], |
| 143 | + notifications: [], |
| 144 | + accounts: [], |
| 145 | + }, |
| 146 | + }, |
| 147 | + isMultichainOrigin: false, |
| 148 | + }; |
| 149 | + |
| 150 | + const result = setEthAccounts(input, ['0x1', '0x2', '0x3']); |
| 151 | + expect(result).toStrictEqual({ |
| 152 | + requiredScopes: { |
| 153 | + 'eip155:1': { |
| 154 | + methods: [], |
| 155 | + notifications: [], |
| 156 | + accounts: ['eip155:1:0x1', 'eip155:1:0x2', 'eip155:1:0x3'], |
| 157 | + }, |
| 158 | + 'eip155:5': { |
| 159 | + methods: [], |
| 160 | + notifications: [], |
| 161 | + accounts: ['eip155:5:0x1', 'eip155:5:0x2', 'eip155:5:0x3'], |
| 162 | + }, |
| 163 | + 'bip122:000000000019d6689c085ae165831e93': { |
| 164 | + methods: [], |
| 165 | + notifications: [], |
| 166 | + accounts: [ |
| 167 | + 'bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6', |
| 168 | + ], |
| 169 | + }, |
| 170 | + }, |
| 171 | + optionalScopes: { |
| 172 | + 'eip155:1': { |
| 173 | + methods: [], |
| 174 | + notifications: [], |
| 175 | + accounts: ['eip155:1:0x1', 'eip155:1:0x2', 'eip155:1:0x3'], |
| 176 | + }, |
| 177 | + 'eip155:10': { |
| 178 | + methods: [], |
| 179 | + notifications: [], |
| 180 | + accounts: ['eip155:10:0x1', 'eip155:10:0x2', 'eip155:10:0x3'], |
| 181 | + }, |
| 182 | + 'eip155:100': { |
| 183 | + methods: [], |
| 184 | + notifications: [], |
| 185 | + accounts: ['eip155:100:0x1', 'eip155:100:0x2', 'eip155:100:0x3'], |
| 186 | + }, |
| 187 | + 'wallet:eip155': { |
| 188 | + methods: [], |
| 189 | + notifications: [], |
| 190 | + accounts: [ |
| 191 | + 'wallet:eip155:0x1', |
| 192 | + 'wallet:eip155:0x2', |
| 193 | + 'wallet:eip155:0x3', |
| 194 | + ], |
| 195 | + }, |
| 196 | + wallet: { |
| 197 | + methods: [], |
| 198 | + notifications: [], |
| 199 | + accounts: [ |
| 200 | + 'wallet:eip155:0x1', |
| 201 | + 'wallet:eip155:0x2', |
| 202 | + 'wallet:eip155:0x3', |
| 203 | + ], |
| 204 | + }, |
| 205 | + }, |
| 206 | + isMultichainOrigin: false, |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + it('returns a CAIP-25 caveat value with missing "wallet:eip155" optional scope filled in, forming CAIP-10 account addresses from the accounts param', () => { |
| 211 | + const input: Caip25CaveatValue = { |
| 212 | + requiredScopes: {}, |
| 213 | + optionalScopes: {}, |
| 214 | + isMultichainOrigin: false, |
| 215 | + }; |
| 216 | + |
| 217 | + const result = setEthAccounts(input, ['0x1', '0x2', '0x3']); |
| 218 | + expect(result).toStrictEqual({ |
| 219 | + requiredScopes: {}, |
| 220 | + optionalScopes: { |
| 221 | + 'wallet:eip155': { |
| 222 | + methods: [], |
| 223 | + notifications: [], |
| 224 | + accounts: [ |
| 225 | + 'wallet:eip155:0x1', |
| 226 | + 'wallet:eip155:0x2', |
| 227 | + 'wallet:eip155:0x3', |
| 228 | + ], |
| 229 | + }, |
| 230 | + }, |
| 231 | + isMultichainOrigin: false, |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + it('does not modify the input CAIP-25 caveat value object in place', () => { |
| 236 | + const input: Caip25CaveatValue = { |
| 237 | + requiredScopes: { |
| 238 | + 'eip155:1': { |
| 239 | + methods: [], |
| 240 | + notifications: [], |
| 241 | + accounts: [], |
| 242 | + }, |
| 243 | + }, |
| 244 | + optionalScopes: {}, |
| 245 | + isMultichainOrigin: false, |
| 246 | + }; |
| 247 | + |
| 248 | + const result = setEthAccounts(input, ['0x1', '0x2', '0x3']); |
| 249 | + expect(input).toStrictEqual({ |
| 250 | + requiredScopes: { |
| 251 | + 'eip155:1': { |
| 252 | + methods: [], |
| 253 | + notifications: [], |
| 254 | + accounts: [], |
| 255 | + }, |
| 256 | + }, |
| 257 | + optionalScopes: {}, |
| 258 | + isMultichainOrigin: false, |
| 259 | + }); |
| 260 | + expect(input).not.toStrictEqual(result); |
| 261 | + }); |
| 262 | + }); |
| 263 | +}); |
0 commit comments