@etherplay/connect 0.0.3 → 0.0.5

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/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { AlchemyMechanism, OriginAccount } from '@etherplay/alchemy';
2
2
  import type { EIP1193WindowWalletProvider } from 'eip-1193';
3
- import { fromEntropyKeyToMnemonic } from '@etherplay/alchemy';
4
- export { fromEntropyKeyToMnemonic };
3
+ import { fromEntropyKeyToMnemonic, originKeyMessage, originPublicKeyPublicationMessage } from '@etherplay/alchemy';
4
+ export { fromEntropyKeyToMnemonic, originPublicKeyPublicationMessage, originKeyMessage };
5
5
  export type { OriginAccount };
6
6
  export type PopupSettings = {
7
7
  walletHost: string;
@@ -81,4 +81,5 @@ export declare function createConnection(settings: {
81
81
  requestSignature: () => Promise<void>;
82
82
  connectOnCurrentWalletAccount: (address: `0x${string}`) => void;
83
83
  disconnect: () => void;
84
+ getSignatureForPublicKeyPublication: () => Promise<`0x${string}`>;
84
85
  };
package/dist/index.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { writable } from 'svelte/store';
2
2
  import { createPopupLauncher } from './popup.js';
3
- import { fromEntropyKeyToMnemonic, fromMnemonicToFirstAccount, fromSignatureToKey, originKeyMessage } from '@etherplay/alchemy';
4
- import { bytesToHex } from '@noble/hashes/utils';
5
- export { fromEntropyKeyToMnemonic };
6
- const encoder = new TextEncoder();
3
+ import { fromEntropyKeyToMnemonic, fromMnemonicToFirstAccount, fromSignatureToKey, originKeyMessage, originPublicKeyPublicationMessage } from '@etherplay/alchemy';
4
+ import { hashMessage } from './utils.js';
5
+ export { fromEntropyKeyToMnemonic, originPublicKeyPublicationMessage, originKeyMessage };
7
6
  const storageAccountKey = '__origin_account';
8
7
  export function createConnection(settings) {
9
8
  let autoConnect = true;
@@ -146,16 +145,32 @@ export function createConnection(settings) {
146
145
  throw new Error(`no wallet provided initialised`);
147
146
  }
148
147
  const message = originKeyMessage(origin);
149
- const messageAsBytes = encoder.encode(message);
150
- const msg = `0x${bytesToHex(messageAsBytes)}`;
148
+ const msg = hashMessage(message);
151
149
  set({
152
150
  ...$connection,
153
151
  step: 'WaitingForSignature'
154
152
  });
155
- const signature = await provider.request({
156
- method: 'personal_sign',
157
- params: [msg, $connection.mechanism.address]
158
- });
153
+ let signature;
154
+ try {
155
+ signature = await provider.request({
156
+ method: 'personal_sign',
157
+ params: [msg, $connection.mechanism.address]
158
+ });
159
+ }
160
+ catch (err) {
161
+ // TODO handle rejection (code: 4001 ?)
162
+ set({
163
+ ...$connection,
164
+ step: 'NeedWalletSignature',
165
+ mechanism: {
166
+ type: 'wallet',
167
+ name: $connection.mechanism.name,
168
+ address: $connection.mechanism.address
169
+ },
170
+ error: { message: 'failed to sign message', cause: err }
171
+ });
172
+ return;
173
+ }
159
174
  const originKey = fromSignatureToKey(signature);
160
175
  const originMnemonic = fromEntropyKeyToMnemonic(originKey);
161
176
  const originAccount = fromMnemonicToFirstAccount(originMnemonic);
@@ -164,11 +179,13 @@ export function createConnection(settings) {
164
179
  signer: {
165
180
  origin,
166
181
  address: originAccount.address,
182
+ publicKey: originAccount.publicKey,
167
183
  privateKey: originAccount.privateKey,
168
184
  mnemonicKey: originKey
169
185
  },
170
186
  metadata: {},
171
- mechanismUsed: $connection.mechanism
187
+ mechanismUsed: $connection.mechanism,
188
+ savedPublicKeyPublicationSignature: undefined
172
189
  };
173
190
  set({
174
191
  ...$connection,
@@ -459,6 +476,29 @@ export function createConnection(settings) {
459
476
  popup?.cancel();
460
477
  set({ step: 'Idle', loading: false, wallets: $connection.wallets });
461
478
  }
479
+ function getSignatureForPublicKeyPublication() {
480
+ if ($connection.step !== 'SignedIn') {
481
+ throw new Error('Not signed in');
482
+ }
483
+ const account = $connection.account;
484
+ if ($connection.mechanism.type === 'wallet') {
485
+ if (!walletProvider) {
486
+ throw new Error(`no provider`);
487
+ }
488
+ const message = originPublicKeyPublicationMessage(origin, account.signer.publicKey);
489
+ const msg = hashMessage(message);
490
+ return walletProvider.request({
491
+ method: 'personal_sign',
492
+ params: [msg, account.address]
493
+ });
494
+ }
495
+ if (account.savedPublicKeyPublicationSignature) {
496
+ return Promise.resolve(account.savedPublicKeyPublicationSignature);
497
+ }
498
+ // TODO offer a way to use iframe + popup to sign the message
499
+ // this would require saving mnemonic or privatekey on etherplay localstorage though
500
+ throw new Error(`no saved public key publication signature for ${account.address}`);
501
+ }
462
502
  return {
463
503
  subscribe: _store.subscribe,
464
504
  connect,
@@ -466,6 +506,7 @@ export function createConnection(settings) {
466
506
  back,
467
507
  requestSignature,
468
508
  connectOnCurrentWalletAccount,
469
- disconnect
509
+ disconnect,
510
+ getSignatureForPublicKeyPublication
470
511
  };
471
512
  }
package/dist/utils.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import type { Readable } from 'svelte/store';
2
2
  export declare function createStorePromise<U, T, V extends Readable<T>>(store: V, executor: (resolve: (value: U | PromiseLike<U>) => void, reject: (reason?: any) => void) => void): Promise<U> & V;
3
+ export declare function hashMessage(message: string): `0x${string}`;
package/dist/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { bytesToHex } from '@noble/hashes/utils';
1
2
  export function createStorePromise(store, executor) {
2
3
  const storePromise = new Promise(executor);
3
4
  for (const key of Object.keys(store)) {
@@ -11,3 +12,9 @@ export function createStorePromise(store, executor) {
11
12
  }
12
13
  return storePromise;
13
14
  }
15
+ const encoder = new TextEncoder();
16
+ export function hashMessage(message) {
17
+ const messageAsBytes = encoder.encode(message);
18
+ const msg = `0x${bytesToHex(messageAsBytes)}`;
19
+ return msg;
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etherplay/connect",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -52,7 +52,7 @@
52
52
  "@scure/bip32": "^1.6.2",
53
53
  "@scure/bip39": "^1.5.4",
54
54
  "zustand": "^5.0.3",
55
- "@etherplay/alchemy": "0.0.2"
55
+ "@etherplay/alchemy": "0.0.4"
56
56
  },
57
57
  "scripts": {
58
58
  "dev": "vite dev --host --port 60002",