@silencelaboratories/walletprovider-sdk 0.0.5 → 0.0.7

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
@@ -1,5 +1,129 @@
1
1
  # walletprovider-sdk
2
2
 
3
+ The frontend library for Silent Network.
4
+
5
+ # Demo
6
+
7
+ Under the [demo](./demo) directory we provide the sample webpage that shows example usage of the library.
8
+ In order to run it execute the following:
9
+
10
+ 1. Build the library
11
+
12
+ ```
13
+ bun install
14
+ bun run build
15
+ ```
16
+
17
+ 2. Run the demo
18
+
19
+ ```
20
+ cd demo
21
+ npm install
22
+ npm run dev
23
+ ```
24
+
25
+ The demo fetches configuration from the `.env` file. We provided example `.env` [here](./demo/.env).
26
+
27
+ The demo communicates with [backend service](#the-backend). Run it before using the demo page.
28
+
29
+ # Using the library
30
+
31
+ The library is published on [npmjs](https://www.npmjs.com/package/@silencelaboratories/walletprovider-sdk) registry.
32
+
33
+ Install it in your project as usual:
34
+
35
+ ```
36
+ npm i @silencelaboratories/walletprovider-sdk
37
+ ```
38
+
39
+ ## The backend
40
+
41
+ The library communicates with backend service. The example implementation of such service is accessible [here](https://shipyard.rs/silencelaboratories/crates/wallet-provider-service). Please refer to backend documentation in order to run the service.
42
+
43
+ Frontend uses [WalletProviderServiceClient](./docs/walletprovider-sdk.walletproviderserviceclient.md) object in order to connect to the backend and send requests.
44
+
45
+ # Documentation
46
+
47
+ For description of classes, interfaces, types, please refer to [documentation](./docs/walletprovider-sdk.md).
48
+
49
+ ## Keygen
50
+
51
+ The full working example is in the [demo](./demo/src/routes/%2Bpage.svelte#L87).
52
+ The core object to use is the [NetworkSigner](./docs/walletprovider-sdk.networksigner.md).
53
+
54
+ It allows to generate keys and do signatures. In order to create, you need two other components. The [WalletProviderServiceClient](./docs/walletprovider-sdk.walletproviderserviceclient.md) that connects to the Backend part of the SDK, and the authentication module. Currently we provide EOA authentication via [EOAAuth](./docs/walletprovider-sdk.eoaauth.md).
55
+
56
+ Let's create the `NetworkSigner`
57
+
58
+ ```ts
59
+ const wpClient = await createWalletProviderService(clusterConfig);
60
+ // Authenticate using EOA
61
+ const eoaAuth = new EOAAuth(accountsFromBrowserWallet[0], new BrowserWallet());
62
+
63
+ // Create a new signer instance
64
+ const sdk = new NetworkSigner(wpClient, threshold, partiesNumber, eoaAuth);
65
+ ```
66
+
67
+ Now you can generate a key, using the [authenticateAndCreateKey](./docs/walletprovider-sdk.networksigner.authenticateandcreatekey.md) method. The method accepts optional permissions. No permissions means _allow all operations_.
68
+
69
+ ```ts
70
+ const permissions = {
71
+ permissions: [
72
+ {
73
+ type: 'erc20',
74
+ method: 'approve',
75
+ to: '0x1234567890123456789012345678901234567890',
76
+ args: {
77
+ spender: '0x1234567890123456789012345678901234567890',
78
+ value: 10000,
79
+ eq: '<',
80
+ },
81
+ },
82
+ ],
83
+ };
84
+
85
+ // Generate a new key
86
+ let resp: KeygenResponse = await sdk.authenticateAndCreateKey(JSON.stringify(permissions));
87
+ ```
88
+
89
+ Calling this method will cause to the Browser Wallet window to pop up, requesting the User to sign the request. After execution [KeygenResponse](./docs/walletprovider-sdk.keygenresponse.md) is returned.
90
+
91
+ The [KeygenResponse](./docs/walletprovider-sdk.keygenresponse.md) contains `keyId` and `publicKey`. The `publicKey` is the public part of the key generated by Silent Network. Use the `keyId` in subsequent calls to sign.
92
+
93
+ ## Signing
94
+
95
+ The full signing example is [here](./demo/src/routes/%2Bpage.svelte#L158).
96
+
97
+ Use the [NetworkSigner.authenticateAndSign](./docs/walletprovider-sdk.networksigner.authenticateandsign.md) method in order to generate a signature.
98
+
99
+ ```ts
100
+ let signMessage = JSON.stringify({
101
+ userOperation: {
102
+ sender: '0x8d4cb2540d993fe34c646299f1ab4af3012ff34c',
103
+ nonce: '0x7',
104
+ initCode: '',
105
+ callData: '0000189a...',
106
+ callGasLimit: '123130',
107
+ verificationGasLimit: '153427',
108
+ preVerificationGas: '66768',
109
+ maxFeePerGas: '',
110
+ maxPriorityFeePerGas: '',
111
+ paymasterAndData: '',
112
+ },
113
+ entryPointVersion: 'v0.6.0',
114
+ entryPointAddress: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
115
+ chainId: 80002,
116
+ });
117
+
118
+ let resp = await sdk.authenticateAndSign(selectedKeyId, signMessage);
119
+ ```
120
+
121
+ The [SignResponse](./docs/walletprovider-sdk.signresponse.md) contains the signature `sign` and the recovery ID `recid`.
122
+
123
+ # Developing the library
124
+
125
+ Audience of this section are library developers.
126
+
3
127
  ## Bun runtime is required
4
128
 
5
129
  Install bun from https://bun.sh
@@ -31,5 +155,11 @@ bun run test:watch
31
155
  ## Format the code
32
156
 
33
157
  ```bash
34
- npx prettier . --write
158
+ bun run format
159
+ ```
160
+
161
+ ## Generate the documentation
162
+
163
+ ```bash
164
+ bun run docs
35
165
  ```
@@ -1,3 +1,6 @@
1
+ /** Externally Owned Account (EOA) atuhentication. Uses secret key stored on a wallet to sign requests.
2
+ * The requests are presented to the user in a readable form by using TypedData (EIP712).
3
+ */
1
4
  import { KeygenSetupOpts, type SignSetupOpts } from './networkSigner.ts';
2
5
  import { type UserAuthentication } from './authentication.ts';
3
6
  import type { TypedDataDomain } from 'viem';
@@ -5,15 +8,45 @@ export type FieldDefinition = {
5
8
  name: string;
6
9
  type: string;
7
10
  };
11
+ /** EIP-712 Typed data struct definition.
12
+ * @alpha
13
+ * */
8
14
  export type TypedData<T> = {
15
+ /** contains the schema definition of the types that are in `msg` */
9
16
  types: Record<string, Array<FieldDefinition>>;
17
+ /** is the signature domain separator */
10
18
  domain: TypedDataDomain;
19
+ /** points to the type from `types`. It's the root object of `message` */
11
20
  primaryType: string;
21
+ /** the request that User is asked to sign */
12
22
  message: T;
13
23
  };
24
+ /**
25
+ * Interface to implement communication between this library, and a Browser Wallet. In order to
26
+ * request the signature from the User.
27
+ * @alpha
28
+ */
14
29
  export interface IBrowserWallet {
30
+ /** Sign data using the secret key stored on Browser Wallet
31
+ * It creates a popup window, presenting the human readable form of `request`
32
+ * @param from - the address used to sign the request
33
+ * @param request - the request to sign by the User in the form of EIP712 typed data.
34
+ * @throws Throws an error if User rejected signature
35
+ * @example The example implementation:
36
+ * ```ts
37
+ * async signTypedData<T>(from: string, request: TypedData<T>): Promise<unknown> {
38
+ * return await browserWallet.request({
39
+ * method: 'eth_signTypedData_v4',
40
+ * params: [from, JSON.stringify(request)],
41
+ * });
42
+ * }
43
+ * ```
44
+ */
15
45
  signTypedData<T>(from: string, request: TypedData<T>): Promise<unknown>;
16
46
  }
47
+ /** Present the request to the User using wallet UI, and ask for sign.
48
+ * The signature is the authorization for the operation
49
+ */
17
50
  export declare function authenticateUsingEOA({ setup, user_id, challenge, browserWallet, }: {
18
51
  setup: KeygenSetupOpts | SignSetupOpts;
19
52
  user_id: string;
@@ -1 +1 @@
1
- {"version":3,"file":"EOAauthentication.d.ts","sourceRoot":"","sources":["../src/EOAauthentication.ts"],"names":[],"mappings":"AAGA,OAAO,EAAmB,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAC1F,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAE5C,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAOF,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;IACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;IAC9C,MAAM,EAAE,eAAe,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,CAAC,CAAC;CACZ,CAAC;AAEF,MAAM,WAAW,cAAc;IAa7B,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,CAA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EACP,SAAS,EACT,aAAa,GACd,EAAE;IACD,KAAK,EAAE,eAAe,GAAG,aAAa,CAAC;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,cAAc,CAAC;CAC/B,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA4B9B"}
@@ -1,8 +1,12 @@
1
1
  import { KeygenSetupOpts, SignSetupOpts } from './networkSigner';
2
2
  import { IBrowserWallet } from './EOAauthentication';
3
- import { IWalletProviderServiceClient } from './walletProviderServiceClientInterface';
3
+ /** Type of the request authentication
4
+ * @alpha
5
+ */
4
6
  export declare enum AuthMethod {
7
+ /** Authentication using Externally Owned Account */
5
8
  EOA = 0,
9
+ /** No authentication */
6
10
  NONE = 1
7
11
  }
8
12
  export type UserCredentials = {
@@ -20,11 +24,22 @@ export interface AuthModule {
20
24
  challenge: string;
21
25
  }): Promise<UserAuthentication>;
22
26
  }
27
+ /** The `AuthModule` implementing Externally Owned Account authentication.
28
+ * @alpha
29
+ */
23
30
  export declare class EOAAuth implements AuthModule {
31
+ /** User ID, typically the ETH address that is used to do authentication */
24
32
  userId: string;
33
+ /** An interface to the wallet, like MetaMask, that is used to sign the requests */
25
34
  browserWallet: IBrowserWallet;
26
- wpClient: IWalletProviderServiceClient;
27
- constructor(userId: string, browserWallet: IBrowserWallet, wpClient: IWalletProviderServiceClient);
35
+ constructor(userId: string, browserWallet: IBrowserWallet);
36
+ /**
37
+ * Prepares a message to present on the Browser Wallet window and requests to sign it.
38
+ * @param setup - either Keygen or Sign setup options
39
+ * @param challenge - the challenge received from the backend
40
+ *
41
+ * @public
42
+ */
28
43
  authenticate({ setup, challenge, }: {
29
44
  setup: KeygenSetupOpts | SignSetupOpts;
30
45
  challenge: string;
@@ -1 +1 @@
1
- {"version":3,"file":"authentication.d.ts","sourceRoot":"","sources":["../src/authentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iB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IAAA;IACH,wBAAwB;IACxB,IAAI,IAAA;CACL;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;IAEX,MAAM,EAAE,MAAM,CAAC;IAEf,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,EAAE,eAAe,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,WAAW,UAAU;IACzB,YAAY,CAAC,EACX,KAAK,EACL,SAAS,GACV,EAAE;QACD,KAAK,EAAE,eAAe,GAAG,aAAa,CAAC;QACvC,SAAS,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACjC;AAED;;GAEG;AACH,qBAAa,OAAQ,YAAW,UAAU;IACxC,2EAA2E;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,aAAa,EAAE,cAAc,CAAC;gBAElB,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc;IAKzD;;;;;;OAMG;IACG,YAAY,CAAC,EACjB,KAAK,EACL,SAAS,GACV,EAAE;QACD,KAAK,EAAE,eAAe,GAAG,aAAa,CAAC;QACvC,SAAS,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAQhC"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,6 @@
1
- export { KeygenSetupOpts, NetworkSigner } from './networkSigner.ts';
2
- export { createViemAccount } from './viemSigner.ts';
1
+ export { NetworkSigner, SignResponse, KeygenResponse } from './networkSigner.ts';
3
2
  export { AuthMethod, EOAAuth } from './authentication.ts';
4
- export type { TypedData } from './EOAauthentication.ts';
5
- export type { UserAuthenticatedRequest, KeygenResponse, SignSetupOpts, SignResponse } from './networkSigner.ts';
6
- export type { IWalletProviderServiceClient } from './walletProviderServiceClientInterface.ts';
3
+ export type { IBrowserWallet, TypedData } from './EOAauthentication.ts';
4
+ export type { ClientConfig, IWalletProviderServiceClient } from './walletProviderServiceClientInterface.ts';
7
5
  export { WalletProviderServiceClient } from './walletProviderServiceClient.ts';
8
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE1D,YAAY,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,YAAY,EAAE,wBAAwB,EAAE,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEhH,YAAY,EAAE,4BAA4B,EAAE,MAAM,2CAA2C,CAAC;AAC9F,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE1D,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAExE,YAAY,EAAE,YAAY,EAAE,4BAA4B,EAAE,MAAM,2CAA2C,CAAC;AAC5G,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC"}