@fairmint/canton-node-sdk 0.0.277 → 0.0.278

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.
Files changed (44) hide show
  1. package/README.md +69 -6
  2. package/build/src/utils/external-signing/index.d.ts +1 -0
  3. package/build/src/utils/external-signing/index.d.ts.map +1 -1
  4. package/build/src/utils/external-signing/index.js +1 -0
  5. package/build/src/utils/external-signing/index.js.map +1 -1
  6. package/build/src/utils/external-signing/wallet-ownership.d.ts +69 -0
  7. package/build/src/utils/external-signing/wallet-ownership.d.ts.map +1 -0
  8. package/build/src/utils/external-signing/wallet-ownership.js +179 -0
  9. package/build/src/utils/external-signing/wallet-ownership.js.map +1 -0
  10. package/build/src/wallets/cip103.d.ts +83 -5
  11. package/build/src/wallets/cip103.d.ts.map +1 -1
  12. package/build/src/wallets/cip103.js +229 -107
  13. package/build/src/wallets/cip103.js.map +1 -1
  14. package/build/src/wallets/da-utilities/client.d.ts.map +1 -1
  15. package/build/src/wallets/da-utilities/client.js +3 -0
  16. package/build/src/wallets/da-utilities/client.js.map +1 -1
  17. package/build/src/wallets/errors.d.ts +18 -0
  18. package/build/src/wallets/errors.d.ts.map +1 -1
  19. package/build/src/wallets/errors.js +15 -1
  20. package/build/src/wallets/errors.js.map +1 -1
  21. package/build/src/wallets/index.d.ts +9 -6
  22. package/build/src/wallets/index.d.ts.map +1 -1
  23. package/build/src/wallets/index.js +10 -3
  24. package/build/src/wallets/index.js.map +1 -1
  25. package/build/src/wallets/ownership.d.ts +79 -0
  26. package/build/src/wallets/ownership.d.ts.map +1 -0
  27. package/build/src/wallets/ownership.js +202 -0
  28. package/build/src/wallets/ownership.js.map +1 -0
  29. package/build/src/wallets/templates.d.ts +15 -0
  30. package/build/src/wallets/templates.d.ts.map +1 -1
  31. package/build/src/wallets/templates.js +33 -9
  32. package/build/src/wallets/templates.js.map +1 -1
  33. package/build/src/wallets/token-registry.d.ts +11 -0
  34. package/build/src/wallets/token-registry.d.ts.map +1 -1
  35. package/build/src/wallets/token-registry.js.map +1 -1
  36. package/build/src/wallets/token-standard-wallet.d.ts +47 -6
  37. package/build/src/wallets/token-standard-wallet.d.ts.map +1 -1
  38. package/build/src/wallets/token-standard-wallet.js +91 -15
  39. package/build/src/wallets/token-standard-wallet.js.map +1 -1
  40. package/build/src/wallets/token-wallet.d.ts +12 -1
  41. package/build/src/wallets/token-wallet.d.ts.map +1 -1
  42. package/build/src/wallets/token-wallet.js +40 -30
  43. package/build/src/wallets/token-wallet.js.map +1 -1
  44. package/package.json +1 -1
package/README.md CHANGED
@@ -72,12 +72,21 @@ See
72
72
  ## Token actions through a CIP-0103 wallet
73
73
 
74
74
  `Wallets` runs token standard (CIP-56) holder actions on Digital Asset Utility Registry instruments
75
- through any CIP-0103 (Canton dApp API) wallet provider: list holdings, mint offers, transfer offers
76
- and burn requests; accept a mint offer; preview and send a transfer; accept, reject or withdraw a
77
- transfer offer; request a burn; and create a transfer preapproval (experimental). It reads choice
78
- contexts from DA's public Utilities API with an injectable `fetch`, and talks to the wallet only
79
- through CIP-0103 (`getPrimaryAccount`, `ledgerApi`, `prepareExecuteAndWait`), so no wallet SDK is
80
- involved.
75
+ through any CIP-0103 (Canton dApp API) wallet provider:
76
+
77
+ - read the wallet's state in one snapshot (`loadState`: holdings, mint offers, transfer offers and
78
+ burn requests), or each list on its own;
79
+ - accept a mint offer (`acceptMintOffer`);
80
+ - preview and send a transfer, and accept, reject or withdraw a transfer offer
81
+ (`acceptTransferOffer`, `rejectTransferOffer`, `withdrawTransferOffer`);
82
+ - propose a burn (`requestBurn`: a burn request the registrar accepts or rejects);
83
+ - create a transfer preapproval (experimental);
84
+ - prove that the user controls the wallet's party by signing a message (`proveWalletOwnership`,
85
+ verified on your server with `verifyWalletOwnership`).
86
+
87
+ It reads choice contexts from DA's public Utilities API with an injectable `fetch`, and talks to the
88
+ wallet only through CIP-0103 (`getPrimaryAccount`, `ledgerApi`, `prepareExecuteAndWait`,
89
+ `signMessage`), so no wallet SDK is involved.
81
90
 
