@johnhenry/browsermesh-apps 0.7.1 → 0.9.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.
- package/README.md +35 -0
- package/package.json +3 -3
- package/src/grant-log.mjs +7 -3
- package/src/internal/verify-order.mjs +149 -0
- package/src/key-distribution.mjs +15 -2
- package/src/payments.mjs +10 -5
- package/src/peer-chat.mjs +7 -4
- package/src/peer-timestamp.mjs +15 -4
- package/src/peer-verification.mjs +4 -2
package/README.md
CHANGED
|
@@ -522,6 +522,41 @@ agent runtime" section for the full design writeup, including two real bugs
|
|
|
522
522
|
found while building it and the recommended DI pattern for new tools going
|
|
523
523
|
forward.
|
|
524
524
|
|
|
525
|
+
## Verify argument order (caller-supplied callbacks)
|
|
526
|
+
|
|
527
|
+
Every signature check in BrowserMesh takes `(identity, signature, data)`, the
|
|
528
|
+
same as `crypto.subtle.verify`. This applies to the callbacks you hand in:
|
|
529
|
+
|
|
530
|
+
| Where | Callback shape |
|
|
531
|
+
|-------|----------------|
|
|
532
|
+
| `PaymentChannel` `opts.verifyFn` | `(publicKey, signature, data) => boolean` |
|
|
533
|
+
| chat service / `PeerChat` `verifyFn` | `(fromPubKey, signature, data) => boolean` |
|
|
534
|
+
| `GrantLog` / key-distribution `wallet.verify` | `(publicKeyBytes, signature, data) => boolean` |
|
|
535
|
+
| `TimestampProof.verify(verifyFn)` and `identity.verify` on `TimestampAuthority` | `(signerPodId, signature, data) => boolean` |
|
|
536
|
+
| `Attestation.verify(verifyFn)` | `(podId, signature, resultHash) => boolean` |
|
|
537
|
+
|
|
538
|
+
**BREAKING (apps 0.9.0):** `peer-timestamp` previously used
|
|
539
|
+
`(signature, data, signerPodId)` and `Attestation.verify` used
|
|
540
|
+
`(podId, resultHash, signature)`. Update those callbacks; swap the arguments.
|
|
541
|
+
|
|
542
|
+
An old-order callback used to return `false` for every message, silently,
|
|
543
|
+
because the library catches errors around callbacks. To make that loud, the
|
|
544
|
+
`PaymentChannel`, chat, `GrantLog` and key-distribution paths run a one-time
|
|
545
|
+
self-test the first time a callback is used (the probe is started when the
|
|
546
|
+
callback is passed in): the callback is called with a known-good Ed25519 test
|
|
547
|
+
vector (RFC 8032, test 2) in the new order. If it rejects that but accepts
|
|
548
|
+
`(publicKey, data, signature)`, a `TypeError` naming the new order is thrown
|
|
549
|
+
from the call that used it (for example `channel.receive()` or
|
|
550
|
+
`chat.receiveEnvelope()`), and every later call rejects the same way. A
|
|
551
|
+
callback that accepts the new order, or that rejects both orders (for example
|
|
552
|
+
it looks keys up in a directory the test key is not in), is left alone. The
|
|
553
|
+
self-test is skipped when `NODE_ENV` is `production`. Because the test vector
|
|
554
|
+
is fed to your callback once, spies on `verifyFn` see one extra call with
|
|
555
|
+
`Uint8Array` arguments.
|
|
556
|
+
|
|
557
|
+
The `peer-timestamp` paths also throw a `TypeError` if the first argument is a
|
|
558
|
+
64-byte signature.
|
|
559
|
+
|
|
525
560
|
## License
|
|
526
561
|
|
|
527
562
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-apps",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Application layer: marketplace, chat, payments, compute, orchestration, and agent tools for BrowserMesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"directory": "packages/browsermesh-apps"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@johnhenry/browsermesh-primitives": ">=0.0
|
|
31
|
-
"@johnhenry/browsermesh-core": ">=0.
|
|
30
|
+
"@johnhenry/browsermesh-primitives": ">=0.2.0 <1.0.0",
|
|
31
|
+
"@johnhenry/browsermesh-core": ">=0.5.0 <1.0.0",
|
|
32
32
|
"@johnhenry/browsermesh-transport": ">=0.1.0 <1.0.0",
|
|
33
33
|
"@johnhenry/browsermesh-sync": ">=0.0.0 <1.0.0",
|
|
34
34
|
"@johnhenry/browsermesh-discovery": ">=0.0.1 <1.0.0",
|
package/src/grant-log.mjs
CHANGED
|
@@ -218,6 +218,7 @@
|
|
|
218
218
|
*/
|
|
219
219
|
|
|
220
220
|
import { ORSet, encodeBase64url, decodeBase64url } from '@johnhenry/browsermesh-primitives'
|
|
221
|
+
import { guardVerifyMethod, isVerifyOrderError } from './internal/verify-order.mjs'
|
|
221
222
|
|
|
222
223
|
/** Default `envelope.type` used to route GrantLog payloads on the shared `onIncomingData()` bus. */
|
|
223
224
|
const DEFAULT_ENVELOPE_TYPE = 'grant-log'
|
|
@@ -304,10 +305,11 @@ export class GrantLog {
|
|
|
304
305
|
#localPodId
|
|
305
306
|
|
|
306
307
|
/** @type {import('@johnhenry/browsermesh-core').IdentityWallet} Duck-typed:
|
|
307
|
-
* only `.sign(podId, data)`, `.verify(pubKeyBytes,
|
|
308
|
+
* only `.sign(podId, data)`, `.verify(pubKeyBytes, sig, data)`,
|
|
308
309
|
* `.getPublicKeyBytes(podId)` are used (the real `IdentityWallet`'s
|
|
309
310
|
* confirmed API, see identity-wallet.mjs / identity.mjs). */
|
|
310
311
|
#wallet
|
|
312
|
+
#verifyWallet
|
|
311
313
|
|
|
312
314
|
/** @type {import('./peer-registry.mjs').PeerRegistry} */
|
|
313
315
|
#registry
|
|
@@ -376,6 +378,7 @@ export class GrantLog {
|
|
|
376
378
|
throw new Error('GrantLog: registry is required and must provide grantCapabilities()/revokeCapabilities()')
|
|
377
379
|
}
|
|
378
380
|
|
|
381
|
+
this.#verifyWallet = guardVerifyMethod(wallet, 'GrantLog wallet.verify')
|
|
379
382
|
this.#resource = resource
|
|
380
383
|
this.#localPodId = localPodId
|
|
381
384
|
this.#wallet = wallet
|
|
@@ -550,8 +553,9 @@ export class GrantLog {
|
|
|
550
553
|
if (derivedPodId !== signedBy) return false
|
|
551
554
|
|
|
552
555
|
const payload = new TextEncoder().encode(signedPayloadOf({ pubKey, scope, action, at, signedBy }))
|
|
553
|
-
return await this.#
|
|
554
|
-
} catch {
|
|
556
|
+
return await this.#verifyWallet(rawPubKeyBytes, sigBytes, payload)
|
|
557
|
+
} catch (err) {
|
|
558
|
+
if (isVerifyOrderError(err)) throw err
|
|
555
559
|
// Malformed base64url, wrong-length keys/signatures, etc. are all
|
|
556
560
|
// "reject", never a thrown error the caller has to remember to catch.
|
|
557
561
|
return false
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Guards for the Ed25519 verify argument order.
|
|
2
|
+
//
|
|
3
|
+
// The canonical order everywhere in BrowserMesh is (identity, signature, data):
|
|
4
|
+
// verify(publicKey, signature, data)
|
|
5
|
+
// verify(signerPodId, signature, data)
|
|
6
|
+
//
|
|
7
|
+
// Library entry points throw a TypeError when they see a 64-byte signature
|
|
8
|
+
// where the identity belongs. Caller-supplied callbacks are different: the
|
|
9
|
+
// library hands THEM the arguments, and callers of callbacks usually catch, so
|
|
10
|
+
// an old-order callback just returns false. `guardVerifyFn` probes a callback
|
|
11
|
+
// once with a known-good vector to turn that silent failure into a TypeError.
|
|
12
|
+
|
|
13
|
+
/** Ed25519 signatures are exactly this many bytes. */
|
|
14
|
+
export const ED25519_SIGNATURE_BYTES = 64
|
|
15
|
+
|
|
16
|
+
/** `err.code` of every TypeError raised by this module. Catch blocks rethrow it. */
|
|
17
|
+
export const VERIFY_ORDER_ERROR_CODE = 'ERR_VERIFY_ARG_ORDER'
|
|
18
|
+
|
|
19
|
+
// RFC 8032 section 7.1, TEST 2 (1-byte message 0x72).
|
|
20
|
+
const VECTOR_PUBLIC_KEY = hex('3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c')
|
|
21
|
+
const VECTOR_MESSAGE = hex('72')
|
|
22
|
+
const VECTOR_SIGNATURE = hex(
|
|
23
|
+
'92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da' +
|
|
24
|
+
'085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00',
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
function hex(s) {
|
|
28
|
+
const out = new Uint8Array(s.length / 2)
|
|
29
|
+
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
|
|
30
|
+
return out
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function orderError(message) {
|
|
34
|
+
const err = new TypeError(message)
|
|
35
|
+
err.code = VERIFY_ORDER_ERROR_CODE
|
|
36
|
+
return err
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {unknown} err */
|
|
40
|
+
export function isVerifyOrderError(err) {
|
|
41
|
+
return err instanceof TypeError && err.code === VERIFY_ORDER_ERROR_CODE
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @param {unknown} v */
|
|
45
|
+
function isSignatureSized(v) {
|
|
46
|
+
return (v instanceof ArrayBuffer || ArrayBuffer.isView(v)) && v.byteLength === ED25519_SIGNATURE_BYTES
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Throw a TypeError when the identity argument is a 64-byte signature, i.e.
|
|
51
|
+
* the caller used the pre-canonical `(signature, data, identity)` order.
|
|
52
|
+
*
|
|
53
|
+
* @param {unknown} identity - what should be a public key or signer pod ID
|
|
54
|
+
* @param {string} label - e.g. 'identity.verify'
|
|
55
|
+
*/
|
|
56
|
+
export function assertIdentityFirst(identity, label) {
|
|
57
|
+
if (isSignatureSized(identity)) {
|
|
58
|
+
throw orderError(
|
|
59
|
+
`${label}: argument order is (signerPodId, signature, data); ` +
|
|
60
|
+
'the first argument is a 64-byte signature, which looks like the old (signature, data, signerPodId) order',
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Dev mode unless NODE_ENV is explicitly 'production'. */
|
|
66
|
+
function devMode() {
|
|
67
|
+
try {
|
|
68
|
+
return globalThis.process?.env?.NODE_ENV !== 'production'
|
|
69
|
+
} catch {
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Probe `verifyFn` with a known-good Ed25519 vector in the canonical order
|
|
76
|
+
* `(publicKey, signature, data)`. If it rejects that but accepts the OLD order
|
|
77
|
+
* `(publicKey, data, signature)`, it is an old-order callback: throw.
|
|
78
|
+
*
|
|
79
|
+
* A callback that accepts the canonical order, rejects both (for instance it
|
|
80
|
+
* resolves keys from a directory the vector is not in), or throws on the
|
|
81
|
+
* probe passes: only a provable old-order callback is an error.
|
|
82
|
+
*
|
|
83
|
+
* @param {Function} verifyFn
|
|
84
|
+
* @param {string} label
|
|
85
|
+
* @returns {Promise<void>}
|
|
86
|
+
*/
|
|
87
|
+
export async function selfTestVerifyFn(verifyFn, label) {
|
|
88
|
+
const attempt = async (...args) => {
|
|
89
|
+
try {
|
|
90
|
+
return (await verifyFn(...args)) === true
|
|
91
|
+
} catch {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (await attempt(VECTOR_PUBLIC_KEY.slice(), VECTOR_SIGNATURE.slice(), VECTOR_MESSAGE.slice())) return
|
|
96
|
+
if (await attempt(VECTOR_PUBLIC_KEY.slice(), VECTOR_MESSAGE.slice(), VECTOR_SIGNATURE.slice())) {
|
|
97
|
+
throw orderError(
|
|
98
|
+
`${label}: verifyFn uses the old argument order. It must now be ` +
|
|
99
|
+
'(publicKey, signature, data), the same as crypto.subtle.verify; ' +
|
|
100
|
+
'it accepted (publicKey, data, signature) for a known-good vector and rejected the new order',
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Wrap a caller-supplied `verifyFn` so the first use runs the one-time order
|
|
107
|
+
* self-test (kicked off now, awaited before the first real call). The probe
|
|
108
|
+
* result is sticky: once an old-order callback is detected every call rejects
|
|
109
|
+
* with the same TypeError. Skipped when NODE_ENV is 'production'.
|
|
110
|
+
*
|
|
111
|
+
* Callers that catch errors around the wrapped function must rethrow when
|
|
112
|
+
* `isVerifyOrderError(err)` is true.
|
|
113
|
+
*
|
|
114
|
+
* @template {Function|null|undefined} F
|
|
115
|
+
* @param {F} verifyFn
|
|
116
|
+
* @param {string} label
|
|
117
|
+
* @returns {F}
|
|
118
|
+
*/
|
|
119
|
+
export function guardVerifyFn(verifyFn, label) {
|
|
120
|
+
if (typeof verifyFn !== 'function' || !devMode()) return verifyFn
|
|
121
|
+
const probe = selfTestVerifyFn(verifyFn, label)
|
|
122
|
+
probe.catch(() => {}) // surfaced on first use, never as an unhandled rejection
|
|
123
|
+
const guarded = async (...args) => {
|
|
124
|
+
await probe
|
|
125
|
+
return verifyFn(...args)
|
|
126
|
+
}
|
|
127
|
+
return /** @type {F} */ (guarded)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Same as {@link guardVerifyFn} for an object exposing `verify(publicKey,
|
|
132
|
+
* signature, data)` (a wallet). Returns a function to call instead of
|
|
133
|
+
* `obj.verify(...)`. The probe is created lazily, once per object.
|
|
134
|
+
*
|
|
135
|
+
* @param {{ verify: Function }} obj
|
|
136
|
+
* @param {string} label
|
|
137
|
+
* @returns {(...args: any[]) => Promise<any>}
|
|
138
|
+
*/
|
|
139
|
+
export function guardVerifyMethod(obj, label) {
|
|
140
|
+
let probe = null
|
|
141
|
+
return async (...args) => {
|
|
142
|
+
if (probe === null) {
|
|
143
|
+
probe = devMode() ? selfTestVerifyFn((...a) => obj.verify(...a), label) : Promise.resolve()
|
|
144
|
+
probe.catch(() => {})
|
|
145
|
+
}
|
|
146
|
+
await probe
|
|
147
|
+
return obj.verify(...args)
|
|
148
|
+
}
|
|
149
|
+
}
|
package/src/key-distribution.mjs
CHANGED
|
@@ -168,6 +168,18 @@
|
|
|
168
168
|
*/
|
|
169
169
|
|
|
170
170
|
import { encodeBase64url, decodeBase64url } from '@johnhenry/browsermesh-primitives'
|
|
171
|
+
import { guardVerifyMethod, isVerifyOrderError } from './internal/verify-order.mjs'
|
|
172
|
+
|
|
173
|
+
// One order self-test per wallet object (see internal/verify-order.mjs).
|
|
174
|
+
const guardedWalletVerify = new WeakMap()
|
|
175
|
+
function verifyViaWallet(wallet) {
|
|
176
|
+
let fn = guardedWalletVerify.get(wallet)
|
|
177
|
+
if (!fn) {
|
|
178
|
+
fn = guardVerifyMethod(wallet, 'key-distribution wallet.verify')
|
|
179
|
+
guardedWalletVerify.set(wallet, fn)
|
|
180
|
+
}
|
|
181
|
+
return fn
|
|
182
|
+
}
|
|
171
183
|
// @johnhenry/browsermesh-core (an optional peerDependency) is imported
|
|
172
184
|
// lazily, at each of the three call sites below, rather than eagerly here
|
|
173
185
|
// -- so this module doesn't force it on every consumer of this package's
|
|
@@ -452,8 +464,9 @@ export function createKeyDistributionService({
|
|
|
452
464
|
const payload = new TextEncoder().encode(signedPayloadOf({ resource, ephemeralPublicKey: envelope.ephemeralPublicKey, wrappedKey: envelope.wrappedKey, iv: envelope.iv, at, signedBy }))
|
|
453
465
|
let validSig = false
|
|
454
466
|
try {
|
|
455
|
-
validSig = await peerNode.wallet
|
|
456
|
-
} catch {
|
|
467
|
+
validSig = await verifyViaWallet(peerNode.wallet)(rawPubKeyBytes, sigBytes, payload)
|
|
468
|
+
} catch (err) {
|
|
469
|
+
if (isVerifyOrderError(err)) throw err
|
|
457
470
|
validSig = false
|
|
458
471
|
}
|
|
459
472
|
if (!validSig) {
|
package/src/payments.mjs
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import { MESH_TYPE } from '@johnhenry/browsermesh-primitives';
|
|
14
14
|
import { silentCatch } from './silent-catch.mjs'
|
|
15
|
+
import { guardVerifyFn, isVerifyOrderError } from './internal/verify-order.mjs'
|
|
15
16
|
|
|
16
17
|
// ---------------------------------------------------------------------------
|
|
17
18
|
// Wire constants — imported from canonical registry
|
|
@@ -63,7 +64,10 @@ function generateEscrowId() {
|
|
|
63
64
|
// in without this published, general-purpose package taking a hard
|
|
64
65
|
// dependency on it. Shape matches peer-chat.mjs's convention:
|
|
65
66
|
// signFn: async (data: Uint8Array) => Uint8Array
|
|
66
|
-
// verifyFn: async (pubKey: Uint8Array,
|
|
67
|
+
// verifyFn: async (pubKey: Uint8Array, sig: Uint8Array, data: Uint8Array) => boolean
|
|
68
|
+
// (canonical (publicKey, signature, data) order; an old-order callback is
|
|
69
|
+
// detected by a one-time self-test and throws a TypeError, see
|
|
70
|
+
// internal/verify-order.mjs)
|
|
67
71
|
|
|
68
72
|
/**
|
|
69
73
|
* Encode a Uint8Array to a base64 string.
|
|
@@ -436,7 +440,7 @@ export class PaymentChannel {
|
|
|
436
440
|
* @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array.
|
|
437
441
|
* When omitted, PaymentUpdate/close signatures stay `null` and close()
|
|
438
442
|
* stays unilateral -- fully backward compatible.
|
|
439
|
-
* @param {Function} [opts.verifyFn] - async (pubKey,
|
|
443
|
+
* @param {Function} [opts.verifyFn] - async (pubKey, sig, data) => boolean
|
|
440
444
|
* @param {Uint8Array} [opts.remotePublicKey] - Remote pod's raw public key,
|
|
441
445
|
* required alongside verifyFn to verify incoming updates/claims.
|
|
442
446
|
* @param {string} [opts.channelId] - Use this exact channel ID instead of
|
|
@@ -456,7 +460,7 @@ export class PaymentChannel {
|
|
|
456
460
|
this.#ttlMs = opts.ttlMs || DEFAULT_TTL_MS;
|
|
457
461
|
this.#createdAt = Date.now();
|
|
458
462
|
this.#signFn = opts.signFn || null;
|
|
459
|
-
this.#verifyFn = opts.verifyFn || null;
|
|
463
|
+
this.#verifyFn = guardVerifyFn(opts.verifyFn, 'PaymentChannel') || null;
|
|
460
464
|
this.#remotePublicKey = opts.remotePublicKey || null;
|
|
461
465
|
}
|
|
462
466
|
|
|
@@ -682,8 +686,9 @@ export class PaymentChannel {
|
|
|
682
686
|
try {
|
|
683
687
|
const data = canonicalBytes(fields);
|
|
684
688
|
const sigBytes = base64ToBytes(signatureB64);
|
|
685
|
-
return await this.#verifyFn(this.#remotePublicKey,
|
|
686
|
-
} catch {
|
|
689
|
+
return await this.#verifyFn(this.#remotePublicKey, sigBytes, data);
|
|
690
|
+
} catch (err) {
|
|
691
|
+
if (isVerifyOrderError(err)) throw err;
|
|
687
692
|
return false;
|
|
688
693
|
}
|
|
689
694
|
}
|
package/src/peer-chat.mjs
CHANGED
|
@@ -147,6 +147,8 @@ function base64ToBytes(b64) {
|
|
|
147
147
|
// ---------------------------------------------------------------------------
|
|
148
148
|
// PeerChat
|
|
149
149
|
// ---------------------------------------------------------------------------
|
|
150
|
+
import { guardVerifyFn, isVerifyOrderError } from './internal/verify-order.mjs'
|
|
151
|
+
|
|
150
152
|
|
|
151
153
|
/**
|
|
152
154
|
* P2P chat, transport-agnostic beyond a caller-supplied `send()` function.
|
|
@@ -195,7 +197,7 @@ export class PeerChat {
|
|
|
195
197
|
* an envelope -- the caller, typically `createChatService()`, adds
|
|
196
198
|
* whatever envelope shape its own transport needs).
|
|
197
199
|
* @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array
|
|
198
|
-
* @param {Function} [opts.verifyFn] - async (fromPubKey,
|
|
200
|
+
* @param {Function} [opts.verifyFn] - async (fromPubKey, sig, data) => boolean
|
|
199
201
|
* @param {number} [opts.maxHistory=1000] - Maximum messages to retain
|
|
200
202
|
* @param {Function} [opts.autoResponder] - async (message) => string|null
|
|
201
203
|
* @param {Function} [opts.onLog] - Logging callback
|
|
@@ -211,7 +213,7 @@ export class PeerChat {
|
|
|
211
213
|
this.#localPubKey = localPubKey
|
|
212
214
|
this.#send = send
|
|
213
215
|
this.#signFn = signFn || null
|
|
214
|
-
this.#verifyFn = verifyFn || null
|
|
216
|
+
this.#verifyFn = guardVerifyFn(verifyFn, 'ChatService') || null
|
|
215
217
|
this.#maxHistory = maxHistory ?? 1000
|
|
216
218
|
this.#autoResponder = autoResponder || null
|
|
217
219
|
this.#onLog = onLog || (() => {})
|
|
@@ -349,8 +351,9 @@ export class PeerChat {
|
|
|
349
351
|
timestamp: message.timestamp,
|
|
350
352
|
}))
|
|
351
353
|
const sigBytes = base64ToBytes(message.signature)
|
|
352
|
-
message.verified = await this.#verifyFn(fromPubKey,
|
|
354
|
+
message.verified = await this.#verifyFn(fromPubKey, sigBytes, data)
|
|
353
355
|
} catch (err) {
|
|
356
|
+
if (isVerifyOrderError(err)) throw err
|
|
354
357
|
this.#onLog(1, `Signature verification failed: ${err.message}`)
|
|
355
358
|
message.verified = false
|
|
356
359
|
}
|
|
@@ -498,7 +501,7 @@ export class PeerChat {
|
|
|
498
501
|
* @param {object} [opts]
|
|
499
502
|
* @param {Function} [opts.signFn] - async (data: Uint8Array) => Uint8Array.
|
|
500
503
|
* Passed straight through to `PeerChat`; omit to send unsigned messages.
|
|
501
|
-
* @param {Function} [opts.verifyFn] - async (fromPubKey,
|
|
504
|
+
* @param {Function} [opts.verifyFn] - async (fromPubKey, sig, data) => boolean.
|
|
502
505
|
* Passed straight through to `PeerChat`; called with each message's real
|
|
503
506
|
* sender, not a fixed key -- see module doc comment's point 2.
|
|
504
507
|
* @param {number} [opts.maxHistory=1000]
|
package/src/peer-timestamp.mjs
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-timestamp.test.mjs
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import { assertIdentityFirst, isVerifyOrderError } from './internal/verify-order.mjs'
|
|
20
|
+
|
|
19
21
|
// ---------------------------------------------------------------------------
|
|
20
22
|
// Constants
|
|
21
23
|
// ---------------------------------------------------------------------------
|
|
@@ -95,7 +97,10 @@ export class TimestampProof {
|
|
|
95
97
|
* themselves -- so `issuedBy` is the pod whose key `verifyFn` must resolve,
|
|
96
98
|
* for witness entries as much as for the proof itself.
|
|
97
99
|
*
|
|
98
|
-
* @param {Function} [verifyFn] - async (
|
|
100
|
+
* @param {Function} [verifyFn] - async (signerPodId: string, signature: Uint8Array, data: string) => boolean
|
|
101
|
+
* (identity, signature, data) -- the same order as `PodIdentity.verify` and
|
|
102
|
+
* `crypto.subtle.verify`. BREAKING: this was (signature, data, signerPodId).
|
|
103
|
+
* A 64-byte signature in the first position throws a TypeError.
|
|
99
104
|
* @returns {Promise<{ valid: boolean, confidence: number, checked: 'structure'|'signatures', reason?: string }>}
|
|
100
105
|
*/
|
|
101
106
|
async verify(verifyFn) {
|
|
@@ -119,10 +124,12 @@ export class TimestampProof {
|
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
const check = async (signature, data, what) => {
|
|
127
|
+
assertIdentityFirst(this.issuedBy, 'TimestampProof.verify')
|
|
122
128
|
let ok
|
|
123
129
|
try {
|
|
124
|
-
ok = await verifyFn(fromBase64(signature), data
|
|
130
|
+
ok = await verifyFn(this.issuedBy, fromBase64(signature), data)
|
|
125
131
|
} catch (err) {
|
|
132
|
+
if (isVerifyOrderError(err)) throw err
|
|
126
133
|
return `verifying ${what} threw: ${err.message}`
|
|
127
134
|
}
|
|
128
135
|
return ok === true ? null : `${what} failed verification`
|
|
@@ -348,7 +355,8 @@ export class TimestampAuthority {
|
|
|
348
355
|
* Verify a TimestampProof.
|
|
349
356
|
*
|
|
350
357
|
* Checks every signature in the proof against the key of the pod that
|
|
351
|
-
* issued it. This requires `identity.verify(signature, data
|
|
358
|
+
* issued it. This requires `identity.verify(signerPodId, signature, data)` (BREAKING:
|
|
359
|
+
* was `(signature, data, signerPodId)`).
|
|
352
360
|
* Without it the only check available is re-signing with THIS authority's
|
|
353
361
|
* own key, which answers a different question -- "did I issue this?" -- and
|
|
354
362
|
* is reported as such rather than as authenticity.
|
|
@@ -363,7 +371,10 @@ export class TimestampAuthority {
|
|
|
363
371
|
|
|
364
372
|
if (this.#identity.verify) {
|
|
365
373
|
const result = await proof.verify(
|
|
366
|
-
(signature, data
|
|
374
|
+
(signerPodId, signature, data) => {
|
|
375
|
+
assertIdentityFirst(signerPodId, 'TimestampAuthority identity.verify')
|
|
376
|
+
return this.#identity.verify(signerPodId, signature, data)
|
|
377
|
+
},
|
|
367
378
|
)
|
|
368
379
|
if (!result.valid) {
|
|
369
380
|
return { valid: false, checked: result.checked, reason: result.reason }
|
|
@@ -97,11 +97,13 @@ export class Attestation {
|
|
|
97
97
|
/**
|
|
98
98
|
* Verify this attestation using the provided verification function.
|
|
99
99
|
*
|
|
100
|
-
* @param {Function} verifyFn - (podId,
|
|
100
|
+
* @param {Function} verifyFn - (podId, signature, resultHash) => boolean
|
|
101
|
+
* (identity, signature, data), the same order as every other verify in
|
|
102
|
+
* BrowserMesh. BREAKING: this was (podId, resultHash, signature).
|
|
101
103
|
* @returns {boolean}
|
|
102
104
|
*/
|
|
103
105
|
verify(verifyFn) {
|
|
104
|
-
return verifyFn(this.podId, this.
|
|
106
|
+
return verifyFn(this.podId, this.signature, this.resultHash)
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
/**
|