@amadeus-protocol/wallet-sdk 0.2.0 → 0.2.1

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 CHANGED
@@ -79,12 +79,12 @@ if (wallet.isAvailable()) {
79
79
  ```
80
80
 
81
81
  The client also has `getAccount`, `getNetwork`, `signTransaction`, `on` / `off`
82
- for provider events, `ensureApiKey` (returns a cached API key or asks the wallet
83
- for a new one) and `switchAccount(target?)`. `switchAccount` asks the wallet to
84
- change the active account; the extension asks the user to approve (it resolves
85
- at once only when `target` is already active), and the call throws
86
- `WalletSdkError` code `UNSUPPORTED` on extension builds without
87
- `requestSwitchAccount`.
82
+ for provider events, `ensureApiKey` (returns a cached API key signed by the
83
+ active account or asks the wallet for a new one) and `switchAccount(target?)`.
84
+ `switchAccount` asks the wallet to change the active account; the extension asks
85
+ the user to approve (it resolves at once only when `target` is already active),
86
+ and the call throws `WalletSdkError` code `UNSUPPORTED` on extension builds
87
+ without `requestSwitchAccount`.
88
88
 
89
89
  The extension can inject `window.amadeus` after your script runs. Listen for
90
90
  `WALLET_INITIALIZED_EVENT` (`amadeus-wallet#initialized`, not
@@ -23,7 +23,7 @@ interface WalletClient {
23
23
  txHash: string;
24
24
  submitted: unknown;
25
25
  }>;
26
- /** Return a valid cached api-key for `aud`, minting (and caching) a fresh one if needed. Coalesces concurrent calls per aud. */
26
+ /** Return a valid cached api-key for `aud` signed by the active account, minting (and caching) a fresh one if needed. Coalesces concurrent calls per account + aud. */
27
27
  ensureApiKey(options: {
28
28
  aud: string;
29
29
  expIn: number;
@@ -1 +1 @@
1
- {"version":3,"file":"injected.d.ts","names":[],"sources":["../../src/client/injected.ts"],"mappings":";;UAyBiB;;EAEhB,IAAI;EACJ,IAAI,gBAAgB;;UAsBJ;EAChB;EACA,eAAe;;EAEf,WAAW;EACX,cAAc;EACd,cAAc;;EAEd,cAAc,kBAAkB;EAChC,gBAAgB,SAAS,yBAAyB,QAAQ;;EAE1D,cACC,SAAS,wBACT;IAAW;IAAiB;MAC1B;IAAU;IAAgB;;;EAE7B,aAAa;IACZ;IACA;IACA,QAAQ;IACR;MACG;EACJ,GAAG,UAAU,sBACZ,OAAO,GACP,KAAK,MAAM,uBAAuB;EAEnC,IAAI,UAAU,sBACb,OAAO,GACP,KAAK,MAAM,uBAAuB;;;;;;iBAQpB,2BAA2B,mBAAmB,kBAAkB"}
1
+ {"version":3,"file":"injected.d.ts","names":[],"sources":["../../src/client/injected.ts"],"mappings":";;UAyBiB;;EAEhB,IAAI;EACJ,IAAI,gBAAgB;;UA+BJ;EAChB;EACA,eAAe;;EAEf,WAAW;EACX,cAAc;EACd,cAAc;;EAEd,cAAc,kBAAkB;EAChC,gBAAgB,SAAS,yBAAyB,QAAQ;;EAE1D,cACC,SAAS,wBACT;IAAW;IAAiB;MAC1B;IAAU;IAAgB;;;EAE7B,aAAa;IACZ;IACA;IACA,QAAQ;IACR;MACG;EACJ,GAAG,UAAU,sBACZ,OAAO,GACP,KAAK,MAAM,uBAAuB;EAEnC,IAAI,UAAU,sBACb,OAAO,GACP,KAAK,MAAM,uBAAuB;;;;;;iBAQpB,2BAA2B,mBAAmB,kBAAkB"}
@@ -23,10 +23,10 @@ function memoryApiKeyStore() {
23
23
  }
24
24
  };
25
25
  }
26
- function isApiKeyValid(apiKey, aud, leewaySeconds) {
26
+ function isApiKeyValid(apiKey, aud, account, leewaySeconds) {
27
27
  try {
28
- const { payload } = decodeApiKey(apiKey);
29
- return payload.aud === aud && payload.exp > Math.floor(Date.now() / 1e3) + leewaySeconds;
28
+ const { payload, address } = decodeApiKey(apiKey);
29
+ return address === account && payload.aud === aud && payload.exp > Math.floor(Date.now() / 1e3) + leewaySeconds;
30
30
  } catch {
31
31
  return false;
32
32
  }
@@ -70,21 +70,24 @@ function createInjectedWalletClient(providerOverride) {
70
70
  })
71
71
  };
72
72
  },
