@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.
@@ -0,0 +1,118 @@
1
+ import { PrivateKey, PublicKey, Utils } from '@bsv/sdk'
2
+
3
+ /** BRC-29's protocol name. Apps sign under it to receive and move payments,
4
+ * and this wallet's own payment keys (BSV address receipts, change) live
5
+ * under it too, so a grant here is a grant over money. */
6
+ const BRC29_PROTOCOL_NAME = '3241645161d8'
7
+
8
+ /** The BRC-42 'anyone' counterparty (1·G), in canonical form. */
9
+ const ANYONE_PUBLIC_KEY = new PrivateKey(1).toPublicKey().toString()
10
+
11
+ type Counterparty = { kind: 'anyone' } | { kind: 'self' } | { kind: 'party'; key: string }
12
+
13
+ /** The protocol grant's counterparty as the permissions manager reports it:
14
+ * 'self' when the app named none, 'anyone' as the literal or as 1·G in any
15
+ * encoding, otherwise another party's key. */
16
+ function classifyCounterparty(counterparty: unknown): Counterparty {
17
+ if (counterparty === undefined || counterparty === null || counterparty === '' || counterparty === 'self') {
18
+ return { kind: 'self' }
19
+ }
20
+ if (counterparty === 'anyone') return { kind: 'anyone' }
21
+ const raw = String(counterparty)
22
+ try {
23
+ const key = PublicKey.fromString(raw).toString()
24
+ return key === ANYONE_PUBLIC_KEY ? { kind: 'anyone' } : { kind: 'party', key }
25
+ } catch {
26
+ return { kind: 'party', key: raw }
27
+ }
28
+ }
29
+
30
+ function abbreviate(key: string): string {
31
+ return key.length <= 13 ? key : key.slice(0, 8) + '…' + key.slice(-4)
32
+ }
33
+
34
+ /** One space-separated part of a key ID as text: the decoded value when the
35
+ * part is canonical base64 of printable ASCII (BRC-29 key IDs are usually
36
+ * base64 prefix + ' ' + base64 suffix), otherwise the part as given. */
37
+ function readablePart(part: string): string {
38
+ try {
39
+ const bytes = Utils.toArray(part, 'base64')
40
+ if (bytes.length === 0 || Utils.toBase64(bytes) !== part) return part
41
+ if (!bytes.every(b => b >= 0x20 && b <= 0x7e)) return part
42
+ return Utils.toUTF8(bytes)
43
+ } catch {
44
+ return part
45
+ }
46
+ }
47
+
48
+ /** A key ID as a person can read it, so an app's fixed key ID ("xana-earnings
49
+ * 1") is visibly not one of the wallet's own. */
50
+ export function readableKeyID(keyID: string): string {
51
+ return keyID.split(' ').map(readablePart).join(' ')
52
+ }
53
+
54
+ export interface ProtocolPrompt {
55
+ /** Sheet title, before any "Renewal" suffix. */
56
+ title: string
57
+ /** What approving lets the app do, completing "<app> …". Undefined means
58
+ * the caller's own text (or its generic fallback) is used. */
59
+ description?: string
60
+ /** Name to show for the protocol in place of its raw ID. */
61
+ protocolLabel: string
62
+ /** Who the keys are shared with, when that is worth naming. */
63
+ counterpartyLabel?: string
64
+ /** The key ID asked for, readable, when the request carries it. */
65
+ keyIDLabel?: string
66
+ /** The approval covers this one call and is never reused
67
+ * (core/services/signingPermissionPolicy.ts's ASK_EVERY_TIME_PROTOCOLS). */
68
+ askEveryTime?: boolean
69
+ }
70
+
71
+ /**
72
+ * Plain-language copy for a protocol permission request. Most protocol names
73
+ * are an app's own words and are shown as they are. BRC-29's is an opaque
74
+ * hex ID, and approving it lets the app sign with a key that can hold money,
75
+ * so it gets its own title and a description that says which money. The
76
+ * address rail's own key IDs never reach a prompt (vault guard's
77
+ * isAddressRailKeyID refuses them), so the copy describes the app's key.
78
+ */
79
+ export function describeProtocolPrompt(protocolName: string, counterparty?: unknown, keyID?: unknown): ProtocolPrompt {
80
+ if (protocolName.toLowerCase().trim() !== BRC29_PROTOCOL_NAME) {
81
+ return { title: 'Protocol Access', protocolLabel: protocolName }
82
+ }
83
+ const party = classifyCounterparty(counterparty)
84
+ const protocolLabel = 'Payments (BRC-29)'
85
+ const keyIDLabel = typeof keyID === 'string' ? readableKeyID(keyID) : undefined
86
+ switch (party.kind) {
87
+ case 'anyone':
88
+ return {
89
+ title: 'Payment Key Signature',
90
+ description:
91
+ 'wants a signature from one of your payment keys. Coins paid to this key could be spent with this signature.',
92
+ protocolLabel,
93
+ askEveryTime: true,
94
+ counterpartyLabel: 'Anyone',
95
+ keyIDLabel
96
+ }
97
+ case 'self':
98
+ return {
99
+ title: 'Payment Key Signature',
100
+ description:
101
+ "wants a signature from one of your wallet's own payment keys. Your change could be spent with this signature.",
102
+ protocolLabel,
103
+ askEveryTime: true,
104
+ counterpartyLabel: 'Only you',
105
+ keyIDLabel
106
+ }
107
+ case 'party':
108
+ return {
109
+ title: 'Payment Key Signature',
110
+ description:
111
+ 'wants a signature from a payment key you share with another party. Coins paid to this key could be spent with this signature.',
112
+ protocolLabel,
113
+ askEveryTime: true,
114
+ counterpartyLabel: abbreviate(party.key),
115
+ keyIDLabel
116
+ }
117
+ }
118
+ }