@partylayer/sdk 0.18.1 → 0.19.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 +2 -0
- package/dist/index.d.mts +51 -8
- package/dist/index.d.ts +51 -8
- package/dist/index.js +164 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +158 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { toWalletId, toPartyId, isRecognizedNetwork, toSignature, ledgerApiBodyToObject, normalizeLedgerMethodLower, hashForPrivacy, createMetricsPayload, isOfficialAdapterFactory, isOfficialProviderAdapter, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, detectNetworkMismatch, NetworkMismatchError, METRICS, AdapterNotRegisteredError, WalletNotFoundError, capabilityGuard, installGuard, toSessionId, CapabilityNotSupportedError, errorMetricName } from '@partylayer/core';
|
|
2
|
-
export { AdapterNotRegisteredError, PartyLayerError as CantonConnectError, CapabilityNotSupportedError, ENABLEMENT_METRICS, ERROR_METRICS, InternalError, METRICS, OriginNotAllowedError, PartyLayerError, REGISTRY_METRICS, RegistryFetchFailedError, RegistrySchemaInvalidError, RegistryVerificationFailedError, SessionExpiredError, TimeoutError, TransportError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError, errorMetricName, isOfficialAdapterFactory, isOfficialProviderAdapter } from '@partylayer/core';
|
|
1
|
+
import { toWalletId, toPartyId, isRecognizedNetwork, toSignature, ledgerApiBodyToObject, normalizeLedgerMethodLower, hashForPrivacy, createMetricsPayload, isOfficialAdapterFactory, isOfficialProviderAdapter, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, detectNetworkMismatch, NetworkMismatchError, METRICS, AdapterNotRegisteredError, WalletNotFoundError, capabilityGuard, installGuard, toSessionId, CapabilityNotSupportedError, toTransferIntent, errorMetricName, toTransactionHash } from '@partylayer/core';
|
|
2
|
+
export { AdapterNotRegisteredError, PartyLayerError as CantonConnectError, CapabilityNotSupportedError, ENABLEMENT_METRICS, ERROR_METRICS, InternalError, METRICS, OriginNotAllowedError, PartyLayerError, REGISTRY_METRICS, RegistryFetchFailedError, RegistrySchemaInvalidError, RegistryVerificationFailedError, SessionExpiredError, TRANSFER_INTENT_FIELDS, TimeoutError, TransportError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError, errorMetricName, isOfficialAdapterFactory, isOfficialProviderAdapter, toTransferIntent } from '@partylayer/core';
|
|
3
3
|
import { RegistryClient } from '@partylayer/registry-client';
|
|
4
4
|
export { deriveGenericWalletName, findMatchingWallet, findMatchingWalletInfo, isCip0103Native, matchesProviderDetection } from '@partylayer/registry-client';
|
|
5
5
|
import { subscribeAnnouncedProviders, createExtensionChannelProvider, discoverProviders, createProviderBridge } from '@partylayer/provider';
|
|
@@ -15,6 +15,64 @@ export { SendAdapter } from '@partylayer/adapter-send';
|
|
|
15
15
|
export { BronAdapter } from '@partylayer/adapter-bron';
|
|
16
16
|
|
|
17
17
|
// src/client.ts
|
|
18
|
+
var UNSUPPORTED_METHOD = 4200;
|
|
19
|
+
var METHOD_NOT_FOUND = -32601;
|
|
20
|
+
function isUnsupportedMethod(err) {
|
|
21
|
+
const code = err?.code;
|
|
22
|
+
return code === UNSUPPORTED_METHOD || code === METHOD_NOT_FOUND;
|
|
23
|
+
}
|
|
24
|
+
function receiptFromExecutedTx(result) {
|
|
25
|
+
const tx = result?.tx;
|
|
26
|
+
const updateId = tx?.payload?.updateId;
|
|
27
|
+
if (typeof updateId !== "string" || updateId.length === 0) return null;
|
|
28
|
+
return {
|
|
29
|
+
transactionHash: toTransactionHash(updateId),
|
|
30
|
+
submittedAt: Date.now(),
|
|
31
|
+
commandId: typeof tx?.commandId === "string" ? tx.commandId : void 0,
|
|
32
|
+
updateId
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function reportOnce(state, walletId, detail, logger) {
|
|
36
|
+
if (state.logged) return;
|
|
37
|
+
state.logged = true;
|
|
38
|
+
logger?.warn(
|
|
39
|
+
`[PartyLayer] ${walletId}: ${detail} Receipts from it carry no real updateId.`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
async function submitViaPrepareExecute(provider, params, opts) {
|
|
43
|
+
const rpcParams = params;
|
|
44
|
+
const { walletId, state, logger } = opts;
|
|
45
|
+
if (state.verb !== "plain") {
|
|
46
|
+
try {
|
|
47
|
+
const result = await provider.request({
|
|
48
|
+
method: "prepareExecuteAndWait",
|
|
49
|
+
params: rpcParams
|
|
50
|
+
});
|
|
51
|
+
state.verb = "awaited";
|
|
52
|
+
const receipt = receiptFromExecutedTx(result);
|
|
53
|
+
if (receipt) return receipt;
|
|
54
|
+
reportOnce(
|
|
55
|
+
state,
|
|
56
|
+
walletId,
|
|
57
|
+
"prepareExecuteAndWait returned no updateId.",
|
|
58
|
+
logger
|
|
59
|
+
);
|
|
60
|
+
return result;
|
|
61
|
+
} catch (err) {
|
|
62
|
+
if (!isUnsupportedMethod(err)) throw err;
|
|
63
|
+
state.verb = "plain";
|
|
64
|
+
reportOnce(
|
|
65
|
+
state,
|
|
66
|
+
walletId,
|
|
67
|
+
"has no prepareExecuteAndWait; using prepareExecute, which the CIP-0103 types return as null.",
|
|
68
|
+
logger
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return provider.request({ method: "prepareExecute", params: rpcParams });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/announce-adapter.ts
|
|
18
76
|
var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
|
|
19
77
|
function announcedWalletId(announceId) {
|
|
20
78
|
return toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
|
|
@@ -48,11 +106,19 @@ var GenericAnnounceAdapter = class {
|
|
|
48
106
|
staticMetadata;
|
|
49
107
|
mapError;
|
|
50
108
|
signMessageBase64;
|
|
109
|
+
/**
|
|
110
|
+
* Which execute verb this wallet turned out to support. Resolved on the first
|
|
111
|
+
* submit and reused, so the wallet is asked at most once.
|
|
112
|
+
*/
|
|
113
|
+
executeVerb = {};
|
|
51
114
|
// Optional WalletAdapter surface — assigned in the ctor ONLY when configured,
|
|
52
115
|
// so `getCapabilities()` and `'x' in adapter` feature-detection stay honest.
|
|
53
116
|
restore;
|
|
54
117
|
on;
|
|
55
118
|
ledgerApi;
|
|
119
|
+
// signMessage is optional too: assigned in the ctor only when the wallet
|
|
120
|
+
// declares support, so a wallet that cannot sign is not advertised as able to.
|
|
121
|
+
signMessage;
|
|
56
122
|
constructor(args) {
|
|
57
123
|
this.walletId = args.walletId ?? announcedWalletId(args.announceId);
|
|
58
124
|
this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
|
|
@@ -66,13 +132,18 @@ var GenericAnnounceAdapter = class {
|
|
|
66
132
|
if (config?.events) this.on = this.makeOn();
|
|
67
133
|
if (config?.restore) this.restore = this.makeRestore();
|
|
68
134
|
if (config?.ledgerApi) this.ledgerApi = this.makeLedgerApi();
|
|
135
|
+
if (config?.signMessage !== false) this.signMessage = this.signMessageImpl.bind(this);
|
|
69
136
|
}
|
|
70
137
|
/**
|
|
71
|
-
* Capabilities
|
|
72
|
-
*
|
|
138
|
+
* Capabilities derived from actual presence: connect and submitTransaction are
|
|
139
|
+
* always present; signMessage, restore, ledgerApi, and events are each included
|
|
140
|
+
* only when the matching method was assigned, so the set is never advertised
|
|
141
|
+
* beyond what the adapter can do.
|
|
73
142
|
*/
|
|
74
143
|
getCapabilities() {
|
|
75
|
-
const caps = ["connect"
|
|
144
|
+
const caps = ["connect"];
|
|
145
|
+
if (this.signMessage) caps.push("signMessage");
|
|
146
|
+
caps.push("submitTransaction");
|
|
76
147
|
if (this.restore) caps.push("restore");
|
|
77
148
|
if (this.ledgerApi) caps.push("ledgerApi");
|
|
78
149
|
if (this.on) caps.push("events");
|
|
@@ -118,7 +189,10 @@ var GenericAnnounceAdapter = class {
|
|
|
118
189
|
} catch {
|
|
119
190
|
}
|
|
120
191
|
}
|
|
121
|
-
|
|
192
|
+
// Real signMessage implementation. Exposed as `this.signMessage` from the ctor
|
|
193
|
+
// only when the wallet declares support, so getCapabilities and feature
|
|
194
|
+
// detection stay honest for a wallet that cannot sign.
|
|
195
|
+
async signMessageImpl(_ctx, session, params) {
|
|
122
196
|
return this.guarded(async () => {
|
|
123
197
|
const rpcParams = this.signMessageBase64 ? { message: toBase64Message(params.message) } : { message: params.message };
|
|
124
198
|
const res = await this.provider.request({ method: "signMessage", params: rpcParams });
|
|
@@ -132,11 +206,12 @@ var GenericAnnounceAdapter = class {
|
|
|
132
206
|
};
|
|
133
207
|
});
|
|
134
208
|
}
|
|
135
|
-
async submitTransaction(
|
|
209
|
+
async submitTransaction(ctx, _session, params) {
|
|
136
210
|
return this.guarded(
|
|
137
|
-
() => this.provider
|
|
138
|
-
|
|
139
|
-
|
|
211
|
+
() => submitViaPrepareExecute(this.provider, params, {
|
|
212
|
+
walletId: String(this.walletId),
|
|
213
|
+
state: this.executeVerb,
|
|
214
|
+
logger: ctx.logger
|
|
140
215
|
})
|
|
141
216
|
);
|
|
142
217
|
}
|
|
@@ -249,6 +324,11 @@ var GenericDiscoveryAdapter = class {
|
|
|
249
324
|
networkHosts;
|
|
250
325
|
/** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
|
|
251
326
|
providerInstance = null;
|
|
327
|
+
/**
|
|
328
|
+
* Which execute verb this wallet turned out to support. Resolved on the first
|
|
329
|
+
* submit and reused, so the wallet is asked at most once.
|
|
330
|
+
*/
|
|
331
|
+
executeVerb = {};
|
|
252
332
|
constructor(args) {
|
|
253
333
|
if (args.official) {
|
|
254
334
|
this.official = args.official;
|
|
@@ -416,10 +496,11 @@ var GenericDiscoveryAdapter = class {
|
|
|
416
496
|
params: { message: params.message }
|
|
417
497
|
});
|
|
418
498
|
}
|
|
419
|
-
async submitTransaction(
|
|
420
|
-
return this.provider
|
|
421
|
-
|
|
422
|
-
|
|
499
|
+
async submitTransaction(ctx, _session, params) {
|
|
500
|
+
return submitViaPrepareExecute(this.provider, params, {
|
|
501
|
+
walletId: String(this.walletId),
|
|
502
|
+
state: this.executeVerb,
|
|
503
|
+
logger: ctx.logger
|
|
423
504
|
});
|
|
424
505
|
}
|
|
425
506
|
};
|
|
@@ -848,6 +929,10 @@ function deriveAnnounceConfig(entry) {
|
|
|
848
929
|
const cfg = entry.adapter?.config ?? {};
|
|
849
930
|
const config = {
|
|
850
931
|
events: entry.capabilities?.events === true,
|
|
932
|
+
// signMessage is ON unless the entry declares capabilities.signMessage:false,
|
|
933
|
+
// so a wallet that cannot sign is reported honestly. The default preserves the
|
|
934
|
+
// baseline for every entry that omits it or sets it true.
|
|
935
|
+
signMessage: entry.capabilities?.signMessage !== false,
|
|
851
936
|
restore: cfg.restore === true,
|
|
852
937
|
ledgerApi: cfg.ledgerApi === true,
|
|
853
938
|
metadata: cfg.metadata === true,
|
|
@@ -1026,7 +1111,8 @@ var PartyLayerClient = class {
|
|
|
1026
1111
|
);
|
|
1027
1112
|
}
|
|
1028
1113
|
if (!filter?.includeExperimental) {
|
|
1029
|
-
|
|
1114
|
+
const channel = this.config.channel || "stable";
|
|
1115
|
+
return registryWallets.filter((walletInfo) => walletInfo.channel === channel);
|
|
1030
1116
|
}
|
|
1031
1117
|
return registryWallets;
|
|
1032
1118
|
}
|
|
@@ -1144,8 +1230,16 @@ var PartyLayerClient = class {
|
|
|
1144
1230
|
if (!this.announceEnabled) return base;
|
|
1145
1231
|
if (this.announceEntriesCache === null) {
|
|
1146
1232
|
try {
|
|
1233
|
+
let injectionPaths;
|
|
1234
|
+
try {
|
|
1235
|
+
const registry = await this.registryClient.getRegistry();
|
|
1236
|
+
const props = registry.wallets.filter((w) => w.adapter?.transport === "announce").map((w) => w.installation?.windowProperty).filter((p) => typeof p === "string" && p.length > 0);
|
|
1237
|
+
if (props.length > 0) injectionPaths = [...new Set(props)];
|
|
1238
|
+
} catch {
|
|
1239
|
+
}
|
|
1147
1240
|
const snapshot = await discoverProviders({
|
|
1148
1241
|
timeoutMs: this.config.discovery?.announceTimeoutMs,
|
|
1242
|
+
injectionPaths,
|
|
1149
1243
|
// Working provider over the announce target channel; G4 (provider.md):
|
|
1150
1244
|
// target defaults to id when omitted — never a shared slot.
|
|
1151
1245
|
createProvider: (a) => createExtensionChannelProvider({ target: a.target ?? a.id })
|
|
@@ -1667,6 +1761,55 @@ var PartyLayerClient = class {
|
|
|
1667
1761
|
throw error;
|
|
1668
1762
|
}
|
|
1669
1763
|
}
|
|
1764
|
+
/**
|
|
1765
|
+
* Request a typed transfer. The application says what it wants; the wallet
|
|
1766
|
+
* builds the command, shows the user what they are authorising, obtains their
|
|
1767
|
+
* approval, signs, executes, and returns the real update id.
|
|
1768
|
+
*
|
|
1769
|
+
* The acting party comes from the active session, never from the caller, and
|
|
1770
|
+
* the intent is narrowed to the allowlisted fields before any adapter sees it,
|
|
1771
|
+
* so no caller-supplied option can reach the wallet.
|
|
1772
|
+
*
|
|
1773
|
+
* Throws `CapabilityNotSupportedError` when the active wallet does not
|
|
1774
|
+
* implement it. Ask first with `session.capabilitiesSnapshot.includes('transfer')`,
|
|
1775
|
+
* or require it at connect with `connect({ requiredCapabilities: ['transfer'] })`.
|
|
1776
|
+
*/
|
|
1777
|
+
async requestTransfer(intent) {
|
|
1778
|
+
const session = await this.getActiveSession();
|
|
1779
|
+
if (!session) {
|
|
1780
|
+
throw new Error("No active session");
|
|
1781
|
+
}
|
|
1782
|
+
const adapter = this.adapters.get(session.walletId);
|
|
1783
|
+
if (!adapter || !adapter.requestTransfer) {
|
|
1784
|
+
throw new CapabilityNotSupportedError(
|
|
1785
|
+
session.walletId,
|
|
1786
|
+
"transfer"
|
|
1787
|
+
);
|
|
1788
|
+
}
|
|
1789
|
+
const safeIntent = toTransferIntent(intent);
|
|
1790
|
+
const correlationId = newCorrelationId();
|
|
1791
|
+
this.log("debug", "requestTransfer:start", "requestTransfer", {}, correlationId);
|
|
1792
|
+
try {
|
|
1793
|
+
this.assertNetworkOk(session);
|
|
1794
|
+
const ctx = this.createAdapterContext();
|
|
1795
|
+
const result = await adapter.requestTransfer(ctx, session, safeIntent);
|
|
1796
|
+
this.emit("tx:status", {
|
|
1797
|
+
type: "tx:status",
|
|
1798
|
+
sessionId: session.sessionId,
|
|
1799
|
+
txId: result.updateId,
|
|
1800
|
+
status: "committed",
|
|
1801
|
+
raw: result
|
|
1802
|
+
}, correlationId);
|
|
1803
|
+
return result;
|
|
1804
|
+
} catch (err) {
|
|
1805
|
+
const error = mapUnknownErrorToPartyLayerError(err, {
|
|
1806
|
+
phase: "requestTransfer",
|
|
1807
|
+
walletId: String(session.walletId)
|
|
1808
|
+
});
|
|
1809
|
+
this.emit("error", { type: "error", error }, correlationId);
|
|
1810
|
+
throw error;
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1670
1813
|
/**
|
|
1671
1814
|
* Subscribe to events
|
|
1672
1815
|
*/
|