@ocap/wallet 1.25.5 → 1.26.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
@@ -34,9 +34,9 @@ const type = WalletType({
34
34
 
35
35
  const wallet = fromSecretKey(sk, type);
36
36
  const message = 'data to sign';
37
- const signature = wallet.sign(message);
37
+ const signature = await wallet.sign(message);
38
38
  assert.equal(signature, sig, 'signature should match');
39
- assert.ok(wallet.verify(message, signature), 'signature should be verified');
39
+ assert.ok(await wallet.verify(message, signature), 'signature should be verified');
40
40
 
41
41
  const wallet2 = fromJSON(wallet.toJSON());
42
42
  // Do something with wallet 2
package/esm/index.d.ts CHANGED
@@ -23,17 +23,19 @@ export interface WalletObject<T extends BytesType = string> {
23
23
  hash(data: BytesType, round?: number, encoding?: 'buffer'): Buffer;
24
24
  hash(data: BytesType, round?: number, encoding?: 'Uint8Array'): Uint8Array;
25
25
  hash(data: BytesType, round?: number, encoding?: EncodingType): BytesType;
26
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): string;
27
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): string;
28
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): string;
29
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): string;
30
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Buffer;
31
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Uint8Array;
32
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): BytesType;
26
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): Promise<string>;
27
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): Promise<string>;
28
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): Promise<string>;
29
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): Promise<string>;
30
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Promise<Buffer>;
31
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Promise<Uint8Array>;
32
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): Promise<BytesType>;
33
33
  verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
34
34
  ethHash(data: string): string;
35
- ethSign(data: string, hashBeforeSign?: boolean): string;
36
- ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): boolean;
35
+ ethSign(data: string, hashBeforeSign?: boolean): Promise<string>;
36
+ ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): Promise<boolean>;
37
+ signETH(data: string, hashBeforeSign?: boolean): Promise<string>;
38
+ signJWT(payload?: any, doSign?: boolean, version?: string): Promise<string>;
37
39
  toJSON(): SerializedWallet;