82
91
  It is browser-safe: import `@fairmint/canton-node-sdk/build/src/wallets/index.js`, which loads none
83
92
  of the SDK's Node dependencies. The package root also exports it as the `Wallets` namespace.
@@ -100,6 +109,22 @@ async function send(provider: Cip103Provider, receiver: string): Promise<void> {
100
109
  }
101
110
  ```
102
111
 
112
+ - Load a page with one `loadState()` call: one `/v2/state/ledger-end` call and one paged
113
+ `/v2/state/active-contracts-page` stream whose cumulative filter carries every template and
114
+ interface. Concurrent `loadState` and `list*` calls on one token wallet share the snapshot in
115
+ flight (unless a write settled since it started). Writes do their own fresh reads.
116
+ - Wallets rate-limit `ledgerApi` (Loop answers CIP-0103 -32005 for an HTTP 429). Loop also makes a
117
+ session check and a provider status check before every `ledgerApi` call, so each call is three
118
+ HTTP requests against Loop's limit; a 429 of the session check arrives as -32603 "Session
119
+ verification failed with status 429." and counts as a rate limit too. Rate-limited reads are
120
+ retried after 0.5, 1, 2 and 4 s (jittered; `readRetry` changes it); then they fail with a
121
+ `WalletReadError` whose `rateLimited` is true, so a UI can offer a retry.
122
+ - The unpaged `/v2/state/active-contracts` is read once instead only when the first paged call is
123
+ refused as not offered or malformed (4200, -32601, -32004, -32602), or when Loop answers -32002
124
+ "Ledger access unavailable", which is how Loop reports any non-429 HTTP failure of its ledger
125
+ proxy (an endpoint not in its allowlist, a Canton 3.5 participant serving the paged endpoint only
126
+ as GET, but also a server error). If the fallback fails too, the error names both failures. A rate
127
+ limit or a timeout never falls back.
103
128
  - Writes never retry. A `WalletSubmitError` has `outcome` `rejected-by-user` (CIP-0103 4001),
104
129
  `failed` (-32003, or a request the wallet refused before taking it on) or `unknown` (session
105
130
  errors 4100/4900/4901 during a submit, timeouts, anything else): check the wallet before
@@ -114,6 +139,44 @@ async function send(provider: Cip103Provider, receiver: string): Promise<void> {
114
139
  deterministically. `requirePreapproval: true` sends only when the receiver pre-approved the
115
140
  transfer, and `expectedKind` refuses a transfer whose kind changed since the preview.
116
141
 
142
+ ### Proving wallet ownership
143
+
144
+ A connected wallet only tells the dApp a party id, which anyone can claim. To know the user controls
145
+ the party, the server issues a challenge, the wallet signs it, and the server verifies the
146
+ signature. A check in the browser proves nothing.
147
+
148
+ 1. The server issues `{ domain, nonce, issuedAt, expiresAt, network? }`: its own host, a random
149
+ single-use nonce it stores, and a short validity window (at most 10 minutes by default).
150
+ 2. The browser calls `proveWalletOwnership(provider, { challenge })`. The wallet signs, as its
151
+ primary account, a canonical text message: a fixed title, a statement that it is not a
152
+ transaction and authorizes nothing, then the domain, party, network, nonce and times. Plain text
153
+ with a fixed prefix can never be a Canton transaction or topology hash (34 binary bytes), which
154
+ the same key signs.
155
+ 3. The server calls
156
+ `verifyWalletOwnership(proof, { expectedNonce, expectedDomain, expectedNetwork })` (Node only,
157
+ from the package root, not the browser entry), then marks the nonce used.
158
+
159
+ `verified: true` means: the message is exactly canonical, for this domain, nonce and network, and
160
+ within its window; the Ed25519 signature over its UTF-8 bytes verifies; and the key's Canton
161
+ fingerprint (`1220` + SHA-256 of purpose 12 and the raw key) is the party's namespace, which is how
162
+ an external party's id is derived from its key. Otherwise the result gives a `reason`. A party not
163
+ namespaced by its signing key (a participant-hosted party, or an external party whose key was
164
+ rotated or delegated) fails with `key-not-bound-to-party`: proving it would need a ledger topology
165
+ lookup (its `PartyToKeyMapping` and namespace delegations), which the verifier does not do.
166
+
167
+ CIP-0103 does not fix the signature format. The verifier accepts what the known wallets send and
168
+ refuses anything else:
169
+
170
+ - Loop (from its wallet code, not its documentation): Ed25519 over the UTF-8 message bytes, a
171
+ lowercase hex signature, and a lowercase hex raw 32-byte public key. Not yet confirmed against a
172
+ live DevNet signature: that Loop's backend relays the hex signature unchanged and that
173
+ `getPrimaryAccount` returns the key in the same hex form.
174
+ - The CIP-0103 reference wallet (splice-wallet-kernel): the same signature scheme, with base64
175
+ signature and key.
176
+ - Keys: 64 hex characters, base64 or base64url of the raw key or of its DER SubjectPublicKeyInfo, 88
177
+ hex characters of DER, or PEM. Signatures: 128 hex characters, or base64/base64url of 64 bytes.
178
+ ECDSA keys are refused.
179
+
117
180
  ## Repository setup and checks
118
181
 
119
182
  ```bash
