accounts 0.15.3 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { Base64 } from 'ox'
2
2
  import { KeyAuthorization } from 'ox/tempo'
3
- import { Account as TempoAccount } from 'viem/tempo'
3
+ import { createClient, custom, encodeErrorResult, encodeFunctionResult } from 'viem'
4
+ import { Abis, Account as TempoAccount } from 'viem/tempo'
4
5
  import { describe, expect, test } from 'vp/test'
5
6
  import * as z from 'zod/mini'
6
7
 
@@ -22,6 +23,23 @@ const limits = [
22
23
  },
23
24
  ] as const
24
25
 
26
+ function createMissingAccessKeyClient() {
27
+ return createClient({
28
+ chain,
29
+ transport: custom({
30
+ async request() {
31
+ throw Object.assign(new Error('reverted'), {
32
+ data: encodeErrorResult({
33
+ abi: Abis.abis,
34
+ errorName: 'KeyNotFound',
35
+ args: [],
36
+ } as never),
37
+ })
38
+ },
39
+ }),
40
+ })
41
+ }
42
+
25
43
  async function authorize(
26
44
  code: string,
27
45
  options: {
@@ -175,7 +193,7 @@ async function get<response extends z.ZodMiniType>(
175
193
  }
176
194
 
177
195
  describe('from', () => {
178
- test('default: shares defaults across the device-code flow', async () => {
196
+ test('default: shares defaults across the device code flow', async () => {
179
197
  const store = CliAuth.Store.memory()
180
198
  const now = () => 1_000
181
199
  const { codeVerifier, request } = await createRequest()
@@ -274,6 +292,145 @@ describe('createDeviceCode', () => {
274
292
  `)
275
293
  })
276
294
 
295
+ test('behavior: creates an access key update request', async () => {
296
+ const store = CliAuth.Store.memory()
297
+ const codeVerifier = 'update-device-code-verifier'
298
+ const result = await CliAuth.createDeviceCode({
299
+ chainId: chain.id,
300
+ random: () => new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]),
301
+ request: {
302
+ action: 'updateAccessKey',
303
+ accessKeyAddress: accessKey.address,
304
+ account: root.address,
305
+ codeChallenge: await createCodeChallenge(codeVerifier),
306
+ limits,
307
+ },
308
+ store,
309
+ })
310
+
311
+ expect(await CliAuth.pending({ code: result.code, store })).toMatchInlineSnapshot(`
312
+ {
313
+ "accessKeyAddress": "${accessKey.address}",
314
+ "account": "${root.address}",
315
+ "action": "updateAccessKey",
316
+ "chainId": 1337n,
317
+ "code": "ABCDEFGH",
318
+ "limits": [
319
+ {
320
+ "limit": 1000n,
321
+ "token": "0x20c0000000000000000000000000000000000001",
322
+ },
323
+ ],
324
+ "status": "pending",
325
+ }
326
+ `)
327
+ })
328
+
329
+ test('behavior: handler uses its configured client to verify pending authorization', async () => {
330
+ const calls: string[] = []
331
+ const original = (
332
+ await authorizeWebAuthn('unused', {
333
+ accessKey,
334
+ limits: [{ limit: 1n, token }],
335
+ })
336
+ ).keyAuthorization
337
+ const handler = Handler.codeAuth({
338
+ chains: [chain],
339
+ policy: {
340
+ update({ limits }) {
341
+ return { limits }
342
+ },
343
+ validate({ expiry, limits }) {
344
+ return { expiry: expiry ?? 0, ...(limits ? { limits } : {}) }
345
+ },
346
+ },
347
+ transports: {
348
+ [chain.id]: custom({
349
+ async request({ method }) {
350
+ calls.push(method)
351
+ throw new Error('Configured transport used.')
352
+ },
353
+ }),
354
+ },
355
+ })
356
+
357
+ const result = await post(handler, {
358
+ body: {
359
+ action: 'updateAccessKey',
360
+ accessKeyAddress: accessKey.address,
361
+ account: webAuthnRoot.address,
362
+ chainId: BigInt(chain.id),
363
+ codeChallenge: await createCodeChallenge('pending-update-device-code-verifier'),
364
+ keyAuthorization: original,
365
+ limits,
366
+ },
367
+ request: CliAuth.createRequest,
368
+ url: 'http://localhost/auth/pkce/code',
369
+ })
370
+
371
+ expect({ calls, status: result.status }).toMatchInlineSnapshot(`
372
+ {
373
+ "calls": [
374
+ "eth_getCode",
375
+ "eth_getCode",
376
+ "eth_getCode",
377
+ "eth_getCode",
378
+ ],
379
+ "status": 400,
380
+ }
381
+ `)
382
+ })
383
+
384
+ test('behavior: requires an explicit update policy', async () => {
385
+ await expect(
386
+ CliAuth.createDeviceCode({
387
+ chainId: chain.id,
388
+ policy: {
389
+ validate({ expiry: requestedExpiry, limits: requestedLimits }) {
390
+ return { expiry: requestedExpiry ?? expiry, limits: requestedLimits }
391
+ },
392
+ },
393
+ request: {
394
+ action: 'updateAccessKey',
395
+ accessKeyAddress: accessKey.address,
396
+ account: root.address,
397
+ codeChallenge: await createCodeChallenge('update-device-code-verifier'),
398
+ limits,
399
+ },
400
+ }),
401
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
402
+ `[Error: Access key updates are not supported by this policy.]`,
403
+ )
404
+ })
405
+
406
+ test('behavior: rejects duplicate access key update tokens', async () => {
407
+ await expect(
408
+ CliAuth.createDeviceCode({
409
+ chainId: chain.id,
410
+ policy: {
411
+ update({ limits }) {
412
+ return { limits }
413
+ },
414
+ validate({ expiry, limits }) {
415
+ return { expiry: expiry ?? 0, ...(limits ? { limits } : {}) }
416
+ },
417
+ },
418
+ request: {
419
+ action: 'updateAccessKey',
420
+ accessKeyAddress: accessKey.address,
421
+ account: root.address,
422
+ codeChallenge: await createCodeChallenge('update-device-code-verifier'),
423
+ limits: [
424
+ { limit: 1n, token },
425
+ { limit: 2n, token: `0x${token.slice(2).toUpperCase()}` as typeof token },
426
+ ],
427
+ },
428
+ }),
429
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
430
+ `[Error: Access key update limits must use unique tokens.]`,
431
+ )
432
+ })
433
+
277
434
  test('behavior: policy rejection returns an error from the handler', async () => {
278
435
  const { request } = await createRequest()
279
436
  const handler = Handler.codeAuth({
@@ -435,7 +592,77 @@ describe('createDeviceCode', () => {
435
592
  const body = await response.json()
436
593
 
437
594
  expect(body.error).toMatchInlineSnapshot(`
438
- "[\n {\n "expected": "string",\n "code": "invalid_type",\n "path": [\n "codeChallenge"\n ],\n "message": "Invalid input"\n },\n {\n "expected": "string",\n "code": "invalid_type",\n "path": [\n "pubKey"\n ],\n "message": "Expected hex value"\n }\n]"
595
+ "[
596
+ {
597
+ "code": "invalid_union",
598
+ "errors": [
599
+ [
600
+ {
601
+ "expected": "string",
602
+ "code": "invalid_type",
603
+ "path": [
604
+ "codeChallenge"
605
+ ],
606
+ "message": "Invalid input"
607
+ },
608
+ {
609
+ "expected": "string",
610
+ "code": "invalid_type",
611
+ "path": [
612
+ "pubKey"
613
+ ],
614
+ "message": "Expected hex value"
615
+ }
616
+ ],
617
+ [
618
+ {
619
+ "code": "invalid_value",
620
+ "values": [
621
+ "updateAccessKey"
622
+ ],
623
+ "path": [
624
+ "action"
625
+ ],
626
+ "message": "Invalid input"
627
+ },
628
+ {
629
+ "expected": "string",
630
+ "code": "invalid_type",
631
+ "path": [
632
+ "accessKeyAddress"
633
+ ],
634
+ "message": "Expected address"
635
+ },
636
+ {
637
+ "expected": "string",
638
+ "code": "invalid_type",
639
+ "path": [
640
+ "account"
641
+ ],
642
+ "message": "Expected address"
643
+ },
644
+ {
645
+ "expected": "string",
646
+ "code": "invalid_type",
647
+ "path": [
648
+ "codeChallenge"
649
+ ],
650
+ "message": "Invalid input"
651
+ },
652
+ {
653
+ "expected": "array",
654
+ "code": "invalid_type",
655
+ "path": [
656
+ "limits"
657
+ ],
658
+ "message": "Invalid input"
659
+ }
660
+ ]
661
+ ],
662
+ "path": [],
663
+ "message": "Invalid input"
664
+ }
665
+ ]"
439
666
  `)
440
667
  expect(response.status).toMatchInlineSnapshot(`400`)
441
668
  })
@@ -544,22 +771,70 @@ Received 2 bytes.]`)
544
771
  })
545
772
 
546
773
  expect(result).toMatchInlineSnapshot(`
547
- {
548
- "error": [$ZodError: [
549
- {
550
- "code": "invalid_format",
551
- "format": "template_literal",
552
- "pattern": "^0x[0-9a-fA-F]{40}$",
553
- "path": [
554
- "limits",
555
- 0,
556
- "token"
557
- ],
558
- "message": "Expected address"
559
- }
560
- ]],
561
- "success": false,
562
- }
774
+ {
775
+ "error": [$ZodError: [
776
+ {
777
+ "code": "invalid_union",
778
+ "errors": [
779
+ [
780
+ {
781
+ "code": "invalid_format",
782
+ "format": "template_literal",
783
+ "pattern": "^0x[0-9a-fA-F]{40}$",
784
+ "path": [
785
+ "limits",
786
+ 0,
787
+ "token"
788
+ ],
789
+ "message": "Expected address"
790
+ }
791
+ ],
792
+ [
793
+ {
794
+ "code": "invalid_value",
795
+ "values": [
796
+ "updateAccessKey"
797
+ ],
798
+ "path": [
799
+ "action"
800
+ ],
801
+ "message": "Invalid input"
802
+ },
803
+ {
804
+ "expected": "string",
805
+ "code": "invalid_type",
806
+ "path": [
807
+ "accessKeyAddress"
808
+ ],
809
+ "message": "Expected address"
810
+ },
811
+ {
812
+ "expected": "string",
813
+ "code": "invalid_type",
814
+ "path": [
815
+ "account"
816
+ ],
817
+ "message": "Expected address"
818
+ },
819
+ {
820
+ "code": "invalid_format",
821
+ "format": "template_literal",
822
+ "pattern": "^0x[0-9a-fA-F]{40}$",
823
+ "path": [
824
+ "limits",
825
+ 0,
826
+ "token"
827
+ ],
828
+ "message": "Expected address"
829
+ }
830
+ ]
831
+ ],
832
+ "path": [],
833
+ "message": "Invalid input"
834
+ }
835
+ ]],
836
+ "success": false,
837
+ }
563
838
  `)
564
839
  })
565
840
 
@@ -907,7 +1182,7 @@ describe('poll', () => {
907
1182
  })
908
1183
 
909
1184
  const first_ =
910
- first.status === 'authorized'
1185
+ first.status === 'authorized' && first.action !== 'updateAccessKey'
911
1186
  ? {
912
1187
  ...first,
913
1188
  keyAuthorization: {
@@ -1035,6 +1310,242 @@ describe('authorize', () => {
1035
1310
  expect(polled.status).toMatchInlineSnapshot(`"authorized"`)
1036
1311
  })
1037
1312
 
1313
+ test('behavior: completes a verified access key update', async () => {
1314
+ const store = CliAuth.Store.memory()
1315
+ const codeVerifier = 'update-device-code-verifier'
1316
+ const { code } = await CliAuth.createDeviceCode({
1317
+ chainId: chain.id,
1318
+ request: {
1319
+ action: 'updateAccessKey',
1320
+ accessKeyAddress: accessKey.address,
1321
+ account: root.address,
1322
+ codeChallenge: await createCodeChallenge(codeVerifier),
1323
+ limits,
1324
+ },
1325
+ store,
1326
+ })
1327
+ const client = createClient({
1328
+ chain,
1329
+ transport: custom({
1330
+ async request() {
1331
+ return encodeFunctionResult({
1332
+ abi: Abis.accountKeychain,
1333
+ functionName: 'getRemainingLimitWithPeriod',
1334
+ result: [limit, 0n],
1335
+ })
1336
+ },
1337
+ }),
1338
+ })
1339
+
1340
+ const authorized = await CliAuth.authorize({
1341
+ client,
1342
+ request: {
1343
+ action: 'updateAccessKey',
1344
+ accountAddress: root.address,
1345
+ chainId: BigInt(chain.id),
1346
+ code,
1347
+ },
1348
+ store,
1349
+ })
1350
+ const polled = await CliAuth.poll({
1351
+ code,
1352
+ request: { codeVerifier },
1353
+ store,
1354
+ })
1355
+
1356
+ expect({ authorized, polled }).toMatchInlineSnapshot(`
1357
+ {
1358
+ "authorized": {
1359
+ "status": "authorized",
1360
+ },
1361
+ "polled": {
1362
+ "action": "updateAccessKey",
1363
+ "status": "authorized",
1364
+ },
1365
+ }
1366
+ `)
1367
+ })
1368
+
1369
+ test('behavior: replaces a signed authorization for an unpublished access key', async () => {
1370
+ const store = CliAuth.Store.memory()
1371
+ const codeVerifier = 'pending-update-device-code-verifier'
1372
+ const original = (
1373
+ await authorize('unused', {
1374
+ limits: [{ limit: 1n, token }],
1375
+ })
1376
+ ).keyAuthorization
1377
+ const { code } = await CliAuth.createDeviceCode({
1378
+ chainId: chain.id,
1379
+ request: {
1380
+ action: 'updateAccessKey',
1381
+ accessKeyAddress: accessKey.address,
1382
+ account: root.address,
1383
+ codeChallenge: await createCodeChallenge(codeVerifier),
1384
+ keyAuthorization: original,
1385
+ limits,
1386
+ },
1387
+ store,
1388
+ })
1389
+ const replacement = (await authorize(code, { limits })).keyAuthorization
1390
+
1391
+ await CliAuth.authorize({
1392
+ client: createMissingAccessKeyClient(),
1393
+ request: {
1394
+ action: 'updateAccessKey',
1395
+ accountAddress: root.address,
1396
+ chainId: BigInt(chain.id),
1397
+ code,
1398
+ keyAuthorization: replacement,
1399
+ },
1400
+ store,
1401
+ })
1402
+ const polled = await CliAuth.poll({
1403
+ code,
1404
+ request: { codeVerifier },
1405
+ store,
1406
+ })
1407
+
1408
+ expect(polled).toEqual({
1409
+ action: 'updateAccessKey',
1410
+ keyAuthorization: replacement,
1411
+ status: 'authorized',
1412
+ })
1413
+ })
1414
+
1415
+ test('behavior: rejects a replacement after the access key is published', async () => {
1416
+ const store = CliAuth.Store.memory()
1417
+ const original = (
1418
+ await authorize('unused', {
1419
+ limits: [{ limit: 1n, token }],
1420
+ })
1421
+ ).keyAuthorization
1422
+ const { code } = await CliAuth.createDeviceCode({
1423
+ chainId: chain.id,
1424
+ request: {
1425
+ action: 'updateAccessKey',
1426
+ accessKeyAddress: accessKey.address,
1427
+ account: root.address,
1428
+ codeChallenge: await createCodeChallenge('pending-update-device-code-verifier'),
1429
+ keyAuthorization: original,
1430
+ limits,
1431
+ },
1432
+ store,
1433
+ })
1434
+ const replacement = (await authorize(code, { limits })).keyAuthorization
1435
+ const client = createClient({
1436
+ chain,
1437
+ transport: custom({
1438
+ async request({ method }) {
1439
+ if (method === 'eth_call')
1440
+ return encodeFunctionResult({
1441
+ abi: Abis.accountKeychain,
1442
+ functionName: 'getKey',
1443
+ result: {
1444
+ enforceLimits: true,
1445
+ expiry: BigInt(expiry),
1446
+ isRevoked: false,
1447
+ keyId: accessKey.address,
1448
+ signatureType: 1,
1449
+ },
1450
+ } as never)
1451
+ throw new Error('Contract lookup unavailable.')
1452
+ },
1453
+ }),
1454
+ })
1455
+
1456
+ await expect(
1457
+ CliAuth.authorize({
1458
+ client,
1459
+ request: {
1460
+ action: 'updateAccessKey',
1461
+ accountAddress: root.address,
1462
+ chainId: BigInt(chain.id),
1463
+ code,
1464
+ keyAuthorization: replacement,
1465
+ },
1466
+ store,
1467
+ }),
1468
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1469
+ `[Error: Access key was published while the update was pending.]`,
1470
+ )
1471
+ })
1472
+
1473
+ test('behavior: rejects immutable field changes to a pending key authorization', async () => {
1474
+ const store = CliAuth.Store.memory()
1475
+ const original = (await authorize('unused')).keyAuthorization
1476
+ const { code } = await CliAuth.createDeviceCode({
1477
+ chainId: chain.id,
1478
+ request: {
1479
+ action: 'updateAccessKey',
1480
+ accessKeyAddress: accessKey.address,
1481
+ account: root.address,
1482
+ codeChallenge: await createCodeChallenge('pending-update-device-code-verifier'),
1483
+ keyAuthorization: original,
1484
+ limits,
1485
+ },
1486
+ store,
1487
+ })
1488
+ const replacement = (await authorize(code, { expiry: expiry + 1 })).keyAuthorization
1489
+
1490
+ await expect(
1491
+ CliAuth.authorize({
1492
+ client: createMissingAccessKeyClient(),
1493
+ request: {
1494
+ action: 'updateAccessKey',
1495
+ accountAddress: root.address,
1496
+ chainId: BigInt(chain.id),
1497
+ code,
1498
+ keyAuthorization: replacement,
1499
+ },
1500
+ store,
1501
+ }),
1502
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1503
+ `[Error: Replacement key authorization changed immutable access key fields.]`,
1504
+ )
1505
+ })
1506
+
1507
+ test('behavior: rejects an access key update before the requested limit is applied', async () => {
1508
+ const store = CliAuth.Store.memory()
1509
+ const { code } = await CliAuth.createDeviceCode({
1510
+ chainId: chain.id,
1511
+ request: {
1512
+ action: 'updateAccessKey',
1513
+ accessKeyAddress: accessKey.address,
1514
+ account: root.address,
1515
+ codeChallenge: await createCodeChallenge('update-device-code-verifier'),
1516
+ limits,
1517
+ },
1518
+ store,
1519
+ })
1520
+ const client = createClient({
1521
+ chain,
1522
+ transport: custom({
1523
+ async request() {
1524
+ return encodeFunctionResult({
1525
+ abi: Abis.accountKeychain,
1526
+ functionName: 'getRemainingLimitWithPeriod',
1527
+ result: [limit - 1n, 0n],
1528
+ })
1529
+ },
1530
+ }),
1531
+ })
1532
+
1533
+ await expect(
1534
+ CliAuth.authorize({
1535
+ client,
1536
+ request: {
1537
+ action: 'updateAccessKey',
1538
+ accountAddress: root.address,
1539
+ chainId: BigInt(chain.id),
1540
+ code,
1541
+ },
1542
+ store,
1543
+ }),
1544
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1545
+ `[Error: Access key spending limits do not match the device code request.]`,
1546
+ )
1547
+ })
1548
+
1038
1549
  test('behavior: accepts user-approved expiry and limit changes', async () => {
1039
1550
  const store = CliAuth.Store.memory()
1040
1551
  const { codeVerifier, request } = await createRequest()
@@ -1063,7 +1574,8 @@ describe('authorize', () => {
1063
1574
  store,
1064
1575
  })
1065
1576
 
1066
- if (polled.status !== 'authorized') throw new Error('Expected device code to be authorized.')
1577
+ if (polled.status !== 'authorized' || polled.action === 'updateAccessKey')
1578
+ throw new Error('Expected device code to be authorized.')
1067
1579
 
1068
1580
  expect(authorized).toMatchInlineSnapshot(`
1069
1581
  {
@@ -1139,7 +1651,7 @@ describe('authorize', () => {
1139
1651
  store,
1140
1652
  }),
1141
1653
  ).rejects.toThrowErrorMatchingInlineSnapshot(
1142
- `[Error: Key authorization key does not match the device-code request.]`,
1654
+ `[Error: Key authorization key does not match the device code request.]`,
1143
1655
  )
1144
1656
  })
1145
1657
 
@@ -1200,7 +1712,7 @@ describe('authorize', () => {
1200
1712
  })
1201
1713
 
1202
1714
  const keyAuthorization =
1203
- polled.status === 'authorized'
1715
+ polled.status === 'authorized' && polled.action !== 'updateAccessKey'
1204
1716
  ? {
1205
1717
  ...polled.keyAuthorization,
1206
1718
  signature: {