@ocap/wallet 1.17.0 → 1.17.3

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { BytesType } from '@ocap/util';
1
+ /// <reference types="node" />
2
+ import { EncodingType, BytesType } from '@ocap/util';
2
3
  import { DidType, DIDType, DIDTypeStr, DIDTypeArg } from '@arcblock/did';
3
4
  declare type KeyPairType = {
4
5
  sk?: BytesType;
@@ -11,41 +12,43 @@ export declare type SerializedWallet = {
11
12
  sk: string;
12
13
  address: string;
13
14
  };
14
- export declare type WalletObject = {
15
+ export interface WalletObject {
15
16
  type: DIDType;
16
17
  secretKey: BytesType;
17
18
  publicKey: BytesType;
18
19
  address: string;
19
- hash: (data: BytesType, round?: number) => string;
20
- sign: (data: BytesType, hashBeforeSign?: boolean) => BytesType;
21
- verify: (data: BytesType, signature: BytesType, hashBeforeVerify?: boolean) => boolean;
22
- ethHash: (data: string) => string;
23
- ethSign: (data: string, hashBeforeSign?: boolean) => string;
24
- ethVerify: (data: string, signature: BytesType, hashBeforeVerify?: boolean) => boolean;
25
- toAddress: () => string;
26
- toJSON: () => SerializedWallet;
27
- };
20
+ hash(data: BytesType, round?: number, encoding?: 'hex'): string;
21
+ hash(data: BytesType, round?: number, encoding?: 'base16'): string;
22
+ hash(data: BytesType, round?: number, encoding?: 'base58'): string;
23
+ hash(data: BytesType, round?: number, encoding?: 'base64'): string;
24
+ hash(data: BytesType, round?: number, encoding?: 'buffer'): Buffer;
25
+ hash(data: BytesType, round?: number, encoding?: 'Uint8Array'): Uint8Array;
26
+ hash(data: BytesType, round?: number, encoding?: EncodingType): BytesType;
27
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'hex'): string;
28
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base16'): string;
29
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base58'): string;
30
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'base64'): string;
31
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Buffer;
32
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Uint8Array;
33
+ sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): BytesType;
34
+ verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean): boolean;
35
+ ethHash(data: string): string;
36
+ ethSign(data: string, hashBeforeSign?: boolean): string;
37
+ ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): boolean;
38
+ toJSON(): SerializedWallet;
39
+ /**
40
+ * @deprecated ES6: use `wallet.address` instead
41
+ */
42
+ toAddress(): string;
43
+ }
28
44
  export declare const WalletType: typeof DidType;
29
45
  /**
30
46
  * Generate an wallet instance that can be used to sign a message or verify a signature
31
- *
32
- * @public
33
- * @static
34
- * @param {object} keyPair - the key pair
35
- * @param {string} keyPair.sk - the secretKey
36
- * @param {string} keyPair.pk - the wallet publicKey
37
- * @param {DIDTypeArg} [type='default'] - wallet type
38
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
39
47
  */
40
48
  export declare function Wallet(keyPair: KeyPairType, t?: DIDTypeArg): WalletObject;
41
49
  /**
42
50
  * Generate a wallet from secretKey
43
51
  *
44
- * @public
45
- * @static
46
- * @param {string} sk - the secret key, `hex encoded string`
47
- * @param {DIDTypeArg} [type='default'] - wallet type
48
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
49
52
  * @example
50
53
  * const assert = require('assert');
51
54
  * const { fromSecretKey } = require('@ocap/wallet');
@@ -64,32 +67,16 @@ export declare function Wallet(keyPair: KeyPairType, t?: DIDTypeArg): WalletObje
64
67
  export declare function fromSecretKey(sk: BytesType, _type?: DIDTypeArg): WalletObject;
65
68
  /**
66
69
  * Generate a wallet from publicKey
67
- *
68
- * @public
69
- * @static
70
- * @param {string} pk - the public key, `hex encoded string`
71
- * @param {DIDTypeArg} [type='default'] - wallet type
72
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
73
70
  */
