@concordium/browser-wallet-api-helpers 2.5.0 → 2.7.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,6 +74,17 @@ if (accountAddress) {
74
74
  }
75
75
  ```
76
76
 
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
+
77
88
  ### getSelectedChain
78
89
 
79
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.
@@ -83,13 +94,13 @@ const provider = await detectConcordiumProvider();
83
94
  const genesisHash = await provider.getSelectedChain();
84
95
  ```
85
96
 
86
- 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.
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.
87
98
 
88
99
  ### sendTransaction
89
100
 
90
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.
91
102
 
92
- 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.
93
104
 
94
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.
95
106
 
@@ -149,7 +160,7 @@ const parameterSchema = {
149
160
 
150
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.
151
162
 
152
- 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.
153
164
 
154
165
  The message should be either a utf8 string or an object with the following fields:
155
166
 
@@ -195,7 +206,7 @@ In this example the user will be shown:
195
206
 
196
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.
197
208
 
198
- 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.
199
210
 
200
211
  The following exemplifies requesting tokens with id AA and BB from the contract on index 1399, and subindex 0 to the account `2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk`.
201
212
 
@@ -204,20 +215,36 @@ const provider = await detectConcordiumProvider();
204
215
  await provider.addCIS2Tokens('2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk', ['AA', 'BB'], '1399', '0');
205
216
  ```
206
217
 
207
- ### 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
208
236
 
209
- 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.
210
- 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.
211
238
 
212
- 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.
213
240
 
214
- 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".
215
242
 
216
243
  ```typescript
217
244
  const statement = myIdStatement;
218
245
  const challenge = '12346789ABCD';
219
246
  const provider = await detectConcordiumProvider();
220
- await provider.requestIdProof('2za2yAXbFiaB151oYqTteZfqiBzibHXizwjNbpdU8hodq9SfEk', ['AA', 'BB'], '1399', '0');
247
+ const verifiablePresentation = await provider.requestVerifiablePresentation(challenge, statement);
221
248
  ```
222
249
 
223
250
  ## Events
@@ -259,7 +286,7 @@ provider.connect().then((accountAddress) => (selectedAccountAddress = accountAdd
259
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.
260
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.
261
288
 
262
- 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.
263
290
 
264
291
  ```typescript
265
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, ConcordiumGRPCClient } 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,8 +92,16 @@ 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
  */
@@ -97,12 +134,32 @@ interface MainWalletApi {
97
134
  addCIS2Tokens(accountAddress: string, tokenIds: string[], contractIndex: bigint, contractSubindex?: bigint): Promise<string[]>;
98
135
  /**
99
136
  * Request that the user provides a proof for the given statement.
137
+ * @deprecated Please use { @link requestVerifiablePresentation} instead.
100
138
  * @param accountAddress the address of the account that should prove the statement.
101
139
  * @param statement the id statement that should be proven.
102
140
  * @param challenge bytes chosen by the verifier. Should be HEX encoded.
103
141
  * @returns The id proof and the id of the credential used to prove it.
104
142
  */
105
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>;
106
163
  }
107
164
  export declare type WalletApi = MainWalletApi & EventListeners;
108
165
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@concordium/browser-wallet-api-helpers",
3
- "version": "2.5.0",
3
+ "version": "2.7.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.2"
22
+ "@concordium/web-sdk": "^6.3.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@babel/core": "^7.17.10",