unwallet-client-sdk 0.2.0 → 0.4.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
@@ -1,6 +1,6 @@
1
1
  # unWallet client-side SDK
2
2
 
3
- :warning: Works fine only on browsers
3
+ :warning: This SDK works fine only on browsers.
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,11 +8,7 @@
8
8
  $ npm install unwallet-client-sdk
9
9
  ```
10
10
 
11
- ## Example
12
-
13
- To execute the sample codes below, you need to create an application with unWallet Enterprise in advance.
14
-
15
- ### Initialization
11
+ ## Initialization
16
12
 
17
13
  ```js
18
14
  import { UnWallet } from "unwallet-client-sdk";
@@ -22,31 +18,7 @@ const unWallet = await UnWallet.init({
22
18
  });
23
19
  ```
24
20
 
25
- ### Requesting a user's ID token in accordance with OpenID Connect (response_type: id_token)
26
-
27
- ```js
28
- unWallet.authorize({
29
- redirectURL: "http://your.app.com/callback",
30
- nonce: "pdITKAtep0pfPOrUXdzjqW6gKvXezurJ", // arbitrary string to prevent replay attacks
31
- });
32
- ```
33
-
34
- ### Requesting a signature for a message
35
-
36
- ```js
37
- const sig = await unWallet.sign({
38
- message: "ARBITRARY_MESSAGE",
39
- });
40
- ```
41
-
42
- ### Requesting a signature for a token transfer transaction
43
-
44
- ```js
45
- const metaTx = await unWallet.signTokenTransfer({
46
- id: 1, // token ID
47
- to: "0x0000000000000000000000000000000000000000", // destination address
48
- amount: 1, // token amount
49
- });
50
- ```
21
+ ## Functions
51
22
 
52
- To execute the transaction, call [POST /metaTransactions of unWallet Enterprise API](https://developers.ent.unwallet.world/ja/latest/unwallet-ent-api.html#post-metatransactions).
23
+ - [For all developers](docs/functions-1.md)
24
+ - [For web3 developers](docs/functions-2.md)
package/dist/types.d.ts CHANGED
@@ -7,6 +7,10 @@ export interface UnWalletConfig {
7
7
  authURL: string;
8
8
  wsAPIURL: string;
9
9
  }
10
+ export interface DigestAndSignature {
11
+ digest: string;
12
+ signature: string;
13
+ }
10
14
  export interface MetaTransaction {
11
15
  executor: string;
12
16
  data: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB"}
@@ -1,4 +1,4 @@
1
- import { Config, UnWalletConfig, MetaTransaction } from "./types";
1
+ import { Config, UnWalletConfig, DigestAndSignature, MetaTransaction } from "./types";
2
2
  export declare class UnWallet {
3
3
  private config;
4
4
  private unWalletConfig;
@@ -16,7 +16,12 @@ export declare class UnWallet {
16
16
  }): void;
17
17
  sign(args: {
18
18
  message: string;
19
- }): Promise<string>;
19
+ }): Promise<DigestAndSignature>;
20
+ signTransaction(args: {
21
+ to: string;
22
+ value?: string;
23
+ data?: string;
24
+ }): Promise<MetaTransaction>;
20
25
  signTokenTransfer(args: {
21
26
  id: number;
22
27
  to: string;
@@ -1 +1 @@
1
- {"version":3,"file":"unwallet.d.ts","sourceRoot":"","sources":["../src/unwallet.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAElE,qBAAa,QAAQ;IACnB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,cAAc,CAAiB;IAEvC,OAAO,CAAC,EAAE,CAAY;IACtB,OAAO,CAAC,YAAY,CAAS;IAE7B,OAAO,CAAC,OAAO,CAAwC;IACvD,OAAO,CAAC,MAAM,CAAwC;gBAE1C,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,EAAE,EAAE,SAAS;IAUzE,OAAO,CAAC,eAAe;WAKT,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAiC9C,SAAS,CAAC,IAAI,EAAE;QACrB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,MAAM,CAAC;QACpB,KAAK,EAAE,MAAM,CAAC;KACf,GAAG,IAAI;IAgBD,IAAI,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAahD,iBAAiB,CAAC,IAAI,EAAE;QAC7B,EAAE,EAAE,MAAM,CAAC;QACX,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;KAChB,GAAG,OAAO,CAAC,eAAe,CAAC;IAerB,kBAAkB,CAAC,IAAI,EAAE;QAC9B,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,MAAM,CAAC;IAgBnB,OAAO,CAAC,eAAe;IAMvB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AE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,MAAM,CAAC;IAiBnB,OAAO,CAAC,eAAe;IAMvB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,eAAe;IAgCvB,OAAO,CAAC,UAAU;CAYnB"}
package/dist/unwallet.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UnWallet = void 0;
4
+ const ethers_1 = require("ethers");
4
5
  const configs_1 = require("./configs");
5
6
  class UnWallet {
6
7
  constructor(config, unWalletConfig, ws) {
@@ -35,7 +36,7 @@ class UnWallet {
35
36
  ws.onmessage = (event) => {
36
37
  const msg = JSON.parse(event.data);
37
38
  if (msg.type === "connectionID") {
38
- unWallet.connectionID = msg.data.value;
39
+ unWallet.connectionID = msg.value;
39
40
  resolve(unWallet);
40
41
  return;
41
42
  }
@@ -60,7 +61,12 @@ class UnWallet {
60
61
  }
61
62
  sign(args) {
62
63
  return new Promise((resolve, reject) => {
63
- this.resolve = resolve;
64
+ this.resolve = (sig) => {
65
+ resolve({
66
+ digest: ethers_1.ethers.utils.sha256(ethers_1.ethers.utils.toUtf8Bytes(args.message)),
67
+ signature: sig,
68
+ });
69
+ };
64
70
  this.reject = reject;
65
71
  const url = new URL(`${this.unWalletConfig.baseURL}/x/sign`);
66
72
  url.searchParams.set("connectionID", this.connectionID);
@@ -69,6 +75,17 @@ class UnWallet {
69
75
  this.openWindow(url);
70
76
  });
71
77
  }
78
+ signTransaction(args) {
79
+ return new Promise((resolve, reject) => {
80
+ this.resolve = resolve;
81
+ this.reject = reject;
82
+ const url = new URL(`${this.unWalletConfig.baseURL}/x/signTransaction`);
83
+ url.searchParams.set("connectionID", this.connectionID);
84
+ url.searchParams.set("clientID", this.config.clientID);
85
+ url.searchParams.set("transaction", JSON.stringify(args));
86
+ this.openWindow(url);
87
+ });
88
+ }
72
89
  signTokenTransfer(args) {
73
90
  return new Promise((resolve, reject) => {
74
91
  this.resolve = resolve;
@@ -105,17 +122,27 @@ class UnWallet {
105
122
  handleWSMessage(msg) {
106
123
  switch (msg.type) {
107
124
  case "signature":
125
+ this.resolve(msg.value);
126
+ break;
108
127
  case "metaTransaction":
128
+ this.resolve(msg.value);
129
+ break;
109
130
  case "presentation":
110
- if (msg.data.value === null) {
111
- this.reject("canceled");
112
- }
113
- else {
114
- this.resolve(msg.data.value);
131
+ this.resolve(msg.value);
132
+ break;
133
+ case "error":
134
+ switch (msg.value) {
135
+ case "rejected":
136
+ this.reject("canceled");
137
+ break;
138
+ default:
139
+ throw new Error(msg.value);
115
140
  }
116
- this.initPromiseArgs();
117
141
  break;
142
+ default:
143
+ throw new Error(`unknown message type: ${msg.type}`);
118
144
  }
145
+ this.initPromiseArgs();
119
146
  }
120
147
  openWindow(url) {
121
148
  const width = screen.width / 2;
@@ -0,0 +1,108 @@
1
+ # Functions (for all developers)
2
+
3
+ > **Note**\
4
+ > To execute the sample codes below, you need to create an application with unWallet Enterprise in advance.
5
+
6
+ ## authorize
7
+
8
+ `authorize` requests a user's ID token in accordance with OpenID Connect (response_type: id_token).
9
+
10
+ ```js
11
+ unWallet.authorize({
12
+ redirectURL: "http://your.app.com/callback",
13
+ nonce: "pdITKAtep0pfPOrUXdzjqW6gKvXezurJ", // arbitrary string to prevent replay attacks
14
+ });
15
+ ```
16
+
17
+ ## sign
18
+
19
+ `sign` requests a signature for a message.
20
+
21
+ ```js
22
+ const sig = await unWallet.sign({
23
+ message: "ARBITRARY_MESSAGE",
24
+ });
25
+ ```
26
+
27
+ ### Example return value
28
+
29
+ ```json
30
+ {
31
+ "digest": "0xc71ed5b0509d8da72abc9527ed72530743822e8d9b33f9695b42e20ece78c09b",
32
+ "signature": "0xd94388b5d51395c46b00f6197de318ba275cd7f9c5e4dccf2059373a4b41b3975403852e2587f262375e6d2b3318380dcd95535ecebec7e8c7ebbbefdcf22a371b"
33
+ }
34
+ ```
35
+
36
+ ### How to verify a signature
37
+
38
+ See also [ERC1271](https://eips.ethereum.org/EIPS/eip-1271).
39
+
40
+ ```js
41
+ const ethers = require("ethers");
42
+
43
+ (async () => {
44
+ const contract = new ethers.Contract(
45
+ "<CONTRACT_WALLET_ADDRESS>",
46
+ [
47
+ {
48
+ inputs: [
49
+ {
50
+ internalType: "bytes32",
51
+ name: "hash",
52
+ type: "bytes32",
53
+ },
54
+ {
55
+ internalType: "bytes",
56
+ name: "signature",
57
+ type: "bytes",
58
+ },
59
+ ],
60
+ name: "isValidSignature",
61
+ outputs: [
62
+ {
63
+ internalType: "bytes4",
64
+ name: "",
65
+ type: "bytes4",
66
+ },
67
+ ],
68
+ stateMutability: "view",
69
+ type: "function",
70
+ },
71
+ ],
72
+ new ethers.providers.JsonRpcProvider("<YOUR_RPC_URL>")
73
+ );
74
+
75
+ try {
76
+ await contract.isValidSignature("<DIGEST>", "<SIGNATURE>");
77
+ } catch (e) {
78
+ console.log("invalid");
79
+ return;
80
+ }
81
+
82
+ console.log("valid");
83
+ })();
84
+ ```
85
+
86
+ ## signTokenTransfer
87
+
88
+ `signTokenTransfer` requests a signature for a token transfer transaction.
89
+
90
+ ```js
91
+ const metaTx = await unWallet.signTokenTransfer({
92
+ id: 101, // token ID
93
+ to: "0xB481148EB6A5f6b5b9Cc10cb0C8304B9B179A8e6", // destination address
94
+ amount: 1, // token amount
95
+ });
96
+ ```
97
+
98
+ ### Example return value
99
+
100
+ ```json
101
+ {
102
+ "executor": "0x3ADBDCBa56d70Fc15Dcbe98901432cC07B2aAaeF",
103
+ "data": "0x...",
104
+ "signature": "0x19eb83842bc2d2c55567d4da63981ae9d4ce76ec567b591f18e18f4e030c4389331ba3ce0f1549331cb51710881320982b7b7a3632a7d81ca214690ecf3267c51c"
105
+ }
106
+ ```
107
+
108
+ To execute the transaction, call [POST /metaTransactions of unWallet Enterprise API](https://developers.ent.unwallet.world/ja/latest/unwallet-ent-api.html#post-metatransactions).
@@ -0,0 +1,32 @@
1
+ # Functions (for web3 developers)
2
+
3
+ Use the following functions only if you want to work with your own contracts.
4
+
5
+ > **Note**\
6
+ > To execute the sample codes below, you need to create an application with unWallet Enterprise in advance.
7
+
8
+ ## signTransaction
9
+
10
+ `signTransaction` requests a signature for a transaction.
11
+
12
+ ```js
13
+ const metaTx = await unWallet.signTransaction({
14
+ to: "0xB481148EB6A5f6b5b9Cc10cb0C8304B9B179A8e6", // the address the transaction is directed to
15
+ value: "0x0", // the MATIC value sent with this transaction (optional)
16
+ data: "0x...", // the hash of the invoked method signature and encoded parameters (optional)
17
+ });
18
+ ```
19
+
20
+ :warning: You cannot specify parameters related to gas here because the gas consumed by the meta transaction is paid by the provider wallet.
21
+
22
+ ### Example return value
23
+
24
+ ```json
25
+ {
26
+ "executor": "0x3ADBDCBa56d70Fc15Dcbe98901432cC07B2aAaeF",
27
+ "data": "0x...",
28
+ "signature": "0x19eb83842bc2d2c55567d4da63981ae9d4ce76ec567b591f18e18f4e030c4389331ba3ce0f1549331cb51710881320982b7b7a3632a7d81ca214690ecf3267c51c"
29
+ }
30
+ ```
31
+
32
+ To execute the transaction, call [POST /metaTransactions of unWallet Enterprise API](https://developers.ent.unwallet.world/ja/latest/unwallet-ent-api.html#post-metatransactions).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unwallet-client-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.4.1",
4
4
  "description": "unWallet client-side SDK",
5
5
  "license": "MIT",
6
6
  "author": "SIVIRA",
@@ -12,6 +12,9 @@
12
12
  "scripts": {
13
13
  "build": "tsc"
14
14
  },
15
+ "dependencies": {
16
+ "ethers": "^5.6.9"
17
+ },
15
18
  "devDependencies": {
16
19
  "typescript": "^4.3.4"
17
20
  }
package/src/types.ts CHANGED
@@ -9,6 +9,11 @@ export interface UnWalletConfig {
9
9
  wsAPIURL: string;
10
10
  }
11
11
 
12
+ export interface DigestAndSignature {
13
+ digest: string;
14
+ signature: string;
15
+ }
16
+
12
17
  export interface MetaTransaction {
13
18
  executor: string;
14
19
  data: string;
package/src/unwallet.ts CHANGED
@@ -1,5 +1,12 @@
1
+ import { ethers } from "ethers";
2
+
1
3
  import { unWalletConfigs } from "./configs";
2
- import { Config, UnWalletConfig, MetaTransaction } from "./types";
4
+ import {
5
+ Config,
6
+ UnWalletConfig,
7
+ DigestAndSignature,
8
+ MetaTransaction,
9
+ } from "./types";
3
10
 
4
11
  export class UnWallet {
5
12
  private config: Config;
@@ -47,7 +54,7 @@ export class UnWallet {
47
54
  ws.onmessage = (event) => {
48
55
  const msg = JSON.parse(event.data);
49
56
  if (msg.type === "connectionID") {
50
- unWallet.connectionID = msg.data.value;
57
+ unWallet.connectionID = msg.value;
51
58
  resolve(unWallet);
52
59
  return;
53
60
  }
@@ -79,15 +86,39 @@ export class UnWallet {
79
86
  location.assign(url.toString());
80
87
  }
81
88
 
82
- public sign(args: { message: string }): Promise<string> {
89
+ public sign(args: { message: string }): Promise<DigestAndSignature> {
83
90
  return new Promise((resolve, reject) => {
84
- this.resolve = resolve;
91
+ this.resolve = (sig: string) => {
92
+ resolve({
93
+ digest: ethers.utils.sha256(ethers.utils.toUtf8Bytes(args.message)),
94
+ signature: sig,
95
+ });
96
+ };
85
97
  this.reject = reject;
86
98
 
87
99
  const url = new URL(`${this.unWalletConfig.baseURL}/x/sign`);
88
100
  url.searchParams.set("connectionID", this.connectionID);
89
101
  url.searchParams.set("clientID", this.config.clientID);
90
102
  url.searchParams.set("message", args.message);
103
+
104
+ this.openWindow(url);
105
+ });
106
+ }
107
+
108
+ public signTransaction(args: {
109
+ to: string;
110
+ value?: string;
111
+ data?: string;
112
+ }): Promise<MetaTransaction> {
113
+ return new Promise((resolve, reject) => {
114
+ this.resolve = resolve;
115
+ this.reject = reject;
116
+
117
+ const url = new URL(`${this.unWalletConfig.baseURL}/x/signTransaction`);
118
+ url.searchParams.set("connectionID", this.connectionID);
119
+ url.searchParams.set("clientID", this.config.clientID);
120
+ url.searchParams.set("transaction", JSON.stringify(args));
121
+
91
122
  this.openWindow(url);
92
123
  });
93
124
  }
@@ -107,6 +138,7 @@ export class UnWallet {
107
138
  url.searchParams.set("id", args.id.toString());
108
139
  url.searchParams.set("to", args.to);
109
140
  url.searchParams.set("amount", args.amount.toString());
141
+
110
142
  this.openWindow(url);
111
143
  });
112
144
  }
@@ -126,6 +158,7 @@ export class UnWallet {
126
158
  url.searchParams.set("clientID", this.config.clientID);
127
159
  url.searchParams.set("credential", args.credential);
128
160
  url.searchParams.set("challenge", args.challenge);
161
+
129
162
  this.openWindow(url);
130
163
  });
131
164
  }
@@ -143,16 +176,33 @@ export class UnWallet {
143
176
  private handleWSMessage(msg: any): void {
144
177
  switch (msg.type) {
145
178
  case "signature":
179
+ this.resolve!(msg.value);
180
+ break;
181
+
146
182
  case "metaTransaction":
183
+ this.resolve!(msg.value);
184
+ break;
185
+
147
186
  case "presentation":
148
- if (msg.data.value === null) {
149
- this.reject!("canceled");
150
- } else {
151
- this.resolve!(msg.data.value);
187
+ this.resolve!(msg.value);
188
+ break;
189
+
190
+ case "error":
191
+ switch (msg.value) {
192
+ case "rejected":
193
+ this.reject!("canceled");
194
+ break;
195
+
196
+ default:
197
+ throw new Error(msg.value);
152
198
  }
153
- this.initPromiseArgs();
154
199
  break;
200
+
201
+ default:
202
+ throw new Error(`unknown message type: ${msg.type}`);
155
203
  }
204
+
205
+ this.initPromiseArgs();
156
206
  }
157
207
 
158
208
  private openWindow(url: URL): void {