@@ -10,4 +10,5 @@ export * from './external-signing-client';
10
10
  export * from './generate-external-party';
11
11
  export * from './prepare-external-transaction';
12
12
  export * from './stellar-utils';
13
+ export * from './wallet-ownership';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utils/external-signing/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utils/external-signing/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
@@ -26,4 +26,5 @@ __exportStar(require("./external-signing-client"), exports);
26
26
  __exportStar(require("./generate-external-party"), exports);
27
27
  __exportStar(require("./prepare-external-transaction"), exports);
28
28
  __exportStar(require("./stellar-utils"), exports);
29
+ __exportStar(require("./wallet-ownership"), exports);
29
30
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/external-signing/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,0DAAwC;AACxC,oDAAkC;AAClC,0DAAwC;AACxC,iEAA+C;AAC/C,6DAA2C;AAC3C,8DAA4C;AAC5C,0DAAwC;AACxC,4DAA0C;AAC1C,4DAA0C;AAC1C,iEAA+C;AAC/C,kDAAgC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/external-signing/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,0DAAwC;AACxC,oDAAkC;AAClC,0DAAwC;AACxC,iEAA+C;AAC/C,6DAA2C;AAC3C,8DAA4C;AAC5C,0DAAwC;AACxC,4DAA0C;AAC1C,4DAA0C;AAC1C,iEAA+C;AAC/C,kDAAgC;AAChC,qDAAmC"}
@@ -0,0 +1,69 @@
1
+ /** Why a proof did not verify. */
2
+ export type WalletOwnershipFailure =
3
+ /** The proof is not an object of string fields. */
4
+ 'malformed-proof'
5
+ /** The message is not exactly a canonical ownership message. */
6
+ | 'malformed-message' | 'domain-mismatch' | 'nonce-mismatch'
7
+ /** The message, the proof and the expected party do not all name the same party, or the namespace differs. */
8
+ | 'party-mismatch' | 'network-mismatch'
9
+ /** The message's `Issued At` or `Expiration Time` is not the one the server issued. */
10
+ | 'window-mismatch'
11
+ /** Issued in the future (beyond the allowed clock skew). */
12
+ | 'not-yet-valid'
13
+ /** Past its expiration time. */
14
+ | 'expired'
15
+ /** The message allows a longer validity window than `maxAgeMs`. */
16
+ | 'lifetime-too-long'
17
+ /** The public key is not an Ed25519 key in an encoding this verifier accepts. */
18
+ | 'unsupported-public-key'
19
+ /** The key's fingerprint is not the party's namespace (see the module comment). */
20
+ | 'key-not-bound-to-party'
21
+ /** The signature is not 64 bytes of hex or base64. */
22
+ | 'malformed-signature'
23
+ /** The signature does not verify. */
24
+ | 'bad-signature';
25
+ export type WalletOwnershipVerification = {
26
+ readonly verified: true;
27
+ readonly partyId: string;
28
+ /** The party's namespace, which is the key's Canton fingerprint. */
29
+ readonly publicKeyFingerprint: string;
30
+ readonly domain: string;
31
+ readonly nonce: string;
32
+ /** The message's network label, as text; not proof that the party is on that network (see the module comment). */
33
+ readonly network?: string;
34
+ readonly issuedAt: string;
35
+ readonly expiresAt: string;
36
+ } | {
37
+ readonly verified: false;
38
+ readonly reason: WalletOwnershipFailure;
39
+ /** A sentence for logs or the user. */
40
+ readonly message: string;
41
+ };
42
+ export interface VerifyWalletOwnershipOptions {
43
+ /** The nonce the server issued for this proof (never the one in the proof). */
44
+ readonly expectedNonce: string;
45
+ /** The server's own host, as the challenge named it. */
46
+ readonly expectedDomain: string;
47
+ /** Required in the message when given. A label compared as text, not proof (see the module comment). */
48
+ readonly expectedNetwork?: string;
49
+ /** The `issuedAt` the server issued; the message must repeat it exactly when given. */
50
+ readonly expectedIssuedAt?: string;
51
+ /** The `expiresAt` the server issued; the message must repeat it exactly when given. */
52
+ readonly expectedExpiresAt?: string;
53
+ /** The party the user claims; the proof must be for it. */
54
+ readonly expectedPartyId?: string;
55
+ /** Defaults to the system clock. */
56
+ readonly now?: Date;
57
+ /** The longest validity window accepted (`expiresAt - issuedAt`). Defaults to 10 minutes. */
58
+ readonly maxAgeMs?: number;
59
+ /** How far in the future `issuedAt` may be. Defaults to 60 seconds. */
60
+ readonly clockSkewMs?: number;
61
+ }
62
+ export declare const DEFAULT_WALLET_OWNERSHIP_MAX_AGE_MS: number;
63
+ export declare const DEFAULT_WALLET_OWNERSHIP_CLOCK_SKEW_MS: number;
64
+ /**
65
+ * Verify a wallet ownership proof (see the module comment for what this does and does not establish). Never throws for
66
+ * a bad proof: the result says why it failed. Throws `TypeError` for invalid options.
67
+ */
68
+ export declare function verifyWalletOwnership(proof: unknown, options: VerifyWalletOwnershipOptions): WalletOwnershipVerification;
69
+ //# sourceMappingURL=wallet-ownership.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-ownership.d.ts","sourceRoot":"","sources":["../../../../src/utils/external-signing/wallet-ownership.ts"],"names":[],"mappings":"AAmCA,kCAAkC;AAClC,MAAM,MAAM,sBAAsB;AAChC,mDAAmD;AACjD,iBAAiB;AACnB,gEAAgE;GAC9D,mBAAmB,GACnB,iBAAiB,GACjB,gBAAgB;AAClB,8GAA8G;GAC5G,gBAAgB,GAChB,kBAAkB;AACpB,uFAAuF;GACrF,iBAAiB;AACnB,4DAA4D;GAC1D,eAAe;AACjB,gCAAgC;GAC9B,SAAS;AACX,mEAAmE;GACjE,mBAAmB;AACrB,iFAAiF;GAC/E,wBAAwB;AAC1B,mFAAmF;GACjF,wBAAwB;AAC1B,sDAAsD;GACpD,qBAAqB;AACvB,qCAAqC;GACnC,eAAe,CAAC;AAEpB,MAAM,MAAM,2BAA2B,GACnC;IACE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,oEAAoE;IACpE,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,kHAAkH;IAClH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,GACD;IACE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,sBAAsB,CAAC;IACxC,uCAAuC;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEN,MAAM,WAAW,4BAA4B;IAC3C,+EAA+E;IAC/E,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,wDAAwD;IACxD,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,wGAAwG;IACxG,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,uFAAuF;IACvF,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,wFAAwF;IACxF,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,2DAA2D;IAC3D,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,oCAAoC;IACpC,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC;IACpB,6FAA6F;IAC7F,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,uEAAuE;IACvE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,eAAO,MAAM,mCAAmC,QAAiB,CAAC;AAClE,eAAO,MAAM,sCAAsC,QAAY,CAAC;AAqDhE;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,4BAA4B,GACpC,2BAA2B,CAwG7B"}
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_WALLET_OWNERSHIP_CLOCK_SKEW_MS = exports.DEFAULT_WALLET_OWNERSHIP_MAX_AGE_MS = void 0;
4
+ exports.verifyWalletOwnership = verifyWalletOwnership;
5
+ /**
6
+ * Server-side verification of a wallet ownership proof (`proveWalletOwnership` in the browser-safe wallets module).
7
+ * Node only: it uses `node:crypto` through the Canton signing helpers, so it is not exported from the wallets entry.
8
+ *
9
+ * What a `verified` result means: the party's own key signed the canonical message for this domain and this nonce,
10
+ * within its validity window. It rests on two facts this function checks itself, trusting nothing the browser says:
11
+ *
12
+ * 1. The Ed25519 signature over the UTF-8 bytes of the message verifies against the submitted public key.
13
+ * 2. The Canton fingerprint of that key (`1220` + SHA-256 of purpose 12 and the raw key) is the party's namespace (the
14
+ * part of the party id after `::`), which is how an external party's id is derived from its key. Loop's parties and
15
+ * the CIP-0103 reference wallet's are external parties of this kind.
16
+ *
17
+ * A party whose namespace is not its signing key's fingerprint (a participant-hosted party, an external party whose key
18
+ * was rotated, or a namespace delegation) fails with `key-not-bound-to-party`. Proving those would need the party's
19
+ * current topology from the ledger (its `PartyToKeyMapping` signing keys, and the namespace delegations that authorize
20
+ * them), which this function does not read.
21
+ *
22
+ * The caller must issue the nonce, store it, pass it as `expectedNonce`, and accept it once: this function cannot tell
23
+ * a replayed proof from a fresh one. Passing the issued times as `expectedIssuedAt` and `expectedExpiresAt` makes the
24
+ * signed text exactly the issued challenge.
25
+ *
26
+ * The message's `Network:` line (`expectedNetwork`, the result's `network`) is checked as text only. A party id is
27
+ * derived from its key and is the same on every network, so a verified proof does not show the party exists or is
28
+ * hosted on that network; a caller that needs that must look the party up on the network (Scan, or the ledger's party
29
+ * and topology APIs).
30
+ */
31
+ const canton_protocol_1 = require("./canton-protocol");
32
+ const canton_ed25519_signer_1 = require("./canton-ed25519-signer");
33
+ const ownership_1 = require("../../wallets/ownership");
34
+ exports.DEFAULT_WALLET_OWNERSHIP_MAX_AGE_MS = 10 * 60 * 1000;
35
+ exports.DEFAULT_WALLET_OWNERSHIP_CLOCK_SKEW_MS = 60 * 1000;
36
+ const ED25519_DER_PREFIX_HEX = '302a300506032b6570032100';
37
+ /**
38
+ * The key as base64 for the Canton helpers: 64 hex characters (raw, as Loop registers it), 88 hex characters (DER
39
+ * SubjectPublicKeyInfo), a PEM public key, or base64/base64url of the raw or DER key (as the CIP-0103 reference wallet
40
+ * sends it). The helpers then accept only a 32-byte key or the exact Ed25519 DER prefix.
41
+ */
42
+ function publicKeyBase64(value) {
43
+ const trimmed = value.trim();
44
+ const hex = trimmed.replace(/^0x/i, '');
45
+ if (/^[0-9a-fA-F]+$/.test(hex) &&
46
+ (hex.length === 64 || (hex.length === 88 && hex.toLowerCase().startsWith(ED25519_DER_PREFIX_HEX)))) {
47
+ return Buffer.from(hex, 'hex').toString('base64');
48
+ }
49
+ const pem = /^-----BEGIN PUBLIC KEY-----\s*([A-Za-z0-9+/=\s]+?)\s*-----END PUBLIC KEY-----$/.exec(trimmed);
50
+ return pem?.[1] !== undefined ? pem[1].replace(/\s+/g, '') : trimmed;
51
+ }
52
+ /** The signature as base64: 128 hex characters (Loop) or base64/base64url (the reference wallet), 64 bytes either way. */
53
+ function signatureBase64(value) {
54
+ const hex = value.trim().replace(/^0x/i, '');
55
+ if (/^[0-9a-fA-F]{128}$/.test(hex))
56
+ return (0, canton_ed25519_signer_1.normalizeCantonEd25519Signature)({ signatureHex: hex });
57
+ return (0, canton_ed25519_signer_1.normalizeCantonEd25519Signature)({ signatureBase64: value });
58
+ }
59
+ function readProof(proof) {
60
+ if (typeof proof !== 'object' || proof === null)
61
+ return null;
62
+ const record = proof;
63
+ const { partyId, publicKey, namespace, message, signature } = record;
64
+ if (typeof partyId !== 'string' ||
65
+ typeof publicKey !== 'string' ||
66
+ typeof message !== 'string' ||
67
+ typeof signature !== 'string' ||
68
+ (namespace !== undefined && typeof namespace !== 'string') ||
69
+ publicKey.length > 1024 ||
70
+ signature.length > 1024) {
71
+ return null;
72
+ }
73
+ return { partyId, publicKey, message, signature, ...(namespace !== undefined && { namespace }) };
74
+ }
75
+ const fail = (reason, message) => ({
76
+ verified: false,
77
+ reason,
78
+ message,
79
+ });
80
+ /**
81
+ * Verify a wallet ownership proof (see the module comment for what this does and does not establish). Never throws for
82
+ * a bad proof: the result says why it failed. Throws `TypeError` for invalid options.
83
+ */
84
+ function verifyWalletOwnership(proof, options) {
85
+ const maxAgeMs = options.maxAgeMs ?? exports.DEFAULT_WALLET_OWNERSHIP_MAX_AGE_MS;
86
+ const skewMs = options.clockSkewMs ?? exports.DEFAULT_WALLET_OWNERSHIP_CLOCK_SKEW_MS;
87
+ const now = (options.now ?? new Date()).getTime();
88
+ if (typeof options.expectedNonce !== 'string' || options.expectedNonce.length === 0) {
89
+ throw new TypeError('expectedNonce is required');
90
+ }
91
+ if (typeof options.expectedDomain !== 'string' || options.expectedDomain.length === 0) {
92
+ throw new TypeError('expectedDomain is required');
93
+ }
94
+ if (!Number.isFinite(now) || !(maxAgeMs > 0) || !(skewMs >= 0)) {
95
+ throw new TypeError('now, maxAgeMs and clockSkewMs must be valid');
96
+ }
97
+ const read = readProof(proof);
98
+ if (read === null)
99
+ return fail('malformed-proof', 'The proof is not { partyId, publicKey, message, signature }');
100
+ const fields = (0, ownership_1.parseWalletOwnershipMessage)(read.message);
101
+ if (fields === null)
102
+ return fail('malformed-message', 'The signed text is not a canonical wallet ownership message');
103
+ if (fields.partyId !== read.partyId) {
104
+ return fail('party-mismatch', 'The signed message names another party than the proof');
105
+ }
106
+ if (options.expectedPartyId !== undefined && read.partyId !== options.expectedPartyId) {
107
+ return fail('party-mismatch', 'The proof is for another party than expected');
108
+ }
109
+ const partyNamespace = read.partyId.slice(read.partyId.indexOf('::') + 2);
110
+ if (read.namespace !== undefined && read.namespace !== partyNamespace) {
111
+ return fail('party-mismatch', "The wallet's namespace is not the party id's namespace");
112
+ }
113
+ if (fields.domain !== options.expectedDomain) {
114
+ return fail('domain-mismatch', `The message was signed for ${fields.domain}, not ${options.expectedDomain}`);
115
+ }
116
+ if (fields.nonce !== options.expectedNonce) {
117
+ return fail('nonce-mismatch', 'The message was signed for another challenge');
118
+ }
119
+ if (options.expectedNetwork !== undefined && fields.network !== options.expectedNetwork) {
120
+ return fail('network-mismatch', `The message names network ${fields.network ?? '(none)'}, not ${options.expectedNetwork}`);
121
+ }
122
+ if ((options.expectedIssuedAt !== undefined && fields.issuedAt !== options.expectedIssuedAt) ||
123
+ (options.expectedExpiresAt !== undefined && fields.expiresAt !== options.expectedExpiresAt)) {
124
+ return fail('window-mismatch', 'The message names another validity window than the server issued');
125
+ }
126
+ const issuedAt = Date.parse(fields.issuedAt);
127
+ const expiresAt = Date.parse(fields.expiresAt);
128
+ if (issuedAt > now + skewMs)
129
+ return fail('not-yet-valid', 'The message was issued in the future');
130
+ if (expiresAt - issuedAt > maxAgeMs) {
131
+ return fail('lifetime-too-long', `The message is valid for longer than ${maxAgeMs} ms`);
132
+ }
133
+ // With the lifetime bounded above, this also bounds the message's age by maxAgeMs.
134
+ if (now >= expiresAt)
135
+ return fail('expired', 'The message has expired');
136
+ let fingerprint;
137
+ let keyBase64;
138
+ try {
139
+ keyBase64 = publicKeyBase64(read.publicKey);
140
+ fingerprint = (0, canton_protocol_1.deriveCantonEd25519PublicKeyFingerprint)(keyBase64);
141
+ }
142
+ catch {
143
+ return fail('unsupported-public-key', 'The public key is not an Ed25519 key in hex, base64 (raw 32 bytes or DER SubjectPublicKeyInfo) or PEM');
144
+ }
145
+ let partyFingerprint;
146
+ try {
147
+ partyFingerprint = (0, canton_protocol_1.extractPublicKeyFingerprint)(read.partyId);
148
+ }
149
+ catch {
150
+ return fail('party-mismatch', 'The party id is not of the form hint::namespace');
151
+ }
152
+ if (partyFingerprint !== fingerprint) {
153
+ return fail('key-not-bound-to-party', "The public key's Canton fingerprint is not the party's namespace, so the key does not prove control of the party (a participant-hosted party, or a rotated or delegated key, needs a topology lookup)");
154
+ }
155
+ let signature;
156
+ try {
157
+ signature = signatureBase64(read.signature);
158
+ }
159
+ catch {
160
+ return fail('malformed-signature', 'The signature is not 64 bytes of hex or base64');
161
+ }
162
+ try {
163
+ (0, canton_protocol_1.assertEd25519Signature)({ publicKeyBase64: keyBase64, payload: read.message, signatureBase64: signature });
164
+ }
165
+ catch {
166
+ return fail('bad-signature', 'The signature does not verify against the public key');
167
+ }
168
+ return {
169
+ verified: true,
170
+ partyId: read.partyId,
171
+ publicKeyFingerprint: fingerprint,
172
+ domain: fields.domain,
173
+ nonce: fields.nonce,
174
+ ...(fields.network !== undefined && { network: fields.network }),
175
+ issuedAt: fields.issuedAt,
176
+ expiresAt: fields.expiresAt,
177
+ };
178
+ }
179
+ //# sourceMappingURL=wallet-ownership.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-ownership.js","sourceRoot":"","sources":["../../../../src/utils/external-signing/wallet-ownership.ts"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,uDAI2B;AAC3B,mEAA0E;AAC1E,uDAAsE;AAwEzD,QAAA,mCAAmC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACrD,QAAA,sCAAsC,GAAG,EAAE,GAAG,IAAI,CAAC;AAEhE,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AAE1D;;;;GAIG;AACH,SAAS,eAAe,CAAC,KAAa;IACpC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACxC,IACE,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;QAC1B,CAAC,GAAG,CAAC,MAAM,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,EAAE,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAC,CAAC,EAClG,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IACD,MAAM,GAAG,GAAG,gFAAgF,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3G,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;AACvE,CAAC;AAED,0HAA0H;AAC1H,SAAS,eAAe,CAAC,KAAa;IACpC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,C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9C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC,qBAAqB,EAAE,gDAAgD,CAAC,CAAC;IACvF,CAAC;IACD,IAAI,CAAC;QACH,IAAA,wCAAsB,EAAC,EAAE,eAAe,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5G,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC,eAAe,EAAE,sDAAsD,CAAC,CAAC;IACvF,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,oBAAoB,EAAE,WAAW;QACjC,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,GAAG,CAAC,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC;QAChE,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,SAAS,EAAE,MAAM,CAAC,SAAS;KAC5B,CAAC;AACJ,CAAC"}
@@ -13,6 +13,10 @@ export type Cip103LedgerApiParams = {
13
13
  resource: string;
14
14
  body?: Record<string, unknown>;
15
15
  };