74
71
  export declare function fromPublicKey(pk: BytesType, _type?: DIDTypeArg): WalletObject;
75
72
  /**
76
73
  * Generate a wallet from address (did)
77
74
  *
78
75
  * Since we do not know the publicKey and secretKey, this kind of wallet cannot be used for signing and verifying
79
- *
80
- * @public
81
- * @static
82
- * @param {string} address - the wallet address
83
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
84
76
  */
85
77
  export declare function fromAddress(address: string): WalletObject;
86
78
  /**
87
79
  * Generate a wallet by generating a random secretKey
88
- *
89
- * @public
90
- * @static
91
- * @param {DIDTypeArg} [type='default'] - wallet type
92
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
93
80
  */
94
81
  export declare function fromRandom(_type?: DIDTypeArg): WalletObject;
95
82
  /**
@@ -98,11 +85,6 @@ export declare function fromRandom(_type?: DIDTypeArg): WalletObject;
98
85
  export declare function fromJSON(json: SerializedWallet): WalletObject;
99
86
  /**
100
87
  * Check if an object is valid wallet object
101
- *
102
- * @public
103
- * @static
104
- * @param {object} wallet
105
- * @param {boolean} canSign - should the wallet support sign
106
88
  */
107
89
  export declare function isValid(wallet: WalletObject, canSign?: boolean): boolean;
108
90
  export {};
package/lib/index.js CHANGED
@@ -7,14 +7,6 @@ const did_1 = require("@arcblock/did");
7
7
  exports.WalletType = did_1.DidType;
8
8
  /**
9
9
  * Generate an wallet instance that can be used to sign a message or verify a signature
10
- *
11
- * @public
12
- * @static
13
- * @param {object} keyPair - the key pair
14
- * @param {string} keyPair.sk - the secretKey
15
- * @param {string} keyPair.pk - the wallet publicKey
16
- * @param {DIDTypeArg} [type='default'] - wallet type
17
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
18
10
  */