38
40
  /**
39
41
  * @deprecated ES6: use `wallet.address` instead
package/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { toHex } from '@ocap/util';
2
2
  import { getSigner, getHasher } from '@ocap/mcrypto';
3
3
  import { toAddress, fromPublicKey as DIDFromPublicKey, toTypeInfo, DidType, } from '@arcblock/did';
4
+ import * as JWT from '@arcblock/jwt';
4
5
  export const WalletType = DidType;
5
6
  /**
6
7
  * Generate an wallet instance that can be used to sign a message or verify a signature
@@ -19,7 +20,8 @@ export function Wallet(keyPair, t = 'default') {
19
20
  return hasher(data, round, encoding);
20
21
  },
21
22
  // @ts-ignore
22
- sign(data, hashBeforeSign = true, encoding = 'hex') {
23
+ // eslint-disable-next-line require-await
24
+ async sign(data, hashBeforeSign = true, encoding = 'hex') {
23
25
  if (!keyPair.sk) {
24
26
  throw new Error('Cannot sign data without a secretKey');
25
27
  }
@@ -43,7 +45,8 @@ export function Wallet(keyPair, t = 'default') {
43
45
  }
44
46
  return signer.ethHash(data);
45
47
  },
46
- ethSign(data, hashBeforeSign = true) {
48
+ // eslint-disable-next-line require-await
49
+ async signETH(data, hashBeforeSign = true) {
47
50
  if (!keyPair.sk) {
48
51
  throw new Error('Cannot sign data without a secretKey');
49
52
  }
@@ -55,13 +58,24 @@ export function Wallet(keyPair, t = 'default') {
55
58
  }
56
59
  return signer.ethSign(data, toHex(keyPair.sk));
57
60
  },
58
- ethVerify(data, signature, hashBeforeVerify = true) {
61
+ ethSign(data, hashBeforeSign = true) {
62
+ return this.signETH(data, hashBeforeSign);
63
+ },
64
+ // eslint-disable-next-line require-await
65
+ async ethVerify(data, signature, hashBeforeVerify = true) {
59
66
  if (typeof signer.ethHash !== 'function') {
60
67
  throw new Error('ethVerify is not supported by signer');
61
68
  }
62
69
  const hash = hashBeforeVerify ? signer.ethHash(data) : data;
63
70
  return signer.ethRecover(hash, signature) === this.address;
64
71
  },
72
+ // eslint-disable-next-line require-await
73
+ async signJWT(payload = {}, doSign = true, version = '1.0.0') {
74
+ if (!keyPair.sk) {
75
+ throw new Error('Cannot sign JWT without a secretKey');
76
+ }
77
+ return JWT.sign(this.address, keyPair.sk, payload, doSign, version);
78
+ },
65
79
  // deprecated
66
80
  toAddress() {
67
81
  return keyPair.pk ? DIDFromPublicKey(keyPair.pk, type) : keyPair.address;
package/lib/index.d.ts CHANGED
@@ -23,17 +23,19 @@ export interface WalletObject<T extends BytesType = string> {
23
23
  hash(data: BytesType, round?: number, encoding?: 'buffer'): Buffer;
24
24
  hash(data: BytesType, round?: number, encoding?: 'Uint8Array'): Uint8Array;
25
25
  hash(data: BytesType, round?: number, encoding?: EncodingType): BytesType;
26
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): string;
27
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): string;
28
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): string;
29
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): string;
30
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Buffer;
31
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Uint8Array;
32
- sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): BytesType;
26
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): Promise<string>;
27
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): Promise<string>;
28
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): Promise<string>;
29
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): Promise<string>;
30
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Promise<Buffer>;
31
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Promise<Uint8Array>;
32
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): Promise<BytesType>;
33
33
  verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
34
34
  ethHash(data: string): string;
35
- ethSign(data: string, hashBeforeSign?: boolean): string;
36
- ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): boolean;
35
+ ethSign(data: string, hashBeforeSign?: boolean): Promise<string>;
36
+ ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): Promise<boolean>;
37
+ signETH(data: string, hashBeforeSign?: boolean): Promise<string>;
38
+ signJWT(payload?: any, doSign?: boolean, version?: string): Promise<string>;
37
39
  toJSON(): SerializedWallet;
38
40
  /**
39
41
  * @deprecated ES6: use `wallet.address` instead
package/lib/index.js CHANGED
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.WalletType = void 0;
4
27
  exports.Wallet = Wallet;
@@ -11,6 +34,7 @@ exports.isValid = isValid;
11
34
  const util_1 = require("@ocap/util");
12
35
  const mcrypto_1 = require("@ocap/mcrypto");
13
36
  const did_1 = require("@arcblock/did");
37
+ const JWT = __importStar(require("@arcblock/jwt"));
14
38
  exports.WalletType = did_1.DidType;
15
39
  /**
16
40
  * Generate an wallet instance that can be used to sign a message or verify a signature
@@ -29,7 +53,8 @@ function Wallet(keyPair, t = 'default') {
29
53
  return hasher(data, round, encoding);
30
54
  },
31
55
  // @ts-ignore
32
- sign(data, hashBeforeSign = true, encoding = 'hex') {
56
+ // eslint-disable-next-line require-await
57
+ async sign(data, hashBeforeSign = true, encoding = 'hex') {
33
58
  if (!keyPair.sk) {
34
59
  throw new Error('Cannot sign data without a secretKey');
35
60
  }
@@ -53,7 +78,8 @@ function Wallet(keyPair, t = 'default') {
53
78
  }
54
79
  return signer.ethHash(data);
55
80
  },
56
- ethSign(data, hashBeforeSign = true) {
81
+ // eslint-disable-next-line require-await
82
+ async signETH(data, hashBeforeSign = true) {
57
83
  if (!keyPair.sk) {
58
84
  throw new Error('Cannot sign data without a secretKey');
59
85
  }
@@ -65,13 +91,24 @@ function Wallet(keyPair, t = 'default') {
65
91
  }
66
92
  return signer.ethSign(data, (0, util_1.toHex)(keyPair.sk));
67
93
  },
68
- ethVerify(data, signature, hashBeforeVerify = true) {
94
+ ethSign(data, hashBeforeSign = true) {
95
+ return this.signETH(data, hashBeforeSign);
96
+ },
97
+ // eslint-disable-next-line require-await
98
+ async ethVerify(data, signature, hashBeforeVerify = true) {
69
99
  if (typeof signer.ethHash !== 'function') {
70
100
  throw new Error('ethVerify is not supported by signer');
71
101
  }
72
102
  const hash = hashBeforeVerify ? signer.ethHash(data) : data;
73
103
  return signer.ethRecover(hash, signature) === this.address;
74
104
  },
105
+ // eslint-disable-next-line require-await
106
+ async signJWT(payload = {}, doSign = true, version = '1.0.0') {
107
+ if (!keyPair.sk) {
108
+ throw new Error('Cannot sign JWT without a secretKey');
109
+ }
110
+ return JWT.sign(this.address, keyPair.sk, payload, doSign, version);
111
+ },
75
112
  // deprecated
76
113
  toAddress() {
77
114
  return keyPair.pk ? (0, did_1.fromPublicKey)(keyPair.pk, type) : keyPair.address;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/wallet",
3
- "version": "1.25.5",
3
+ "version": "1.26.0",
4
4
  "description": "Utility function to create and use an forge compatible crypto wallet",
5
5
  "keywords": [
6
6
  "crypto",
@@ -43,9 +43,10 @@
43
43
  "url": "https://github.com/ArcBlock/blockchain/issues"
44
44
  },
45
45
  "dependencies": {
46
- "@arcblock/did": "1.25.5",
47
- "@ocap/util": "1.25.5",
48
- "@ocap/mcrypto": "1.25.5"
46
+ "@arcblock/did": "1.26.0",
47
+ "@arcblock/jwt": "1.26.0",
48
+ "@ocap/mcrypto": "1.26.0",
49
+ "@ocap/util": "1.26.0"
49
50
  },
50
51
  "scripts": {
51
52
  "lint": "eslint tests src",