16
+ /** `signMessage` params (the OpenRPC's `SignMessageRequest`). */
17
+ export type Cip103SignMessageParams = {
18
+ message: string;
19
+ };
16
20
  /** The dApp API requests this SDK makes, each in CIP-0103's `{ method, params }` form. */
17
21
  export type Cip103Request = {
18
22
  method: 'getPrimaryAccount';
@@ -22,6 +26,9 @@ export type Cip103Request = {
22
26
  } | {
23
27
  method: 'prepareExecuteAndWait';
24
28
  params: Cip103PrepareExecuteParams;
29
+ } | {
30
+ method: 'signMessage';
31
+ params: Cip103SignMessageParams;
25
32
  };
26
33
  /** An event listener of CIP-0103's Provider API. */
27
34
  export type Cip103EventListener = (...args: unknown[]) => void;
@@ -57,6 +64,8 @@ export declare const CIP103_ERROR_CODES: {
57
64
  };
58
65
  /** The numeric `code` of a provider error (`ProviderRpcError`, `JsonRpcError` or a plain object). */
59
66
  export declare function cip103ErrorCode(error: unknown): number | undefined;
67
+ /** `message (CIP-0103 4200 Unsupported Method)`, so a UI can show what the wallet answered. */
68
+ export declare function describeError(error: unknown): string;
60
69
  /**
61
70
  * A provider error from `prepareExecuteAndWait` as a `WalletSubmitError`:
62
71
  *
@@ -69,9 +78,22 @@ export declare function cip103ErrorCode(error: unknown): number | undefined;
69
78
  */
70
79
  export declare function cip103SubmitError(error: unknown, commandId?: string): WalletSubmitError;
71
80
  /**
72
- * The wallet party: the primary account's `partyId` (`getPrimaryAccount`). The dApp runs its wallet's own connect flow
73
- * first; a provider that is not connected answers 4100.
81
+ * The fields of the CIP-0103 `Wallet` (account) this SDK reads. `publicKey` and `namespace` are as the wallet reports
82
+ * them (their encoding is not specified by CIP-0103); absent when the wallet did not send them.
74
83
  */
84
+ export interface Cip103Account {
85
+ readonly partyId: string;
86
+ readonly publicKey?: string;
87
+ readonly namespace?: string;
88
+ readonly networkId?: string;
89
+ readonly signingProviderId?: string;
90
+ }
91
+ /**
92
+ * The wallet's primary account (`getPrimaryAccount`). The dApp runs its wallet's own connect flow first; a provider
93
+ * that is not connected answers 4100.
94
+ */
95
+ export declare function primaryAccount(provider: Cip103Provider): Promise<Cip103Account>;
96
+ /** The wallet party: the primary account's `partyId` (`getPrimaryAccount`). */
75
97
  export declare function primaryPartyId(provider: Cip103Provider): Promise<string>;
76
98
  /**
77
99
  * Entries asked for per `/v2/state/active-contracts-page` request (`maxPageSize`). Well under the participant's default
@@ -83,14 +105,70 @@ export declare const ACS_PAGE_SIZE = 200;
83
105
  * of one filter). A read that needs more fails with `WalletReadError` instead of paging on.
84
106
  */
85
107
  export declare const ACS_MAX_PAGES = 500;
108
+ /**
109
+ * Codes of a first paged ACS call after which the snapshot is read once through the unpaged
110
+ * `/v2/state/active-contracts` instead: the wallet does not offer the paged endpoint (4200, -32601, -32004), or refused
111
+ * the paged request's shape (-32602). Loop reports those cases differently (see {@link LOOP_HTTP_REFUSED_MESSAGE}). Any
112
+ * other failure (a rate limit, a timeout, a refused or disconnected session) is reported as it is: a second read would
113
+ * fail the same way and only add load.
114
+ */
115
+ export declare const ACS_FALLBACK_CODES: ReadonlySet<number>;
116
+ /**
117
+ * Loop's message, with code -32002, for any non-OK HTTP reply of its ledger proxy other than 429 that carries no
118
+ * structured error (`@fivenorth/loop-sdk` `Connection.ledgerApi`): an endpoint its allowlist does not offer, a
119
+ * participant that serves the paged endpoint only as GET (Canton 3.5) or refuses its `maxPageSize`, or a server error.
120
+ * Loop's timeout or network failure is another message ("Ledger request failed or timed out") and does not fall back.
121
+ */
122
+ export declare const LOOP_HTTP_REFUSED_MESSAGE = "Ledger access unavailable";
123
+ /**
124
+ * Whether a provider error is a rate limit: -32005, or -32603 whose message says HTTP status 429 (Loop's session check,
125
+ * which runs before every `ledgerApi` call, throws a plain `Error` that Loop's CIP-0103 adapter maps to -32603).
126
+ */
127
+ export declare function isRateLimited(error: unknown): boolean;
128
+ /**
129
+ * Waits before each retry of a read the wallet rate-limited (-32005): 0.5 s, 1 s, 2 s, 4 s, so at most five attempts
130
+ * over about 7.5 s. Each wait is jittered to between half and all of its value, so parallel readers spread out.
131
+ */
132
+ export declare const DEFAULT_READ_RETRY_DELAYS_MS: readonly number[];
133
+ /** How reads back off when the wallet rate-limits them (-32005). Never applied to submissions. */
134
+ export interface WalletReadRetryOptions {
135
+ /** The wait before each retry, in milliseconds; defaults to {@link DEFAULT_READ_RETRY_DELAYS_MS}. Empty: no retry. */
136
+ readonly delaysMs?: readonly number[];
137
+ /** Defaults to `setTimeout`. */
138
+ readonly sleep?: (ms: number) => Promise<void>;
139
+ /** A number in [0, 1) for the jitter; defaults to `Math.random`. */
140
+ readonly random?: () => number;
141
+ }
142
+ /** Contracts of one ACS snapshot, read at one ledger offset. */
143
+ export interface AcsSnapshot {
144
+ readonly offset: number;
145
+ readonly contracts: ActiveContract[];
146
+ }
147
+ export interface AcsReadOptions {
148
+ /**
149
+ * Read at this ledger offset (one an earlier read reported) instead of the current ledger end, so a later read joins
150
+ * an earlier snapshot.
151
+ */
152
+ readonly activeAtOffset?: number;
153
+ }
86
154
  /** What the token actions need from a wallet: one party, its contracts, and a submit that waits for the commit. */
87
155
  export interface Cip103Wallet {
88
156
  readonly partyId: string;
89
- /** Active contracts of `partyId` matching the filter, as one snapshot at the ledger end. */
90
- listActiveContracts(filter: ContractFilter): Promise<ActiveContract[]>;
157
+ /** Active contracts of `partyId` matching the filter, as one snapshot at the ledger end (or at `activeAtOffset`). */
158
+ listActiveContracts(filter: ContractFilter, options?: AcsReadOptions): Promise<ActiveContract[]>;
159
+ /**
160
+ * Active contracts of `partyId` matching any of the filters, as one snapshot at the ledger end: one ledger-end call
161
+ * and one paged ACS stream whose cumulative filter carries every filter. A contract matching several is returned
162
+ * once. With `activeAtOffset`, the snapshot is read at that offset and the ledger end is not read.
163
+ */
164
+ readActiveContracts(filters: readonly ContractFilter[], options?: AcsReadOptions): Promise<AcsSnapshot>;
91
165
  /** Sign and submit as `partyId`; resolves once committed, else throws `WalletSubmitError`. Never retries. */
92
166
  submit(submission: WalletSubmission): Promise<SubmitResult>;
93
167
  }
168
+ export interface Cip103WalletOptions {
169
+ /** How reads back off when the wallet rate-limits them. */
170
+ readonly readRetry?: WalletReadRetryOptions;
171
+ }
94
172
  /** The SDK's CIP-0103 client acting as `partyId` through `provider`. */
95
- export declare function cip103Wallet(provider: Cip103Provider, partyId: string): Cip103Wallet;
173
+ export declare function cip103Wallet(provider: Cip103Provider, partyId: string, options?: Cip103WalletOptions): Cip103Wallet;
96
174
  //# sourceMappingURL=cip103.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cip103.d.ts","sourceRoot":"","sources":["../../../src/wallets/cip103.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAmB,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE9D,OAAO,KAAK,EACV,cAAc,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