@concordium/browser-wallet-api-helpers 2.4.0 → 2.6.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 CHANGED
@@ -51,14 +51,14 @@ declare global {
51
51
 
52
52
  ### connect
53
53
 
54
- To request a connection to the wallet from the user, the `connect` method has to be invoked. The method returns a `Promise` resolving with information related to the most recently selected account, which has whitelisted the dApp, or rejecting if the request is rejected in the wallet. If the wallet is locked, then this call prompts the user to first unlock the wallet before accepting or rejecting the connection request.
54
+ To request a connection to the wallet from the user, the `connect` method has to be invoked. The method returns a `Promise` resolving with information related to the most recently selected account, which has allowlisted the dApp, or rejecting if the request is rejected in the wallet. If the wallet is locked, then this call prompts the user to first unlock the wallet before accepting or rejecting the connection request.
55
55
 
56
56
  ```typescript
57
57
  const provider = await detectConcordiumProvider();
58
58
  const accountAddress = await provider.connect();
59
59
  ```
60
60
 
61
- N.B. In the current version, if the dApp is already whitelisted, but not by the currently selected account, the returned account will not actually be the most recently selected account, but instead the oldest account that has whitelisted the dApp.
61
+ N.B. In the current version, if the dApp is already allowlisted, but not by the currently selected account, the returned account will not actually be the most recently selected account, but instead the oldest account that has allowlisted the dApp.
62
62
 
63
63
  ### getMostRecentlySelectedAccount
64
64
 
@@ -74,13 +74,33 @@ if (accountAddress) {
74
74
  }
75
75
  ```
76
76
 
77
- N.B. In the current version, if the currently selected account has not whitelisted the dApp, the returned account will not actually be the most recently selected account, but instead the oldest account that has whitelisted the dApp.
77
+ ### requestAccounts
78
+
79
+ To request a connection to the wallet from the user, the `requestAccounts` method has to be invoked. The method returns a `Promise` resolving with the list of accounts
80
+ that the user has chosen to connect with. The list may be empty. Alternatively the promise will be rejecting if the request is rejected in the wallet. If the wallet is locked,
81
+ then this call prompts the user to first unlock the wallet before accepting or rejecting the request.
82
+
83
+ ```typescript
84
+ const provider = await detectConcordiumProvider();
85
+ const accountAddresses = await provider.requestAccounts();
86
+ ```
87
+
88
+ ### getSelectedChain
89
+
90
+ This can be invoked to get the genesis hash of the chain selected in the wallet. The method returns a `Promise`, resolving with the genesis hash (as a hex string) of the selected chain, or undefined if the wallet is locked or has not been set up by the user.
91
+
92
+ ```typescript
93
+ const provider = await detectConcordiumProvider();
94
+ const genesisHash = await provider.getSelectedChain();
95
+ ```
96
+
97
+ N.B. In the current version, if the currently selected account has not allowlisted the dApp, the returned account will not actually be the most recently selected account, but instead the oldest account that has allowlisted the dApp.
78
98
 
79
99
  ### sendTransaction
80
100
 
81
101
  To send a transaction, three arguments need to be provided: The account address for the account in the wallet that should sign the transaction, a transaction type and a corresponding payload. Invoking `sendTransaction` returns a `Promise`, which resolves with the transaction hash for the submitted transaction.
82
102
 
83
- If the wallet is locked, or you have not connected with the wallet (or previously been whitelisted) or if the user rejects signing the transaction, the `Promise` will reject.
103
+ If the wallet is locked, or you have not connected with the wallet (or previously been allowlisted) or if the user rejects signing the transaction, the `Promise` will reject.
84
104
 
85
105
  The following exemplifies how to create a simple transfer of funds from one account to another. Please note that [@concordium/web-sdk](https://github.com/Concordium/concordium-node-sdk-js/tree/main/packages/web) is used to provide the correct formats and types for the transaction payload.
86
106
 
@@ -140,12 +160,12 @@ const parameterSchema = {
140
160
 
141
161
  It is possible to sign arbitrary messages using the keys for an account stored in the wallet, by invoking the `signMessage` method. The first parameter is the account to be used for signing the message. This method returns a `Promise` resolving with a signature of the message.
142
162
 
143
- If the wallet is locked, or you have not connected with the wallet (or previously been whitelisted) or if the user rejects signing the meesage, the `Promise` will reject.
163
+ If the wallet is locked, or you have not connected with the wallet (or previously been allowlisted) or if the user rejects signing the meesage, the `Promise` will reject.
144
164
 
145
- The message should either utf8 string or an object with the following fields:
165
+ The message should be either a utf8 string or an object with the following fields:
146
166
 
147
167
  - data: A hex string representing the bytes that should be signed.
148
- - schema: A base64 string that represents a schema for the data field, and which can be used to deserialize the data into a JSON format.
168
+ - schema: A base64 string that represents a schema for the data field, and which can be used to deserialize the data into a JSON format. (For reference, the type of schema used here is the same that is used for smart contract types)
149
169
 
150
170
  The following exemplifies requesting a signature of a message, where the message is a utf8 string:
151
171
 
@@ -186,7 +206,7 @@ In this example the user will be shown:
186
206
 
187
207
  It is possible to suggest CIS-2 tokens to be added to an account's display. This method returns a `Promise` resolving with a list containing the ids of the tokens that were added.
188
208
 
189
- If the wallet is locked, or you have not connected with the wallet (or previously been whitelisted) or if the user rejects signing the meesage, the `Promise` will reject.
209
+ If the wallet is locked, or you have not connected with the wallet (or previously been allowlisted) or if the user rejects signing the meesage, the `Promise` will reject.
190
210
 
191
211
  The following exemplifies requesting tokens with id AA and BB from the contract on index 1399, and subindex 0 to the account `2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk`.
192
212
 
@@ -195,20 +215,36 @@ const provider = await detectConcordiumProvider();
195
215
  await provider.addCIS2Tokens('2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk', ['AA', 'BB'], '1399', '0');
196
216
  ```
197
217
 
198
- ### Prove ID statement
218
+ ### Add Web3Id Credentials
219
+
220
+ To add a Web3IdCredential, use the `addWeb3IdCredential` endpoint.
221
+ The credential itself and the url for the metadata must be provided. In addition, the function takes a callback function that takes a DID for the credentialHolderId as input, and which should return the randomness used to create the commitments on the values/properties in the credential, and a proof (which can be a signature on the commitments and credentialHolderId) of the credential's validity. If the callback does not return a valid proof, the credential is not added to the wallet.
222
+
223
+ Note that the id fields of the credential are omitted, and added by the wallet itself, as they require the credentialHolderId.
224
+
225
+ // TODO link to help for how to create proof and randomness
226
+
227
+ ```typescript
228
+ provider.addWeb3IdCredential(credential, metadataUrl, async (id) => {
229
+ const randomness = createRandomness(attributes); // Choose some randomness for the attribute commitments.
230
+ const proof = createCredentialProof(credential, id, randomness); // Create a proof to prove that the commitments are created by the issuer.
231
+ return { proof, randomness };
232
+ });
233
+ ```
234
+
235
+ ### Request Verifiable Presentation for web3Id statements
199
236
 
200
- It is possible to request a proof for a given ID statement on a specific account. The function takes 3 arguments. The statement to be proved, a challenge to ensure that the proof was not generated for a different context, and the account that should prove that statement.
201
- This method returns a `Promise` resolving with an object containing the proof and the credential id (field name: credential) of the credential used to prove the statement.
237
+ It is possible to request a verifiable presentation for a given set of web3Id statements. The function takes 2 arguments. A challenge to ensure that the proof was not generated for a different context, and the statements to be proven. This method returns a Promise resolving with the verifiable presentation for the statements.
202
238
 
203
- If the wallet is locked, or you have not connected with the wallet (or previously been whitelisted) or if the user rejects proving the statement, the `Promise` will reject.
239
+ If the wallet is locked, or you have not connected with the wallet (or previously been allowlisted) or if the user rejects proving the statement, the Promise will reject.
204
240
 
205
- The following exemplifies requesting a proof for a statement name myIdStatement (To see how to create a statement check out [our documentation](https://developer.concordium.software/en/mainnet/net/guides/create-proofs.html)) with a challenge of "12346789ABCD" id, for the account `2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk`.
241
+ The following exemplifies requesting a verifiable presentation for a statement named myIdStatement (To see how to create a statement check out our documentation) with a challenge of "12346789ABCD".
206
242
 
207
243
  ```typescript
208
244
  const statement = myIdStatement;
209
245
  const challenge = '12346789ABCD';
210
246
  const provider = await detectConcordiumProvider();
211
- await provider.requestIdProof('2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk', ['AA', 'BB'], '1399', '0');
247
+ const verifiablePresentation = await provider.requestVerifiablePresentation(challenge, statement);
212
248
  ```
213
249
 
214
250
  ## Events
@@ -250,7 +286,7 @@ provider.connect().then((accountAddress) => (selectedAccountAddress = accountAdd
250
286
  The wallet API exposes access to a JSON-RPC client. This allows a dApp to communicate with the same node as the wallet is connected to, and enables dApps to access the JSON-RPC interface without being connected to a separate server itself. The client is accessed as shown in the example below.
251
287
  The dApp does not need to recreate the client again when the wallet changes node or network, the client will always use the wallet's current connected JSON-RPC server.
252
288
 
253
- If you have not connected with the wallet (or previously been whitelisted), the commands will not be executed and the method will throw an error.
289
+ If you have not connected with the wallet (or previously been allowlisted), the commands will not be executed and the method will throw an error.
254
290
 
255
291
  ```typescript
256
292
  const provider = await detectConcordiumProvider();
package/lib/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare type LaxStringEnumValue<E extends string> = `${E}`;
2
+ export declare type LaxNumberEnumValue<E extends number> = `${E}` extends `${infer T extends number}` ? T : never;
package/lib/util.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,33 @@
1
- import type { AccountTransactionPayload, AccountTransactionSignature, AccountTransactionType, InitContractPayload, JsonRpcClient, SchemaVersion, UpdateContractPayload, IdStatement, IdProofOutput } from '@concordium/web-sdk';
1
+ import type { AccountTransactionPayload, AccountTransactionSignature, AccountTransactionType, InitContractPayload, JsonRpcClient, SchemaVersion, UpdateContractPayload, IdStatement, IdProofOutput, ConcordiumGRPCClient, CredentialStatements, VerifiablePresentation, CredentialSubject, HexString } from '@concordium/web-sdk';
2
+ import { LaxNumberEnumValue, LaxStringEnumValue } from './util';
3
+ export interface MetadataUrl {
4
+ url: string;
5
+ hash?: string;
6
+ }
7
+ interface CredentialSchema {
8
+ id: string;
9
+ type: string;
10
+ }
11
+ /**
12
+ * The expected form of a Web3IdCredential, with the id fields omitted.
13
+ */
14
+ export interface APIVerifiableCredential {
15
+ $schema: string;
16
+ type: string[];
17
+ issuer: string;
18
+ issuanceDate: string;
19
+ credentialSubject: Omit<CredentialSubject, 'id'>;
20
+ credentialSchema: CredentialSchema;
21
+ }
22
+ /**
23
+ * Expected format for the proof that the Web3IdCredential's attribute commitments are valid
24
+ */
25
+ export interface CredentialProof {
26
+ proofPurpose: 'assertionMethod';
27
+ proofValue: HexString;
28
+ type: 'Ed25519Signature2020';
29
+ verificationMethod: string;
30
+ }
2
31
  export declare type SendTransactionPayload = Exclude<AccountTransactionPayload, UpdateContractPayload | InitContractPayload> | Omit<UpdateContractPayload, 'message'> | Omit<InitContractPayload, 'param'>;
3
32
  export declare type SmartContractParameters = {
4
33
  [key: string]: SmartContractParameters;
@@ -22,7 +51,7 @@ export declare enum SchemaType {
22
51
  Parameter = "parameter"
23
52
  }
24
53
  export declare type SchemaWithContext = {
25
- type: SchemaType;
54
+ type: LaxStringEnumValue<SchemaType>;
26
55
  value: string;
27
56
  };
28
57
  declare type EventListener<Args extends any[]> = (...args: Args) => void;
@@ -44,7 +73,7 @@ interface MainWalletApi {
44
73
  * @param schema schema used for the initContract and updateContract transactions to serialize the parameters. Should be base64 encoded.
45
74
  * @param schemaVersion version of the schema provided. Must be supplied for schemas that use version 0 or 1, as they don't have the version embedded.
46
75
  */
47
- sendTransaction(accountAddress: string, type: AccountTransactionType.Update | AccountTransactionType.InitContract, payload: SendTransactionPayload, parameters: SmartContractParameters, schema: string | SchemaWithContext, schemaVersion?: SchemaVersion): Promise<string>;
76
+ sendTransaction(accountAddress: string, type: LaxNumberEnumValue<AccountTransactionType.Update | AccountTransactionType.InitContract>, payload: SendTransactionPayload, parameters: SmartContractParameters, schema: string | SchemaWithContext, schemaVersion?: SchemaVersion): Promise<string>;
48
77
  /**
49
78
  * Sends a transaction to the Concordium Wallet and awaits the users action. Note that a header is not sent, and will be constructed by the wallet itself.
50
79
  * Note that if the user rejects signing the transaction, this will throw an error.
@@ -52,7 +81,7 @@ interface MainWalletApi {
52
81
  * @param type the type of transaction that is to be signed and sent.
53
82
  * @param payload the payload of the transaction to be signed and sent. Note that for smart contract transactions, the payload should not contain the parameters, those should instead be provided in the subsequent argument instead.
54
83
  */
55
- sendTransaction(accountAddress: string, type: AccountTransactionType, payload: SendTransactionPayload): Promise<string>;
84
+ sendTransaction(accountAddress: string, type: LaxNumberEnumValue<AccountTransactionType>, payload: SendTransactionPayload): Promise<string>;
56
85
  /**
57
86
  * Sends a message to the Concordium Wallet and awaits the users action. If the user signs the message, this will resolve to the signature.
58
87
  * Note that if the user rejects signing the message, this will throw an error.
@@ -63,14 +92,35 @@ interface MainWalletApi {
63
92
  /**
64
93
  * Requests a connection to the Concordium wallet, prompting the user to either accept or reject the request.
65
94
  * If a connection has already been accepted for the url once the returned promise will resolve without prompting the user.
95
+ * @deprecated use {@link requestAccounts} instead
66
96
  */
67
97
  connect(): Promise<string | undefined>;
98
+ /**
99
+ * Requests a connection to the Concordium wallet, prompting the user to either accept or reject the request. The
100
+ * user will be prompted to select which accounts should be connected. The list of connected accounts is returned
101
+ * to the caller. If a connection has already been accepted previously, then the returned promise will resolve
102
+ * with the list of connected accounts. Note that the list of accounts may be empty.
103
+ */
104
+ requestAccounts(): Promise<string[] | undefined>;
68
105
  /**
69
106
  * Returns some connected account, prioritizing the most recently selected. Resolves with account address or undefined if there are no connected account.
70
107
  */
71
108
  getMostRecentlySelectedAccount(): Promise<string | undefined>;
72
109
  removeAllListeners(event?: EventType | string | undefined): this;
110
+ /**
111
+ * @deprecated use { @link getGrpcClient} instead
112
+ */
73
113
  getJsonRpcClient(): JsonRpcClient;
114
+ /**
115
+ * Returns a gRPC client that is connected to the node that the wallet is.
116
+ * Note that any calls will throw an error, if the site is not connected with the wallet.
117
+ */
118
+ getGrpcClient(): ConcordiumGRPCClient;
119
+ /**
120
+ * Returns the genesis hash of the currently selected chain in the wallet.
121
+ * Returns undefined if the wallet is either locked or not set up by the user.
122
+ */
123
+ getSelectedChain(): Promise<string | undefined>;
74
124
  /**
75
125
  * Request that the user adds the specified tokens for a given contract to the wallet.
76
126
  * Returns which of the given tokens the user accepted to add the tokens into the wallet.
@@ -84,12 +134,32 @@ interface MainWalletApi {
84
134
  addCIS2Tokens(accountAddress: string, tokenIds: string[], contractIndex: bigint, contractSubindex?: bigint): Promise<string[]>;
85
135
  /**
86
136
  * Request that the user provides a proof for the given statement.
137
+ * @deprecated Please use { @link requestVerifiablePresentation} instead.
87
138
  * @param accountAddress the address of the account that should prove the statement.
88
139
  * @param statement the id statement that should be proven.
89
140
  * @param challenge bytes chosen by the verifier. Should be HEX encoded.
90
141
  * @returns The id proof and the id of the credential used to prove it.
91
142
  */
92
143
  requestIdProof(accountAddress: string, statement: IdStatement, challenge: string): Promise<IdProofOutput>;
144
+ /**
145
+ * Requests that a web3IdCredential is added to the wallet.
146
+ * Note that this will throw an error if the dApp is not allowlisted, locked, or if the user rejects adding the credential.
147
+ * @param credential the web3IdCredential that should be added to the wallet
148
+ * @param metadataUrl the url where the metadata, to display the credential, is located.
149
+ * @param createSignature a callback function, which takes a DID identifier for the credentialHolderId as input and must return the randomness used for the commitment of the values and signature on the commitments and credentialId.
150
+ * @returns the DID identifier for the credentialHolderId, i.e. the publicKey that will be associated with the credential.
151
+ */
152
+ addWeb3IdCredential(credential: APIVerifiableCredential, metadataUrl: MetadataUrl, generateProofAndRandomness: (credentialHolderIdDID: string) => Promise<{
153
+ randomness: Record<string, string>;
154
+ proof: CredentialProof;
155
+ }>): Promise<string>;
156
+ /**
157
+ * Request that the user provides a proof for the given statements.
158
+ * @param challenge bytes chosen by the verifier. Should be HEX encoded.
159
+ * @param statements the web3Id statements that should be proven. The promise rejects if the array of statements is empty.
160
+ * @returns The presentation for the statements.
161
+ */
162
+ requestVerifiablePresentation(challenge: string, statements: CredentialStatements): Promise<VerifiablePresentation>;
93
163
  }
94
164
  export declare type WalletApi = MainWalletApi & EventListeners;
95
165
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@concordium/browser-wallet-api-helpers",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "license": "Apache-2.0",
5
5
  "packageManager": "yarn@3.2.0",
6
6
  "main": "lib/index.js",
@@ -19,7 +19,7 @@
19
19
  "url": "https://concordium.com"
20
20
  },
21
21
  "dependencies": {
22
- "@concordium/web-sdk": "^3.4.0"
22
+ "@concordium/web-sdk": "^6.1.1"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@babel/core": "^7.17.10",
@@ -31,6 +31,7 @@
31
31
  "webpack-cli": "^4.9.2"
32
32
  },
33
33
  "scripts": {
34
- "build": "tsc && webpack"
34
+ "build": "tsc && webpack",
35
+ "build:api-helpers": "yarn build"
35
36
  }
36
37
  }