19
11
  function Wallet(keyPair, t = 'default') {
20
12
  const type = (0, did_1.DidType)(t);
@@ -25,10 +17,12 @@ function Wallet(keyPair, t = 'default') {
25
17
  secretKey: keyPair.sk,
26
18
  publicKey: keyPair.pk,
27
19
  address: keyPair.pk ? (0, did_1.fromPublicKey)(keyPair.pk, type) : keyPair.address,
28
- hash(data, round = 1) {
29
- return hasher(data, round);
20
+ // @ts-ignore
21
+ hash(data, round = 1, encoding = 'hex') {
22
+ return hasher(data, round, encoding);
30
23
  },
31
- sign(data, hashBeforeSign = true) {
24
+ // @ts-ignore
25
+ sign(data, hashBeforeSign = true, encoding = 'hex') {
32
26
  if (!keyPair.sk) {
33
27
  throw new Error('Cannot sign data without a secretKey');
34
28
  }
@@ -36,7 +30,7 @@ function Wallet(keyPair, t = 'default') {
36
30
  const hash = hasher(data, 1);
37
31
  return signer.sign(hash, keyPair.sk);
38
32
  }
39
- return signer.sign(data, keyPair.sk);
33
+ return signer.sign(data, keyPair.sk, encoding);
40
34
  },
41
35
  verify(data, signature, hashBeforeVerify = true) {
42
36
  if (!keyPair.pk) {
@@ -59,9 +53,9 @@ function Wallet(keyPair, t = 'default') {
59
53
  throw new Error('ethSign is not supported by signer');
60
54
  }
61
55
  if (hashBeforeSign) {
62
- return signer.ethSign(signer.ethHash(data), keyPair.sk);
56
+ return signer.ethSign(signer.ethHash(data), (0, util_1.toHex)(keyPair.sk));
63
57
  }
64
- return signer.ethSign(data, keyPair.sk);
58
+ return signer.ethSign(data, (0, util_1.toHex)(keyPair.sk));
65
59
  },
66
60
  ethVerify(data, signature, hashBeforeVerify = true) {
67
61
  if (typeof signer.ethHash !== 'function') {
@@ -88,11 +82,6 @@ exports.Wallet = Wallet;
88
82
  /**
89
83
  * Generate a wallet from secretKey
90
84
  *
91
- * @public
92
- * @static
93
- * @param {string} sk - the secret key, `hex encoded string`
94
- * @param {DIDTypeArg} [type='default'] - wallet type
95
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
96
85
  * @example
97
86
  * const assert = require('assert');
98
87
  * const { fromSecretKey } = require('@ocap/wallet');
@@ -116,12 +105,6 @@ function fromSecretKey(sk, _type = 'default') {
116
105
  exports.fromSecretKey = fromSecretKey;
117
106
  /**
118
107
  * Generate a wallet from publicKey
119
- *
120
- * @public
121
- * @static
122
- * @param {string} pk - the public key, `hex encoded string`
123
- * @param {DIDTypeArg} [type='default'] - wallet type
124
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
125
108
  */
126
109
  function fromPublicKey(pk, _type = 'default') {
127
110
  return Wallet({ pk }, (0, did_1.DidType)(_type));
@@ -131,11 +114,6 @@ exports.fromPublicKey = fromPublicKey;
131
114
  * Generate a wallet from address (did)
132
115
  *
133
116
  * Since we do not know the publicKey and secretKey, this kind of wallet cannot be used for signing and verifying
134
- *
135
- * @public
136
- * @static
137
- * @param {string} address - the wallet address
138
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
139
117
  */
140
118
  function fromAddress(address) {
141
119
  return Wallet({ address: (0, did_1.toAddress)(address) }, (0, did_1.toTypeInfo)(address));
@@ -143,11 +121,6 @@ function fromAddress(address) {
143
121
  exports.fromAddress = fromAddress;
144
122
  /**
145
123
  * Generate a wallet by generating a random secretKey
146
- *
147
- * @public
148
- * @static
149
- * @param {DIDTypeArg} [type='default'] - wallet type
150
- * @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
151
124
  */
152
125
  function fromRandom(_type = 'default') {
153
126
  const type = (0, did_1.DidType)(_type);
@@ -166,11 +139,6 @@ function fromJSON(json) {
166
139
  exports.fromJSON = fromJSON;
167
140
  /**
168
141
  * Check if an object is valid wallet object
169
- *
170
- * @public
171
- * @static
172
- * @param {object} wallet
173
- * @param {boolean} canSign - should the wallet support sign
174
142
  */
175
143
  function isValid(wallet, canSign = true) {
176
144
  if (!wallet || typeof wallet !== 'object') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/wallet",
3
- "version": "1.17.0",
3
+ "version": "1.17.3",
4
4
  "description": "Utility function to create and use an forge compatible crypto wallet",
5
5
  "keywords": [
6
6
  "crypto",
@@ -52,9 +52,9 @@
52
52
  "url": "https://github.com/ArcBlock/asset-chain/issues"
53
53
  },
54
54
  "dependencies": {
55
- "@arcblock/did": "1.17.0",
56
- "@ocap/mcrypto": "1.17.0",
57
- "@ocap/util": "1.17.0"
55
+ "@arcblock/did": "1.17.3",
56
+ "@ocap/mcrypto": "1.17.3",
57
+ "@ocap/util": "1.17.3"
58
58
  },
59
- "gitHead": "f0944b60a5fb4115e1a3727bb07f68174bc56c5c"
59
+ "gitHead": "7a74e4e2b362a6e6ea8d14617f0480966c3363d5"
60
60
  }