73
- ensureApiKey: ({ aud, expIn, store = defaultStore, leewaySeconds = 60 }) => {
73
+ ensureApiKey: async ({ aud, expIn, store = defaultStore, leewaySeconds = 60 }) => {
74
+ const provider = requireProvider();
75
+ const account = await provider.getAccount();
74
76
  const cached = store.get(aud);
75
- if (cached && isApiKeyValid(cached, aud, leewaySeconds)) return Promise.resolve(cached);
76
- const inFlight = pendingApiKeys.get(aud);
77
+ if (cached && isApiKeyValid(cached, aud, account, leewaySeconds)) return cached;
78
+ const pendingKey = JSON.stringify([account, aud]);
79
+ const inFlight = pendingApiKeys.get(pendingKey);
77
80
  if (inFlight) return inFlight;
78
- const request = requireProvider().generateApiKey({
81
+ const request = provider.generateApiKey({
79
82
  aud,
80
83
  exp_in: expIn
81
84
  }).then(({ apiKey }) => {
82
85
  store.set(apiKey, aud);
83
86
  return apiKey;
84
87
  }).finally(() => {
85
- pendingApiKeys.delete(aud);
88
+ pendingApiKeys.delete(pendingKey);
86
89
  });
87
- pendingApiKeys.set(aud, request);
90
+ pendingApiKeys.set(pendingKey, request);
88
91
  return request;
89
92
  },
90
93
  on: (event, cb) => requireProvider().on(event, cb),
@@ -1 +1 @@
1
- {"version":3,"file":"injected.js","names":[],"sources":["../../src/client/injected.ts"],"sourcesContent":["/**\n * A framework-agnostic dApp client over the injected `window.amadeus` provider\n * (the browser extension). It wraps the raw provider with the ergonomics every\n * dApp currently hand-rolls: a single-account `connect`, sign-and-submit, and a\n * cached `ensureApiKey` that decodes expiry via the audited api-key module\n * (replacing the copies in hub / prime-hub).\n *\n * The mobile deep-link/QR path uses the transport module (connect handshake +\n * sealed envelopes) instead of an injected provider; this client is the\n * same-device, extension-present case.\n */\n\nimport { decodeApiKey } from '../crypto/apikey'\nimport { WalletSdkError } from '../errors'\nimport { submitTransaction } from '../tx'\nimport type {\n\tAmadeusEventPayloadMap,\n\tAmadeusProvider,\n\tAmadeusProviderEvent,\n\tSignTransactionRequest,\n\tSignTransactionResponse\n} from '../types'\nimport { getAmadeusProvider } from '../types'\n\n/** Where a cached api-key lives. Default is in-memory; dApps can pass localStorage-backed. */\nexport interface ApiKeyStore {\n\t/** `aud` is passed so a store can keep one key per audience. */\n\tget(aud?: string): string | null\n\tset(apiKey: string, aud?: string): void\n}\n\nfunction memoryApiKeyStore(): ApiKeyStore {\n\tconst keysByAud = new Map<string, string>()\n\treturn {\n\t\tget: (aud = '') => keysByAud.get(aud) ?? null,\n\t\tset: (apiKey, aud = '') => {\n\t\t\tkeysByAud.set(aud, apiKey)\n\t\t}\n\t}\n}\n\nfunction isApiKeyValid(apiKey: string, aud: string, leewaySeconds: number): boolean {\n\ttry {\n\t\tconst { payload } = decodeApiKey(apiKey)\n\t\treturn payload.aud === aud && payload.exp > Math.floor(Date.now() / 1000) + leewaySeconds\n\t} catch {\n\t\treturn false\n\t}\n}\n\nexport interface WalletClient {\n\tisAvailable(): boolean\n\tgetProvider(): AmadeusProvider | null\n\t/** Prompt connection; resolves to the connected account (Base58 address). */\n\tconnect(): Promise<string>\n\tgetAccount(): Promise<string | null>\n\tgetNetwork(): Promise<string>\n\t/** Ask the wallet to switch the active account (user-confirmed); resolves to the new address. */\n\tswitchAccount(target?: string): Promise<string>\n\tsignTransaction(request: SignTransactionRequest): Promise<SignTransactionResponse>\n\t/** Sign then submit the packed bytes to a node; resolves to the tx hash + node reply. */\n\tsignAndSubmit(\n\t\trequest: SignTransactionRequest,\n\t\toptions: { nodeUrl: string; wait?: boolean }\n\t): Promise<{ txHash: string; submitted: unknown }>\n\t/** Return a valid cached api-key for `aud`, minting (and caching) a fresh one if needed. Coalesces concurrent calls per aud. */\n\tensureApiKey(options: {\n\t\taud: string\n\t\texpIn: number\n\t\tstore?: ApiKeyStore\n\t\tleewaySeconds?: number\n\t}): Promise<string>\n\ton<E extends AmadeusProviderEvent>(\n\t\tevent: E,\n\t\tcb: (data: AmadeusEventPayloadMap[E]) => void\n\t): void\n\toff<E extends AmadeusProviderEvent>(\n\t\tevent: E,\n\t\tcb: (data: AmadeusEventPayloadMap[E]) => void\n\t): void\n}\n\n/**\n * Create a client bound to the injected provider. Pass `providerOverride` to\n * inject a provider (e.g. in tests); otherwise it reads `window.amadeus`.\n */\nexport function createInjectedWalletClient(providerOverride?: AmadeusProvider): WalletClient {\n\tconst getProvider = () => providerOverride ?? getAmadeusProvider()\n\tconst requireProvider = (): AmadeusProvider => {\n\t\tconst provider = getProvider()\n\t\tif (!provider)\n\t\t\tthrow new WalletSdkError('INVALID_ARGUMENT', 'Amadeus wallet is not available')\n\t\treturn provider\n\t}\n\n\tconst defaultStore = memoryApiKeyStore()\n\tconst pendingApiKeys = new Map<string, Promise<string>>()\n\n\treturn {\n\t\tisAvailable: () => getProvider() !== null,\n\t\tgetProvider,\n\t\tconnect: async () => {\n\t\t\tconst accounts = await requireProvider().requestAccounts()\n\t\t\tif (!accounts.length)\n\t\t\t\tthrow new WalletSdkError('INVALID_ARGUMENT', 'no account returned')\n\t\t\treturn accounts[0]\n\t\t},\n\t\tgetAccount: () => requireProvider().getAccount(),\n\t\tgetNetwork: () => requireProvider().getNetwork(),\n\t\tswitchAccount: async (target) => {\n\t\t\tconst provider = requireProvider()\n\t\t\tif (typeof provider.requestSwitchAccount !== 'function')\n\t\t\t\tthrow new WalletSdkError('UNSUPPORTED', 'wallet does not support switchAccount')\n\t\t\treturn provider.requestSwitchAccount(target)\n\t\t},\n\t\tsignTransaction: (request) => requireProvider().signTransaction(request),\n\t\tsignAndSubmit: async (request, options) => {\n\t\t\tconst { txHash, txPacked } = await requireProvider().signTransaction(request)\n\t\t\tconst submitted = await submitTransaction(txPacked, {\n\t\t\t\tnodeUrl: options.nodeUrl,\n\t\t\t\twait: options.wait\n\t\t\t})\n\t\t\treturn { txHash, submitted }\n\t\t},\n\t\tensureApiKey: ({ aud, expIn, store = defaultStore, leewaySeconds = 60 }) => {\n\t\t\tconst cached = store.get(aud)\n\t\t\tif (cached && isApiKeyValid(cached, aud, leewaySeconds)) return Promise.resolve(cached)\n\t\t\tconst inFlight = pendingApiKeys.get(aud)\n\t\t\tif (inFlight) return inFlight\n\t\t\tconst request = requireProvider()\n\t\t\t\t.generateApiKey({ aud, exp_in: expIn })\n\t\t\t\t.then(({ apiKey }) => {\n\t\t\t\t\tstore.set(apiKey, aud)\n\t\t\t\t\treturn apiKey\n\t\t\t\t})\n\t\t\t\t.finally(() => {\n\t\t\t\t\tpendingApiKeys.delete(aud)\n\t\t\t\t})\n\t\t\tpendingApiKeys.set(aud, request)\n\t\t\treturn request\n\t\t},\n\t\ton: (event, cb) => requireProvider().on(event, cb),\n\t\toff: (event, cb) => requireProvider().off(event, cb)\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA+BA,SAAS,oBAAiC;CACzC,MAAM,4BAAY,IAAI,IAAoB;CAC1C,OAAO;EACN,MAAM,MAAM,OAAO,UAAU,IAAI,GAAG,KAAK;EACzC,MAAM,QAAQ,MAAM,OAAO;GAC1B,UAAU,IAAI,KAAK,MAAM;EAC1B;CACD;AACD;AAEA,SAAS,cAAc,QAAgB,KAAa,eAAgC;CACnF,IAAI;EACH,MAAM,EAAE,YAAY,aAAa,MAAM;EACvC,OAAO,QAAQ,QAAQ,OAAO,QAAQ,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI;CAC7E,QAAQ;EACP,OAAO;CACR;AACD;;;;;AAsCA,SAAgB,2BAA2B,kBAAkD;CAC5F,MAAM,oBAAoB,oBAAoB,mBAAmB;CACjE,MAAM,wBAAyC;EAC9C,MAAM,WAAW,YAAY;EAC7B,IAAI,CAAC,UACJ,MAAM,IAAI,eAAe,oBAAoB,iCAAiC;EAC/E,OAAO;CACR;CAEA,MAAM,eAAe,kBAAkB;CACvC,MAAM,iCAAiB,IAAI,IAA6B;CAExD,OAAO;EACN,mBAAmB,YAAY,MAAM;EACrC;EACA,SAAS,YAAY;GACpB,MAAM,WAAW,MAAM,gBAAgB,CAAC,CAAC,gBAAgB;GACzD,IAAI,CAAC,SAAS,QACb,MAAM,IAAI,eAAe,oBAAoB,qBAAqB;GACnE,OAAO,SAAS;EACjB;EACA,kBAAkB,gBAAgB,CAAC,CAAC,WAAW;EAC/C,kBAAkB,gBAAgB,CAAC,CAAC,WAAW;EAC/C,eAAe,OAAO,WAAW;GAChC,MAAM,WAAW,gBAAgB;GACjC,IAAI,OAAO,SAAS,yBAAyB,YAC5C,MAAM,IAAI,eAAe,eAAe,uCAAuC;GAChF,OAAO,SAAS,qBAAqB,MAAM;EAC5C;EACA,kBAAkB,YAAY,gBAAgB,CAAC,CAAC,gBAAgB,OAAO;EACvE,eAAe,OAAO,SAAS,YAAY;GAC1C,MAAM,EAAE,QAAQ,aAAa,MAAM,gBAAgB,CAAC,CAAC,gBAAgB,OAAO;GAK5E,OAAO;IAAE;IAAQ,WAAA,MAJO,kBAAkB,UAAU;KACnD,SAAS,QAAQ;KACjB,MAAM,QAAQ;IACf,CAAC;GAC0B;EAC5B;EACA,eAAe,EAAE,KAAK,OAAO,QAAQ,cAAc,gBAAgB,SAAS;GAC3E,MAAM,SAAS,MAAM,IAAI,GAAG;GAC5B,IAAI,UAAU,cAAc,QAAQ,KAAK,aAAa,GAAG,OAAO,QAAQ,QAAQ,MAAM;GACtF,MAAM,WAAW,eAAe,IAAI,GAAG;GACvC,IAAI,UAAU,OAAO;GACrB,MAAM,UAAU,gBAAgB,CAAC,CAC/B,eAAe;IAAE;IAAK,QAAQ;GAAM,CAAC,CAAC,CACtC,MAAM,EAAE,aAAa;IACrB,MAAM,IAAI,QAAQ,GAAG;IACrB,OAAO;GACR,CAAC,CAAC,CACD,cAAc;IACd,eAAe,OAAO,GAAG;GAC1B,CAAC;GACF,eAAe,IAAI,KAAK,OAAO;GAC/B,OAAO;EACR;EACA,KAAK,OAAO,OAAO,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE;EACjD,MAAM,OAAO,OAAO,gBAAgB,CAAC,CAAC,IAAI,OAAO,EAAE;CACpD;AACD"}
1
+ {"version":3,"file":"injected.js","names":[],"sources":["../../src/client/injected.ts"],"sourcesContent":["/**\n * A framework-agnostic dApp client over the injected `window.amadeus` provider\n * (the browser extension). It wraps the raw provider with the ergonomics every\n * dApp currently hand-rolls: a single-account `connect`, sign-and-submit, and a\n * cached `ensureApiKey` that decodes expiry via the audited api-key module\n * (replacing the copies in hub / prime-hub).\n *\n * The mobile deep-link/QR path uses the transport module (connect handshake +\n * sealed envelopes) instead of an injected provider; this client is the\n * same-device, extension-present case.\n */\n\nimport { decodeApiKey } from '../crypto/apikey'\nimport { WalletSdkError } from '../errors'\nimport { submitTransaction } from '../tx'\nimport type {\n\tAmadeusEventPayloadMap,\n\tAmadeusProvider,\n\tAmadeusProviderEvent,\n\tSignTransactionRequest,\n\tSignTransactionResponse\n} from '../types'\nimport { getAmadeusProvider } from '../types'\n\n/** Where a cached api-key lives. Default is in-memory; dApps can pass localStorage-backed. */\nexport interface ApiKeyStore {\n\t/** `aud` is passed so a store can keep one key per audience. */\n\tget(aud?: string): string | null\n\tset(apiKey: string, aud?: string): void\n}\n\nfunction memoryApiKeyStore(): ApiKeyStore {\n\tconst keysByAud = new Map<string, string>()\n\treturn {\n\t\tget: (aud = '') => keysByAud.get(aud) ?? null,\n\t\tset: (apiKey, aud = '') => {\n\t\t\tkeysByAud.set(aud, apiKey)\n\t\t}\n\t}\n}\n\nfunction isApiKeyValid(\n\tapiKey: string,\n\taud: string,\n\taccount: string | null,\n\tleewaySeconds: number\n): boolean {\n\ttry {\n\t\tconst { payload, address } = decodeApiKey(apiKey)\n\t\treturn (\n\t\t\taddress === account &&\n\t\t\tpayload.aud === aud &&\n\t\t\tpayload.exp > Math.floor(Date.now() / 1000) + leewaySeconds\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\nexport interface WalletClient {\n\tisAvailable(): boolean\n\tgetProvider(): AmadeusProvider | null\n\t/** Prompt connection; resolves to the connected account (Base58 address). */\n\tconnect(): Promise<string>\n\tgetAccount(): Promise<string | null>\n\tgetNetwork(): Promise<string>\n\t/** Ask the wallet to switch the active account (user-confirmed); resolves to the new address. */\n\tswitchAccount(target?: string): Promise<string>\n\tsignTransaction(request: SignTransactionRequest): Promise<SignTransactionResponse>\n\t/** Sign then submit the packed bytes to a node; resolves to the tx hash + node reply. */\n\tsignAndSubmit(\n\t\trequest: SignTransactionRequest,\n\t\toptions: { nodeUrl: string; wait?: boolean }\n\t): Promise<{ txHash: string; submitted: unknown }>\n\t/** Return a valid cached api-key for `aud` signed by the active account, minting (and caching) a fresh one if needed. Coalesces concurrent calls per account + aud. */\n\tensureApiKey(options: {\n\t\taud: string\n\t\texpIn: number\n\t\tstore?: ApiKeyStore\n\t\tleewaySeconds?: number\n\t}): Promise<string>\n\ton<E extends AmadeusProviderEvent>(\n\t\tevent: E,\n\t\tcb: (data: AmadeusEventPayloadMap[E]) => void\n\t): void\n\toff<E extends AmadeusProviderEvent>(\n\t\tevent: E,\n\t\tcb: (data: AmadeusEventPayloadMap[E]) => void\n\t): void\n}\n\n/**\n * Create a client bound to the injected provider. Pass `providerOverride` to\n * inject a provider (e.g. in tests); otherwise it reads `window.amadeus`.\n */\nexport function createInjectedWalletClient(providerOverride?: AmadeusProvider): WalletClient {\n\tconst getProvider = () => providerOverride ?? getAmadeusProvider()\n\tconst requireProvider = (): AmadeusProvider => {\n\t\tconst provider = getProvider()\n\t\tif (!provider)\n\t\t\tthrow new WalletSdkError('INVALID_ARGUMENT', 'Amadeus wallet is not available')\n\t\treturn provider\n\t}\n\n\tconst defaultStore = memoryApiKeyStore()\n\tconst pendingApiKeys = new Map<string, Promise<string>>()\n\n\treturn {\n\t\tisAvailable: () => getProvider() !== null,\n\t\tgetProvider,\n\t\tconnect: async () => {\n\t\t\tconst accounts = await requireProvider().requestAccounts()\n\t\t\tif (!accounts.length)\n\t\t\t\tthrow new WalletSdkError('INVALID_ARGUMENT', 'no account returned')\n\t\t\treturn accounts[0]\n\t\t},\n\t\tgetAccount: () => requireProvider().getAccount(),\n\t\tgetNetwork: () => requireProvider().getNetwork(),\n\t\tswitchAccount: async (target) => {\n\t\t\tconst provider = requireProvider()\n\t\t\tif (typeof provider.requestSwitchAccount !== 'function')\n\t\t\t\tthrow new WalletSdkError('UNSUPPORTED', 'wallet does not support switchAccount')\n\t\t\treturn provider.requestSwitchAccount(target)\n\t\t},\n\t\tsignTransaction: (request) => requireProvider().signTransaction(request),\n\t\tsignAndSubmit: async (request, options) => {\n\t\t\tconst { txHash, txPacked } = await requireProvider().signTransaction(request)\n\t\t\tconst submitted = await submitTransaction(txPacked, {\n\t\t\t\tnodeUrl: options.nodeUrl,\n\t\t\t\twait: options.wait\n\t\t\t})\n\t\t\treturn { txHash, submitted }\n\t\t},\n\t\tensureApiKey: async ({ aud, expIn, store = defaultStore, leewaySeconds = 60 }) => {\n\t\t\tconst provider = requireProvider()\n\t\t\tconst account = await provider.getAccount()\n\t\t\tconst cached = store.get(aud)\n\t\t\tif (cached && isApiKeyValid(cached, aud, account, leewaySeconds)) return cached\n\t\t\tconst pendingKey = JSON.stringify([account, aud])\n\t\t\tconst inFlight = pendingApiKeys.get(pendingKey)\n\t\t\tif (inFlight) return inFlight\n\t\t\tconst request = provider\n\t\t\t\t.generateApiKey({ aud, exp_in: expIn })\n\t\t\t\t.then(({ apiKey }) => {\n\t\t\t\t\tstore.set(apiKey, aud)\n\t\t\t\t\treturn apiKey\n\t\t\t\t})\n\t\t\t\t.finally(() => {\n\t\t\t\t\tpendingApiKeys.delete(pendingKey)\n\t\t\t\t})\n\t\t\tpendingApiKeys.set(pendingKey, request)\n\t\t\treturn request\n\t\t},\n\t\ton: (event, cb) => requireProvider().on(event, cb),\n\t\toff: (event, cb) => requireProvider().off(event, cb)\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA+BA,SAAS,oBAAiC;CACzC,MAAM,4BAAY,IAAI,IAAoB;CAC1C,OAAO;EACN,MAAM,MAAM,OAAO,UAAU,IAAI,GAAG,KAAK;EACzC,MAAM,QAAQ,MAAM,OAAO;GAC1B,UAAU,IAAI,KAAK,MAAM;EAC1B;CACD;AACD;AAEA,SAAS,cACR,QACA,KACA,SACA,eACU;CACV,IAAI;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W,gBAAgB;GACjC,MAAM,UAAU,MAAM,SAAS,WAAW;GAC1C,MAAM,SAAS,MAAM,IAAI,GAAG;GAC5B,IAAI,UAAU,cAAc,QAAQ,KAAK,SAAS,aAAa,GAAG,OAAO;GACzE,MAAM,aAAa,KAAK,UAAU,CAAC,SAAS,GAAG,CAAC;GAChD,MAAM,WAAW,eAAe,IAAI,UAAU;GAC9C,IAAI,UAAU,OAAO;GACrB,MAAM,UAAU,SACd,eAAe;IAAE;IAAK,QAAQ;GAAM,CAAC,CAAC,CACtC,MAAM,EAAE,aAAa;IACrB,MAAM,IAAI,QAAQ,GAAG;IACrB,OAAO;GACR,CAAC,CAAC,CACD,cAAc;IACd,eAAe,OAAO,UAAU;GACjC,CAAC;GACF,eAAe,IAAI,YAAY,OAAO;GACtC,OAAO;EACR;EACA,KAAK,OAAO,OAAO,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE;EACjD,MAAM,OAAO,OAAO,gBAAgB,CAAC,CAAC,IAAI,OAAO,EAAE;CACpD;AACD"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/react/index.ts"],"sourcesContent":["/**\n * React ergonomics for dApps: a single `useWallet()` hook over the injected\n * provider client. It fixes the `amadeus#initialized` vs `amadeus-wallet#initialized`\n * detection bug once (dApps historically listened for the wrong name) and wires\n * the provider's account/network/disconnect events into state.\n *\n * Imported from the `@amadeus-protocol/wallet-sdk/react` subpath so non-React\n * consumers (the mobile wallet, node verifiers) never pull in React.\n */\n\nimport { useCallback, useEffect, useMemo, useState } from 'react'\n\nimport { createInjectedWalletClient, type WalletClient } from '../client/injected'\nimport { WALLET_INITIALIZED_EVENT } from '../constants'\nimport type {\n\tAmadeusEventPayloadMap,\n\tSignTransactionRequest,\n\tSignTransactionResponse\n} from '../types'\n\nexport type WalletStatus = 'unavailable' | 'disconnected' | 'connecting' | 'connected'\n\nexport interface UseWalletResult {\n\tclient: WalletClient\n\tstatus: WalletStatus\n\taccount: string | null\n\tnetwork: string | null\n\tisAvailable: boolean\n\tconnect: () => Promise<void>\n\tdisconnect: () => void\n\t/** Switch the active account (user-confirmed in the wallet); `account` reflects the change via the provider's accountsChanged event. */\n\tswitchAccount: (target?: string) => Promise<string>\n\tsignTransaction: (request: SignTransactionRequest) => Promise<SignTransactionResponse>\n\tsignAndSubmit: WalletClient['signAndSubmit']\n\tensureApiKey: WalletClient['ensureApiKey']\n}\n\nexport function useWallet(): UseWalletResult {\n\tconst client = useMemo(() => createInjectedWalletClient(), [])\n\tconst [status, setStatus] = useState<WalletStatus>(() =>\n\t\tclient.isAvailable() ? 'disconnected' : 'unavailable'\n\t)\n\tconst [account, setAccount] = useState<string | null>(null)\n\tconst [network, setNetwork] = useState<string | null>(null)\n\n\t// Detect a (possibly late-injected) provider, then read initial account/network.\n\tuseEffect(() => {\n\t\tif (typeof window === 'undefined') return\n\t\tconst onInit = () => {\n\t\t\tif (client.isAvailable()) setStatus((s) => (s === 'unavailable' ? 'disconnected' : s))\n\t\t}\n\t\twindow.addEventListener(WALLET_INITIALIZED_EVENT, onInit)\n\t\tif (client.isAvailable()) {\n\t\t\tclient\n\t\t\t\t.getAccount()\n\t\t\t\t.then((a) => {\n\t\t\t\t\tif (a) {\n\t\t\t\t\t\tsetAccount(a)\n\t\t\t\t\t\tsetStatus('connected')\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t\tclient\n\t\t\t\t.getNetwork()\n\t\t\t\t.then(setNetwork)\n\t\t\t\t.catch(() => {})\n\t\t}\n\t\treturn () => window.removeEventListener(WALLET_INITIALIZED_EVENT, onInit)\n\t}, [client])\n\n\t// Mirror provider events into state.\n\tuseEffect(() => {\n\t\tif (!client.isAvailable()) return\n\t\tconst applyAccounts = (accounts: string[]) => {\n\t\t\tconst next = accounts[0] ?? null\n\t\t\tsetAccount(next)\n\t\t\tsetStatus(next ? 'connected' : 'disconnected')\n\t\t}\n\t\tconst onAccountsChanged = (d: AmadeusEventPayloadMap['accountsChanged']) =>\n\t\t\tapplyAccounts(d.accounts)\n\t\tconst onConnect = (d: AmadeusEventPayloadMap['connect']) => {\n\t\t\tapplyAccounts(d.accounts)\n\t\t\t// The extension sends `network: null` before it knows one; keep the last known value.\n\t\t\tif (d.network) setNetwork(d.network)\n\t\t}\n\t\tconst onDisconnect = () => {\n\t\t\tsetAccount(null)\n\t\t\tsetStatus('disconnected')\n\t\t}\n\t\tconst onNetworkChanged = (d: AmadeusEventPayloadMap['networkChanged']) =>\n\t\t\tsetNetwork(d.network)\n\n\t\tclient.on('accountsChanged', onAccountsChanged)\n\t\tclient.on('connect', onConnect)\n\t\tclient.on('disconnect', onDisconnect)\n\t\tclient.on('networkChanged', onNetworkChanged)\n\t\treturn () => {\n\t\t\tclient.off('accountsChanged', onAccountsChanged)\n\t\t\tclient.off('connect', onConnect)\n\t\t\tclient.off('disconnect', onDisconnect)\n\t\t\tclient.off('networkChanged', onNetworkChanged)\n\t\t}\n\t}, [client])\n\n\tconst connect = useCallback(async () => {\n\t\tsetStatus('connecting')\n\t\ttry {\n\t\t\tconst a = await client.connect()\n\t\t\tsetAccount(a)\n\t\t\tsetStatus('connected')\n\t\t} catch (error) {\n\t\t\tsetStatus(client.isAvailable() ? 'disconnected' : 'unavailable')\n\t\t\tthrow error\n\t\t}\n\t}, [client])\n\n\tconst disconnect = useCallback(() => {\n\t\tsetAccount(null)\n\t\tsetStatus('disconnected')\n\t}, [])\n\n\treturn {\n\t\tclient,\n\t\tstatus,\n\t\taccount,\n\t\tnetwork,\n\t\tisAvailable: client.isAvailable(),\n\t\tconnect,\n\t\tdisconnect,\n\t\tswitchAccount: client.switchAccount,\n\t\tsignTransaction: client.signTransaction,\n\t\tsignAndSubmit: client.signAndSubmit,\n\t\tensureApiKey: client.ensureApiKey\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;AAqCA,SAAgB,YAA6B;CAC5C,MAAM,SAAS,cAAc,2BAA2B,GAAG,CAAC,CAAC;CAC7D,MAAM,CAAC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,OAAO,IAAI,WAAW,SAAS;GAC/B,OAAO,IAAI,cAAc,YAAY;GACrC,OAAO,IAAI,kBAAkB,gBAAgB;EAC9C;CACD,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,UAAU,YAAY,YAAY;EACvC,UAAU,YAAY;EACtB,IAAI;GACH,MAAM,IAAI,MAAM,OAAO,QAAQ;GAC/B,WAAW,CAAC;GACZ,UAAU,WAAW;EACtB,SAAS,OAAO;GACf,UAAU,OAAO,YAAY,IAAI,iBAAiB,aAAa;GAC/D,MAAM;EACP;CACD,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,aAAa,kBAAkB;EACpC,WAAW,IAAI;EACf,UAAU,cAAc;CACzB,GAAG,CAAC,CAAC;CAEL,OAAO;EACN;EACA;EACA;EACA;EACA,aAAa,OAAO,YAAY;EAChC;EACA;EACA,eAAe,OAAO;EACtB,iBAAiB,OAAO;EACxB,eAAe,OAAO;EACtB,cAAc,OAAO;CACtB;AACD"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/react/index.ts"],"sourcesContent":["/**\n * React ergonomics for dApps: a single `useWallet()` hook over the injected\n * provider client. It fixes the `amadeus#initialized` vs `amadeus-wallet#initialized`\n * detection bug once (dApps historically listened for the wrong name) and wires\n * the provider's account/network/disconnect events into state.\n *\n * Imported from the `@amadeus-protocol/wallet-sdk/react` subpath so non-React\n * consumers (the mobile wallet, node verifiers) never pull in React.\n */\n\nimport { useCallback, useEffect, useMemo, useState } from 'react'\n\nimport { createInjectedWalletClient, type WalletClient } from '../client/injected'\nimport { WALLET_INITIALIZED_EVENT } from '../constants'\nimport type {\n\tAmadeusEventPayloadMap,\n\tSignTransactionRequest,\n\tSignTransactionResponse\n} from '../types'\n\nexport type WalletStatus = 'unavailable' | 'disconnected' | 'connecting' | 'connected'\n\nexport interface UseWalletResult {\n\tclient: WalletClient\n\tstatus: WalletStatus\n\taccount: string | null\n\tnetwork: string | null\n\tisAvailable: boolean\n\tconnect: () => Promise<void>\n\tdisconnect: () => void\n\t/** Switch the active account (user-confirmed in the wallet); `account` reflects the change via the provider's accountsChanged event. */\n\tswitchAccount: (target?: string) => Promise<string>\n\tsignTransaction: (request: SignTransactionRequest) => Promise<SignTransactionResponse>\n\tsignAndSubmit: WalletClient['signAndSubmit']\n\tensureApiKey: WalletClient['ensureApiKey']\n}\n\nexport function useWallet(): UseWalletResult {\n\tconst client = useMemo(() => createInjectedWalletClient(), [])\n\tconst [status, setStatus] = useState<WalletStatus>(() =>\n\t\tclient.isAvailable() ? 'disconnected' : 'unavailable'\n\t)\n\tconst [account, setAccount] = useState<string | null>(null)\n\tconst [network, setNetwork] = useState<string | null>(null)\n\n\t// Detect a (possibly late-injected) provider, then read initial account/network.\n\tuseEffect(() => {\n\t\tif (typeof window === 'undefined') return\n\t\tconst onInit = () => {\n\t\t\tif (client.isAvailable()) setStatus((s) => (s === 'unavailable' ? 'disconnected' : s))\n\t\t}\n\t\twindow.addEventListener(WALLET_INITIALIZED_EVENT, onInit)\n\t\tif (client.isAvailable()) {\n\t\t\tclient\n\t\t\t\t.getAccount()\n\t\t\t\t.then((a) => {\n\t\t\t\t\tif (a) {\n\t\t\t\t\t\tsetAccount(a)\n\t\t\t\t\t\tsetStatus('connected')\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(() => {})\n\t\t\tclient\n\t\t\t\t.getNetwork()\n\t\t\t\t.then(setNetwork)\n\t\t\t\t.catch(() => {})\n\t\t}\n\t\treturn () => window.removeEventListener(WALLET_INITIALIZED_EVENT, onInit)\n\t}, [client])\n\n\t// Mirror provider events into state.\n\tuseEffect(() => {\n\t\tif (!client.isAvailable()) return\n\t\tconst applyAccounts = (accounts: string[]) => {\n\t\t\tconst next = accounts[0] ?? null\n\t\t\tsetAccount(next)\n\t\t\tsetStatus(next ? 'connected' : 'disconnected')\n\t\t}\n\t\tconst onAccountsChanged = (d: AmadeusEventPayloadMap['accountsChanged']) =>\n\t\t\tapplyAccounts(d.accounts)\n\t\tconst onConnect = (d: AmadeusEventPayloadMap['connect']) => {\n\t\t\tapplyAccounts(d.accounts)\n\t\t\t// Extension 1.9.2 always sends a network here; other providers may omit it, so keep the last value.\n\t\t\tif (d.network) setNetwork(d.network)\n\t\t}\n\t\tconst onDisconnect = () => {\n\t\t\tsetAccount(null)\n\t\t\tsetStatus('disconnected')\n\t\t}\n\t\tconst onNetworkChanged = (d: AmadeusEventPayloadMap['networkChanged']) =>\n\t\t\tsetNetwork(d.network)\n\n\t\tclient.on('accountsChanged', onAccountsChanged)\n\t\tclient.on('connect', onConnect)\n\t\tclient.on('disconnect', onDisconnect)\n\t\tclient.on('networkChanged', onNetworkChanged)\n\t\treturn () => {\n\t\t\tclient.off('accountsChanged', onAccountsChanged)\n\t\t\tclient.off('connect', onConnect)\n\t\t\tclient.off('disconnect', onDisconnect)\n\t\t\tclient.off('networkChanged', onNetworkChanged)\n\t\t}\n\t}, [client])\n\n\tconst connect = useCallback(async () => {\n\t\tsetStatus('connecting')\n\t\ttry {\n\t\t\tconst a = await client.connect()\n\t\t\tsetAccount(a)\n\t\t\tsetStatus('connected')\n\t\t} catch (error) {\n\t\t\tsetStatus(client.isAvailable() ? 'disconnected' : 'unavailable')\n\t\t\tthrow error\n\t\t}\n\t}, [client])\n\n\tconst disconnect = useCallback(() => {\n\t\tsetAccount(null)\n\t\tsetStatus('disconnected')\n\t}, [])\n\n\treturn {\n\t\tclient,\n\t\tstatus,\n\t\taccount,\n\t\tnetwork,\n\t\tisAvailable: client.isAvailable(),\n\t\tconnect,\n\t\tdisconnect,\n\t\tswitchAccount: client.switchAccount,\n\t\tsignTransaction: client.signTransaction,\n\t\tsignAndSubmit: client.signAndSubmit,\n\t\tensureApiKey: client.ensureApiKey\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;AAqCA,SAAgB,YAA6B;CAC5C,MAAM,SAAS,cAAc,2BAA2B,GAAG,CAAC,CAAC;CAC7D,MAAM,CAAC,QAAQ,aAAa,eAC3B,OAAO,YAAY,IAA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,IAAI,cAAc,YAAY;GACrC,OAAO,IAAI,kBAAkB,gBAAgB;EAC9C;CACD,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,UAAU,YAAY,YAAY;EACvC,UAAU,YAAY;EACtB,IAAI;GACH,MAAM,IAAI,MAAM,OAAO,QAAQ;GAC/B,WAAW,CAAC;GACZ,UAAU,WAAW;EACtB,SAAS,OAAO;GACf,UAAU,OAAO,YAAY,IAAI,iBAAiB,aAAa;GAC/D,MAAM;EACP;CACD,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,aAAa,kBAAkB;EACpC,WAAW,IAAI;EACf,UAAU,cAAc;CACzB,GAAG,CAAC,CAAC;CAEL,OAAO;EACN;EACA;EACA;EACA;EACA,aAAa,OAAO,YAAY;EAChC;EACA;EACA,eAAe,OAAO;EACtB,iBAAiB,OAAO;EACxB,eAAe,OAAO;EACtB,cAAc,OAAO;CACtB;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amadeus-protocol/wallet-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Shared dApp <-> wallet connection layer for Amadeus Protocol: BLS API-key tokens, transaction signing, message/connect signing, and the typed provider contract. One audited implementation for the wallet, the extension, and every dApp.",
5
5
  "type": "module",
6
6
  "sideEffects": false,