@bsv/expo-wallet-toolbox 0.13.0 → 0.15.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/CHANGELOG.md +71 -0
- package/core/context/WalletContext.tsx +14 -2
- package/core/demo/demoAssets.ts +2 -2
- package/core/localpay/build.ts +1 -1
- package/core/localpay/pending.ts +1 -1
- package/core/localpay/session.ts +1 -1
- package/core/localpay/settlementAck.ts +1 -1
- package/core/localpay/verify.ts +2 -2
- package/core/mandala/bundle.ts +1 -1
- package/core/mandala/createRuntime.ts +62 -16
- package/core/mandala/drain.ts +9 -6
- package/core/mandala/inboxPrehold.ts +8 -1
- package/core/mandala/permissionModule.ts +120 -14
- package/core/mandala/resendTransfer.ts +2 -2
- package/core/mandala/token.ts +45 -0
- package/core/mandala/types.ts +5 -0
- package/core/offline/tokenFrames.ts +1 -1
- package/core/parsePeerPayURI.ts +2 -2
- package/core/services/signingPermissionPolicy.ts +258 -0
- package/core/services/vault/guard.ts +43 -67
- package/package.json +4 -4
- package/ui/components/ui/PermissionSheet.tsx +17 -7
- package/ui/components/ui/protocolPrompt.ts +118 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BRC-162 token-script boundary for the wallet.
|
|
3
|
+
*
|
|
4
|
+
* A drop-in for the old `MandalaToken` (8-chunk PushDrop) from `@bsv/templates`
|
|
5
|
+
* 1.x, over `Bsv21Binary` from `@bsv/templates` 2.x. `decode` yields
|
|
6
|
+
* `{ assetId, amount, pubKeyHash }` for a VALUE-role output only (deploy and
|
|
7
|
+
* authority outputs are not coins) and throws on anything else, exactly as the
|
|
8
|
+
* old decoder did, so every call site keeps its `try/catch` shape.
|
|
9
|
+
*/
|
|
10
|
+
import { LockingScript, type WalletCounterparty, type WalletInterface, type WalletProtocol } from '@bsv/sdk'
|
|
11
|
+
import { Bsv21Binary, tokenIdToString } from '@bsv/templates'
|
|
12
|
+
|
|
13
|
+
export class MandalaToken {
|
|
14
|
+
private readonly inner: Bsv21Binary
|
|
15
|
+
|
|
16
|
+
constructor(wallet?: WalletInterface, originator?: string) {
|
|
17
|
+
this.inner = new Bsv21Binary(wallet, originator)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static decode(script: LockingScript | string): { assetId: string; amount: number; pubKeyHash: number[] } {
|
|
21
|
+
const ls = LockingScript.fromHex(typeof script === 'string' ? script : script.toHex())
|
|
22
|
+
const d = Bsv21Binary.decode(ls)
|
|
23
|
+
if (d.role !== 'value' || d.tokenId == null) throw new Error(`not a BRC-162 value output (role ${d.role})`)
|
|
24
|
+
if (d.restPubKeyHash == null) throw new Error('BRC-162 output is not P2PKH-locked')
|
|
25
|
+
if (d.amount > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error('BRC-162 amount exceeds 2^53-1')
|
|
26
|
+
return { assetId: tokenIdToString(d.tokenId), amount: Number(d.amount), pubKeyHash: d.restPubKeyHash }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A value coin of `assetId` locked to `pubKeyHash`. */
|
|
30
|
+
lock(assetId: string, amount: number, pubKeyHash: readonly number[]): LockingScript {
|
|
31
|
+
return LockingScript.fromHex(this.inner.lock(assetId, BigInt(amount), pubKeyHash).toHex())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A value coin locked to hash160 of the wallet-derived (BRC-29 style) key. */
|
|
35
|
+
async lockBRC29(
|
|
36
|
+
assetId: string,
|
|
37
|
+
amount: number,
|
|
38
|
+
protocolID: WalletProtocol,
|
|
39
|
+
keyID: string,
|
|
40
|
+
counterparty: WalletCounterparty
|
|
41
|
+
): Promise<LockingScript> {
|
|
42
|
+
const s = await this.inner.lockBRC29(assetId, BigInt(amount), protocolID, keyID, counterparty)
|
|
43
|
+
return LockingScript.fromHex(s.toHex())
|
|
44
|
+
}
|
|
45
|
+
}
|
package/core/mandala/types.ts
CHANGED
|
@@ -120,6 +120,11 @@ export type OverlayVerdict =
|
|
|
120
120
|
/** An AdmissionEntry as carried on the wire (frame v4 `admissions[]`). */
|
|
121
121
|
export interface AdmissionEntryWire {
|
|
122
122
|
txid: string
|
|
123
|
+
/**
|
|
124
|
+
* BRC-162 σI v3 topic (`tokenTopic(assetId)`). Optional: when absent the
|
|
125
|
+
* verifier derives it from the transaction's own token outputs.
|
|
126
|
+
*/
|
|
127
|
+
topic?: string
|
|
123
128
|
outputsToAdmit: number[]
|
|
124
129
|
signature: Uint8Array
|
|
125
130
|
signerKey: string
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
* an explicit `refused`/`orphaned` verdict from the overlay.
|
|
28
28
|
*/
|
|
29
29
|
import { Beef, Transaction } from '@bsv/sdk'
|
|
30
|
-
import { MandalaToken } from '
|
|
30
|
+
import { MandalaToken } from '../mandala/token'
|
|
31
31
|
import type { EvidenceFrame, TokenFrameSource } from '../mandala/drain'
|
|
32
32
|
import type { OfflineActionRow } from '../storage/methods/offlineActions'
|
|
33
33
|
import { frameBytesFromQr, unsealFrame } from '../localpay/codec'
|
package/core/parsePeerPayURI.ts
CHANGED
|
@@ -54,8 +54,8 @@ export function peerPayHasErrors(result: PeerPayValidationResult): boolean {
|
|
|
54
54
|
|
|
55
55
|
const PEERPAY_SCHEME = 'peerpay:'
|
|
56
56
|
const COMPRESSED_PUBLIC_KEY_REGEX = /^0[23][0-9a-fA-F]{64}$/
|
|
57
|
-
/** A
|
|
58
|
-
const ASSET_OUTPOINT_REGEX = /^[0-9a-fA-F]{64}
|
|
57
|
+
/** A BRC-162 token id: the 32-byte deploy txid in hex, then `_0`. */
|
|
58
|
+
const ASSET_OUTPOINT_REGEX = /^[0-9a-fA-F]{64}_0$/
|
|
59
59
|
/** A whole non-negative figure with no sign, point or leading zero. */
|
|
60
60
|
const WHOLE_FIGURE_REGEX = /^(0|[1-9][0-9]*)$/
|
|
61
61
|
/**
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { Hash, Utils } from '@bsv/sdk'
|
|
2
|
+
import { WalletPermissionsManager } from '@bsv/wallet-toolbox-mobile'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Protocols a connected app may sign under without a protocol prompt, even
|
|
6
|
+
* though seekProtocolPermissionsForSigning is on.
|
|
7
|
+
*
|
|
8
|
+
* `auth message signature` is the one protocol @bsv/auth signs under: BRC-103
|
|
9
|
+
* mutual authentication and its BRC-104 HTTP transport (AuthFetch) both sign
|
|
10
|
+
* every handshake and message with it. Any site that talks to an
|
|
11
|
+
* authenticated server does this on its first request, so prompting for it
|
|
12
|
+
* would ask on nearly every connection. Names are compared the way
|
|
13
|
+
* KeyDeriver.computeInvoiceNumber normalizes them, since that is the key the
|
|
14
|
+
* signature is made with.
|
|
15
|
+
*/
|
|
16
|
+
export const PROMPT_FREE_SIGNING_PROTOCOLS = new Set(['auth message signature'])
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Protocols whose approval is never reused: every signature under them is
|
|
20
|
+
* asked for again, one call at a time. Each approval is still recorded as a
|
|
21
|
+
* permission token, tagged with the key ID it was given for, as a history of
|
|
22
|
+
* what the user approved; the manager just never looks those tokens up.
|
|
23
|
+
*
|
|
24
|
+
* BRC-29 (`3241645161d8`) is where this wallet keeps money: BSV address
|
|
25
|
+
* receipts (counterparty 'anyone', date key IDs), change (counterparty self)
|
|
26
|
+
* and payments from other parties. A grant is keyed by origin, protocol and
|
|
27
|
+
* counterparty, never key ID, so a reused approval for one app key would also
|
|
28
|
+
* cover every one of those. Asking per call keeps each signature the user's
|
|
29
|
+
* decision.
|
|
30
|
+
*/
|
|
31
|
+
export const ASK_EVERY_TIME_PROTOCOLS = new Set(['3241645161d8'])
|
|
32
|
+
|
|
33
|
+
type EnsureProtocolPermissionArgs = Parameters<WalletPermissionsManager['ensureProtocolPermission']>[0]
|
|
34
|
+
type CreateSignatureArgs = Parameters<WalletPermissionsManager['createSignature']>
|
|
35
|
+
type VerifySignatureArgs = Parameters<WalletPermissionsManager['verifySignature']>
|
|
36
|
+
|
|
37
|
+
function protocolName(protocolID: unknown): string | undefined {
|
|
38
|
+
return Array.isArray(protocolID) && typeof protocolID[1] === 'string' ? protocolID[1].toLowerCase().trim() : undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isPromptFreeSigning({ usageType, privileged, protocolID }: EnsureProtocolPermissionArgs): boolean {
|
|
42
|
+
const name = protocolName(protocolID)
|
|
43
|
+
return usageType === 'signing' && !privileged && name !== undefined && PROMPT_FREE_SIGNING_PROTOCOLS.has(name)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isAskEveryTimeProtocol(protocolID: unknown): boolean {
|
|
47
|
+
const name = protocolName(protocolID)
|
|
48
|
+
return name !== undefined && ASK_EVERY_TIME_PROTOCOLS.has(name)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Matches the manager's protocol cache key,
|
|
52
|
+
* `proto:<origin>:<privileged>:<level>,<name>:<counterparty>`, capturing the
|
|
53
|
+
* name. Origins may carry a port, so the match anchors on the privileged flag. */
|
|
54
|
+
const PROTOCOL_CACHE_KEY = /^proto:.*:(?:true|false):\d+,([^:]*):/
|
|
55
|
+
|
|
56
|
+
function isAskEveryTimeCacheKey(key: unknown): boolean {
|
|
57
|
+
const match = typeof key === 'string' ? PROTOCOL_CACHE_KEY.exec(key) : null
|
|
58
|
+
return match !== null && isAskEveryTimeProtocol([2, match[1]])
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Largest tag the wallet stores (validateTag: 1..300 bytes). */
|
|
62
|
+
const MAX_TAG_BYTES = 300
|
|
63
|
+
|
|
64
|
+
/** The token tag recording which key ID an approval was given for. Tags are
|
|
65
|
+
* lowercased by the wallet and key IDs are case-sensitive (often base64), so
|
|
66
|
+
* the key ID is stored as hex of its UTF-8 bytes, or as its SHA-256 when that
|
|
67
|
+
* would not fit in a tag. */
|
|
68
|
+
export function keyIDTag(keyID: string): string {
|
|
69
|
+
const bytes = Utils.toArray(keyID, 'utf8')
|
|
70
|
+
const tag = `keyid ${Utils.toHex(bytes)}`
|
|
71
|
+
return tag.length <= MAX_TAG_BYTES ? tag : `keyidhash ${Utils.toHex(Hash.sha256(bytes))}`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A protocol permission request with the key ID this class attaches to it. */
|
|
75
|
+
interface ProtocolRequest {
|
|
76
|
+
type?: string
|
|
77
|
+
originator?: string
|
|
78
|
+
protocolID?: unknown
|
|
79
|
+
keyID?: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The manager's internals this class reaches. They are private in the
|
|
83
|
+
* typings only; __tests__/services/signingPermissionPolicy.test.ts runs
|
|
84
|
+
* against the real manager, so an upgrade that renames one fails there. */
|
|
85
|
+
interface ManagerInternals {
|
|
86
|
+
prepareOriginator(originator: string): { normalized: string }
|
|
87
|
+
activeRequests: Map<string, { request: ProtocolRequest }>
|
|
88
|
+
findProtocolToken(originator: string, privileged: boolean, protocolID: unknown, ...rest: unknown[]): Promise<unknown>
|
|
89
|
+
markRecentGrant(request: ProtocolRequest): void
|
|
90
|
+
cachePermission(key: string, expiry: number): void
|
|
91
|
+
buildTagsForRequest(request: ProtocolRequest): string[]
|
|
92
|
+
firePermissionRequestEvent(request: ProtocolRequest, key: string): Promise<void>
|
|
93
|
+
underlying: { verifySignature: WalletPermissionsManager['verifySignature'] }
|
|
94
|
+
fetchManifestPermissions(originator: string): Promise<ManifestPermissions>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The parts of a site's manifest.json permissions the manager reads. */
|
|
98
|
+
interface ManifestPermissions {
|
|
99
|
+
groupPermissions: { protocolPermissions?: Array<{ protocolID?: unknown }> } | null
|
|
100
|
+
counterpartyPermissions: { protocols?: Array<{ protocolName?: unknown }> } | null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The manifest's permissions without ASK_EVERY_TIME_PROTOCOLS. A site's
|
|
104
|
+
* manifest may list them in its grouped (bulk) request, but such a grant is
|
|
105
|
+
* never kept, so the manager would find it missing on every call and raise
|
|
106
|
+
* the bulk sheet again before each signature prompt. */
|
|
107
|
+
function withoutAskEveryTime(manifest: ManifestPermissions): ManifestPermissions {
|
|
108
|
+
const { groupPermissions, counterpartyPermissions } = manifest
|
|
109
|
+
return {
|
|
110
|
+
groupPermissions:
|
|
111
|
+
groupPermissions?.protocolPermissions == null
|
|
112
|
+
? groupPermissions
|
|
113
|
+
: {
|
|
114
|
+
...groupPermissions,
|
|
115
|
+
protocolPermissions: groupPermissions.protocolPermissions.filter(
|
|
116
|
+
p => !isAskEveryTimeProtocol(p?.protocolID)
|
|
117
|
+
)
|
|
118
|
+
},
|
|
119
|
+
counterpartyPermissions:
|
|
120
|
+
counterpartyPermissions?.protocols == null
|
|
121
|
+
? counterpartyPermissions
|
|
122
|
+
: {
|
|
123
|
+
...counterpartyPermissions,
|
|
124
|
+
protocols: counterpartyPermissions.protocols.filter(p => !isAskEveryTimeProtocol([2, p?.protocolName]))
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* WalletPermissionsManager with this wallet's signing policy:
|
|
131
|
+
* - PROMPT_FREE_SIGNING_PROTOCOLS sign with no prompt;
|
|
132
|
+
* - ASK_EVERY_TIME_PROTOCOLS never reuse an approval. Each one is minted as
|
|
133
|
+
* a token tagged with its key ID, but no token, cache entry or recent-grant
|
|
134
|
+
* cover is ever read back for them; a site's manifest never puts them in a
|
|
135
|
+
* grouped request; and one origin's createSignature calls under them run
|
|
136
|
+
* one at a time, so a burst of calls cannot ride on one approval;
|
|
137
|
+
* - a non-privileged verifySignature is never prompted.
|
|
138
|
+
* Every other check runs unchanged.
|
|
139
|
+
*/
|
|
140
|
+
export class SigningPolicyPermissionsManager extends WalletPermissionsManager {
|
|
141
|
+
private readonly askEveryTimeQueues = new Map<string, Promise<unknown>>()
|
|
142
|
+
/** Key ID of the ask-every-time createSignature call each (normalized)
|
|
143
|
+
* origin has in flight. The per-origin queue keeps it to one at a time. */
|
|
144
|
+
private readonly keyIDInFlight = new Map<string, string>()
|
|
145
|
+
|
|
146
|
+
constructor(...args: ConstructorParameters<typeof WalletPermissionsManager>) {
|
|
147
|
+
super(...args)
|
|
148
|
+
const internals = this as unknown as ManagerInternals
|
|
149
|
+
const findProtocolToken = internals.findProtocolToken.bind(this)
|
|
150
|
+
internals.findProtocolToken = async (originator, privileged, protocolID, ...rest) =>
|
|
151
|
+
isAskEveryTimeProtocol(protocolID)
|
|
152
|
+
? undefined
|
|
153
|
+
: await findProtocolToken(originator, privileged, protocolID, ...rest)
|
|
154
|
+
const markRecentGrant = internals.markRecentGrant.bind(this)
|
|
155
|
+
internals.markRecentGrant = request => {
|
|
156
|
+
if (request?.type === 'protocol' && isAskEveryTimeProtocol(request.protocolID)) return
|
|
157
|
+
markRecentGrant(request)
|
|
158
|
+
}
|
|
159
|
+
const cachePermission = internals.cachePermission.bind(this)
|
|
160
|
+
internals.cachePermission = (key, expiry) => {
|
|
161
|
+
if (isAskEveryTimeCacheKey(key)) return
|
|
162
|
+
cachePermission(key, expiry)
|
|
163
|
+
}
|
|
164
|
+
const buildTagsForRequest = internals.buildTagsForRequest.bind(this)
|
|
165
|
+
internals.buildTagsForRequest = request => {
|
|
166
|
+
const tags = buildTagsForRequest(request)
|
|
167
|
+
return typeof request?.keyID === 'string' ? [...tags, keyIDTag(request.keyID)] : tags
|
|
168
|
+
}
|
|
169
|
+
// The request object is the one the manager keeps until it is answered,
|
|
170
|
+
// so the key ID set here reaches both the prompt (the event's arguments
|
|
171
|
+
// spread it) and the token a grant mints (buildTagsForRequest above).
|
|
172
|
+
const firePermissionRequestEvent = internals.firePermissionRequestEvent.bind(this)
|
|
173
|
+
internals.firePermissionRequestEvent = async (request, key) => {
|
|
174
|
+
if (request?.type === 'protocol' && isAskEveryTimeProtocol(request.protocolID) && request.originator) {
|
|
175
|
+
const keyID = this.keyIDInFlight.get(request.originator)
|
|
176
|
+
if (keyID !== undefined) request.keyID = keyID
|
|
177
|
+
}
|
|
178
|
+
return await firePermissionRequestEvent(request, key)
|
|
179
|
+
}
|
|
180
|
+
const fetchManifestPermissions = internals.fetchManifestPermissions.bind(this)
|
|
181
|
+
internals.fetchManifestPermissions = async originator =>
|
|
182
|
+
withoutAskEveryTime(await fetchManifestPermissions(originator))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private get internals(): ManagerInternals {
|
|
186
|
+
return this as unknown as ManagerInternals
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
override async ensureProtocolPermission(args: EnsureProtocolPermissionArgs): Promise<boolean> {
|
|
190
|
+
if (isPromptFreeSigning(args)) {
|
|
191
|
+
// Still refuse a missing or malformed originator, as the full check
|
|
192
|
+
// does first.
|
|
193
|
+
this.internals.prepareOriginator(args.originator)
|
|
194
|
+
return true
|
|
195
|
+
}
|
|
196
|
+
return await super.ensureProtocolPermission(args)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
override async createSignature(
|
|
200
|
+
...args: CreateSignatureArgs
|
|
201
|
+
): ReturnType<WalletPermissionsManager['createSignature']> {
|
|
202
|
+
return await this.oneAtATime(args[0], args[1], async () => await super.createSignature(...args))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Verifying a signature reveals nothing and spends nothing, so a
|
|
206
|
+
* non-privileged verification is answered without a protocol prompt. The
|
|
207
|
+
* originator is still validated, and admin-reserved (`admin…`) and
|
|
208
|
+
* module-routed (`p …`) protocols still take the manager's full path. */
|
|
209
|
+
override async verifySignature(
|
|
210
|
+
...args: VerifySignatureArgs
|
|
211
|
+
): ReturnType<WalletPermissionsManager['verifySignature']> {
|
|
212
|
+
const [request, originator] = args
|
|
213
|
+
const name = protocolName(request?.protocolID)
|
|
214
|
+
if (request?.privileged || name === undefined || name.startsWith('admin') || name.startsWith('p ')) {
|
|
215
|
+
return await super.verifySignature(...args)
|
|
216
|
+
}
|
|
217
|
+
this.internals.prepareOriginator(String(originator))
|
|
218
|
+
return await this.internals.underlying.verifySignature(request, originator)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Runs an ask-every-time createSignature call after the same origin's
|
|
223
|
+
* previous one. The manager hands every call already waiting on a request
|
|
224
|
+
* the same answer, so concurrent calls would otherwise share a single
|
|
225
|
+
* approval. While it runs, its key ID is what its prompt shows and what a
|
|
226
|
+
* grant of its request records.
|
|
227
|
+
*/
|
|
228
|
+
private async oneAtATime<T>(
|
|
229
|
+
args: { protocolID?: unknown; keyID?: string },
|
|
230
|
+
originator: string | undefined,
|
|
231
|
+
call: () => Promise<T>
|
|
232
|
+
): Promise<T> {
|
|
233
|
+
if (!isAskEveryTimeProtocol(args?.protocolID)) return await call()
|
|
234
|
+
let origin: string
|
|
235
|
+
try {
|
|
236
|
+
origin = this.internals.prepareOriginator(String(originator)).normalized
|
|
237
|
+
} catch {
|
|
238
|
+
return await call() // the manager refuses it with its own error
|
|
239
|
+
}
|
|
240
|
+
const previous = this.askEveryTimeQueues.get(origin) ?? Promise.resolve()
|
|
241
|
+
const current = previous
|
|
242
|
+
.catch(() => {})
|
|
243
|
+
.then(async () => {
|
|
244
|
+
this.keyIDInFlight.set(origin, String(args.keyID))
|
|
245
|
+
try {
|
|
246
|
+
return await call()
|
|
247
|
+
} finally {
|
|
248
|
+
this.keyIDInFlight.delete(origin)
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
this.askEveryTimeQueues.set(origin, current)
|
|
252
|
+
try {
|
|
253
|
+
return await current
|
|
254
|
+
} finally {
|
|
255
|
+
if (this.askEveryTimeQueues.get(origin) === current) this.askEveryTimeQueues.delete(origin)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -29,9 +29,8 @@
|
|
|
29
29
|
*/
|
|
30
30
|
import {
|
|
31
31
|
LockingScript,
|
|
32
|
-
PrivateKey,
|
|
33
|
-
PublicKey,
|
|
34
32
|
Transaction,
|
|
33
|
+
Utils,
|
|
35
34
|
Validation,
|
|
36
35
|
type ListActionsArgs,
|
|
37
36
|
type ListActionsResult,
|
|
@@ -121,56 +120,50 @@ const PRIVILEGED_CAPABLE = new Set<keyof WalletInterface>([
|
|
|
121
120
|
* own vault code ever reaches it. */
|
|
122
121
|
const VAULT_PROTOCOL_NAMES = new Set(['vault', 'vault salt', 'vault marker', 'vault descriptor', 'vault meta'])
|
|
123
122
|
|
|
124
|
-
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
|
|
144
|
-
|
|
123
|
+
/** Mandala's token protocol (`[2, 'p mandala token']`) is not reserved here:
|
|
124
|
+
* its `'p '` prefix routes every call under it to MandalaTokenModule
|
|
125
|
+
* (core/mandala/permissionModule.ts), which binds connected-app signatures to
|
|
126
|
+
* transactions the user approved.
|
|
127
|
+
*
|
|
128
|
+
* BRC-29 (`[2, '3241645161d8']`) was reserved here too (XR-020) and is not any
|
|
129
|
+
* more: it is the standard BRC-100 payment protocol, and refusing it broke
|
|
130
|
+
* every external payment app that signs under it. External signing under it
|
|
131
|
+
* goes through the permissions manager's protocol prompt instead
|
|
132
|
+
* (seekProtocolPermissionsForSigning in WalletContext.tsx), except for the
|
|
133
|
+
* address rail's own key IDs, which stay refused (isAddressRailKeyID). */
|
|
134
|
+
|
|
135
|
+
/** base64('legacy'), the suffix every address-rail key ID ends in
|
|
136
|
+
* (pay/rails/address.ts's LEGACY_DERIVATION_SUFFIX). */
|
|
137
|
+
const ADDRESS_RAIL_KEY_SUFFIX = Utils.toBase64(Utils.toArray('legacy', 'utf8'))
|
|
138
|
+
|
|
139
|
+
/** True for a key ID shaped like the BRC-29 address rail's own:
|
|
140
|
+
* `base64(YYYY-MM-DD) + ' ' + base64('legacy')` (pay/rails/address.ts's
|
|
141
|
+
* legacyKeyId). Those keys hold this wallet's BSV-address receipts, and anyone
|
|
142
|
+
* who knows a date can name one, so no connected origin has a reason to ask
|
|
143
|
+
* for them: refused outright, for every method and counterparty, whatever
|
|
144
|
+
* permission the user has given. KeyDeriver uses the key ID verbatim, so the
|
|
145
|
+
* comparison is exact. */
|
|
146
|
+
export function isAddressRailKeyID(keyID: unknown): boolean {
|
|
147
|
+
if (typeof keyID !== 'string') return false
|
|
148
|
+
const parts = keyID.split(' ')
|
|
149
|
+
if (parts.length !== 2 || parts[1] !== ADDRESS_RAIL_KEY_SUFFIX) return false
|
|
145
150
|
try {
|
|
146
|
-
|
|
151
|
+
const bytes = Utils.toArray(parts[0], 'base64')
|
|
152
|
+
if (Utils.toBase64(bytes) !== parts[0]) return false
|
|
153
|
+
return /^\d{4}-\d{2}-\d{2}$/.test(Utils.toUTF8(bytes))
|
|
147
154
|
} catch {
|
|
148
|
-
return
|
|
155
|
+
return false
|
|
149
156
|
}
|
|
150
157
|
}
|
|
151
158
|
|
|
152
|
-
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
* failure to read the identity key counts as a match, so the call is refused. */
|
|
161
|
-
async function derivesTowardAnotherParty(
|
|
162
|
-
getPublicKey: (args: any, originator?: string) => Promise<{ publicKey: string }>,
|
|
163
|
-
args: any,
|
|
164
|
-
adminOriginator: string
|
|
165
|
-
): Promise<boolean> {
|
|
166
|
-
const counterparty = counterpartyPublicKey(args?.counterparty)
|
|
167
|
-
if (counterparty === undefined || counterparty === ANYONE_PUBLIC_KEY) return false
|
|
168
|
-
try {
|
|
169
|
-
const own = counterpartyPublicKey((await getPublicKey({ identityKey: true }, adminOriginator))?.publicKey)
|
|
170
|
-
return own !== undefined && counterparty !== own
|
|
171
|
-
} catch {
|
|
172
|
-
return false
|
|
173
|
-
}
|
|
159
|
+
/** BRC-29's protocol name, under which the address rail derives. */
|
|
160
|
+
const BRC29_PROTOCOL_NAME = '3241645161d8'
|
|
161
|
+
|
|
162
|
+
function requestsAddressRailKey(args: unknown): boolean {
|
|
163
|
+
return (
|
|
164
|
+
matchesProtocolNamespace(args, new Set([BRC29_PROTOCOL_NAME])) &&
|
|
165
|
+
isAddressRailKeyID((args as { keyID?: unknown }).keyID)
|
|
166
|
+
)
|
|
174
167
|
}
|
|
175
168
|
|
|
176
169
|
/** XR-027: protocol namespace this package's own saved-pairing authority tag
|
|
@@ -213,10 +206,6 @@ function requestsVaultProtocol(args: unknown): boolean {
|
|
|
213
206
|
return matchesProtocolNamespace(args, VAULT_PROTOCOL_NAMES)
|
|
214
207
|
}
|
|
215
208
|
|
|
216
|
-
function requestsReservedRailProtocol(args: unknown): boolean {
|
|
217
|
-
return matchesProtocolNamespace(args, RESERVED_RAIL_PROTOCOL_NAMES)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
209
|
function requestsConnectionAuthorityProtocol(args: unknown): boolean {
|
|
221
210
|
return matchesProtocolNamespace(args, CONNECTION_AUTHORITY_PROTOCOL_NAMES)
|
|
222
211
|
}
|
|
@@ -705,28 +694,15 @@ export function guardVaultAccess<T extends WalletInterface>(
|
|
|
705
694
|
|
|
706
695
|
if (!PRIVILEGED_CAPABLE.has(method) && !OUTPUT_NAMING.has(method)) return value.bind(target)
|
|
707
696
|
|
|
708
|
-
return async (
|
|
709
|
-
if (originator === adminOriginator) return await bound(
|
|
710
|
-
// The reserved-rail getPublicKey check awaits before deriving. Decide
|
|
711
|
-
// on, and derive from, one plain copy so an accessor on the caller's
|
|
712
|
-
// object cannot answer the check with one counterparty and the
|
|
713
|
-
// derivation with another.
|
|
714
|
-
let args = rawArgs
|
|
715
|
-
if (method === 'getPublicKey' && rawArgs !== null && typeof rawArgs === 'object') {
|
|
716
|
-
try {
|
|
717
|
-
args = JSON.parse(JSON.stringify(rawArgs))
|
|
718
|
-
} catch {
|
|
719
|
-
return deny(String(method), originator)
|
|
720
|
-
}
|
|
721
|
-
}
|
|
697
|
+
return async (args: any, originator?: string) => {
|
|
698
|
+
if (originator === adminOriginator) return await bound(args, originator)
|
|
722
699
|
if (
|
|
723
700
|
PRIVILEGED_CAPABLE.has(method) &&
|
|
724
701
|
(args?.privileged ||
|
|
725
702
|
requestsVaultProtocol(args) ||
|
|
726
703
|
requestsConnectionAuthorityProtocol(args) ||
|
|
727
704
|
requestsPendingAbortAuthorityProtocol(args) ||
|
|
728
|
-
(
|
|
729
|
-
!(method === 'getPublicKey' && (await derivesTowardAnotherParty(bound, args, adminOriginator)))))
|
|
705
|
+
requestsAddressRailKey(args))
|
|
730
706
|
) {
|
|
731
707
|
return deny(String(method), originator)
|
|
732
708
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsv/expo-wallet-toolbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Wallet screens and P2P local-payment backend for Expo/React Native apps, extracted from BSV Wallet.",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
"@bsv/air-gap": "^0.1.3",
|
|
33
33
|
"@bsv/backup-cache-client": "^0.1.0",
|
|
34
34
|
"@bsv/btms-permission-module": "^1.2.1",
|
|
35
|
-
"@bsv/mandala": "^0.
|
|
35
|
+
"@bsv/mandala": "^0.4.0",
|
|
36
36
|
"@bsv/message-box-client": "^2.5.3",
|
|
37
37
|
"@bsv/react-native-localpay-transport": "*",
|
|
38
|
-
"@bsv/sdk": "^2.8.
|
|
39
|
-
"@bsv/templates": "^
|
|
38
|
+
"@bsv/sdk": "^2.8.11",
|
|
39
|
+
"@bsv/templates": "^2.0.0",
|
|
40
40
|
"@bsv/wallet-toolbox-mobile": "^2.14.3",
|
|
41
41
|
"@expo/vector-icons": "^15.0.3",
|
|
42
42
|
"@noble/curves": "^2.3.0",
|
|
@@ -5,6 +5,7 @@ import Sheet from './Sheet'
|
|
|
5
5
|
import PressableScale from './PressableScale'
|
|
6
6
|
import { useTranslation } from 'react-i18next'
|
|
7
7
|
import AmountDisplay from '../wallet/AmountDisplay'
|
|
8
|
+
import { describeProtocolPrompt } from './protocolPrompt'
|
|
8
9
|
import {
|
|
9
10
|
spacing,
|
|
10
11
|
radii,
|
|
@@ -250,17 +251,22 @@ export function deriveActive(
|
|
|
250
251
|
|
|
251
252
|
if (ctx.protocolAccessModalOpen && ctx.protocolRequests.length > 0) {
|
|
252
253
|
const r = ctx.protocolRequests[0]
|
|
254
|
+
const prompt = describeProtocolPrompt(r.protocolID ?? '', r.counterparty, r.keyID)
|
|
255
|
+
const details: { label: string; value: string }[] = [
|
|
256
|
+
{ label: 'Protocol', value: truncate(prompt.protocolLabel, 40) },
|
|
257
|
+
{ label: 'Security level', value: String(r.protocolSecurityLevel) }
|
|
258
|
+
]
|
|
259
|
+
if (prompt.counterpartyLabel) details.push({ label: 'Shared with', value: prompt.counterpartyLabel })
|
|
260
|
+
if (prompt.keyIDLabel) details.push({ label: 'Key ID', value: truncate(prompt.keyIDLabel, 40) })
|
|
261
|
+
if (prompt.askEveryTime) details.push({ label: 'Approval', value: 'This signature only' })
|
|
253
262
|
return {
|
|
254
263
|
kind: 'protocol',
|
|
255
264
|
requestID: r.requestID,
|
|
256
265
|
originator: r.originator || 'Unknown app',
|
|
257
|
-
title: r.renewal ?
|
|
258
|
-
description: r.description || 'wants to use a cryptographic protocol',
|
|
266
|
+
title: r.renewal ? `${prompt.title} Renewal` : prompt.title,
|
|
267
|
+
description: prompt.description ?? (r.description || 'wants to use a cryptographic protocol'),
|
|
259
268
|
renewal: r.renewal,
|
|
260
|
-
details
|
|
261
|
-
{ label: 'Protocol ID', value: truncate(r.protocolID, 28) },
|
|
262
|
-
{ label: 'Security level', value: String(r.protocolSecurityLevel) }
|
|
263
|
-
]
|
|
269
|
+
details
|
|
264
270
|
}
|
|
265
271
|
}
|
|
266
272
|
|
|
@@ -373,6 +379,8 @@ export function deriveActive(
|
|
|
373
379
|
description = 'wants to remove a Mandala token holding from your wallet'
|
|
374
380
|
} else if (promptData.type === 'mandala_access') {
|
|
375
381
|
description = 'wants to see your Mandala token balance'
|
|
382
|
+
} else if (promptData.type === 'mandala_signature') {
|
|
383
|
+
description = 'wants to sign with one of your token keys. Only approve if you just asked this app to move tokens'
|
|
376
384
|
} else {
|
|
377
385
|
description = 'wants to access Mandala tokens'
|
|
378
386
|
}
|
|
@@ -807,7 +815,9 @@ const PermissionSheet: React.FC = () => {
|
|
|
807
815
|
</Text>
|
|
808
816
|
{active.groupPermissions.protocolPermissions.map((p, i) => (
|
|
809
817
|
<View key={i} style={styles.groupRow}>
|
|
810
|
-
<Text style={[styles.groupRowLabel, { color: colors.textPrimary }]}>
|
|
818
|
+
<Text style={[styles.groupRowLabel, { color: colors.textPrimary }]}>
|
|
819
|
+
{describeProtocolPrompt(p.protocolID[1], p.counterparty).protocolLabel}
|
|
820
|
+
</Text>
|
|
811
821
|
{p.description && (
|
|
812
822
|
<Text style={[styles.groupRowDesc, { color: colors.textSecondary }]}>{p.description}</Text>
|
|
813
823
|
)}
|