@ocap/wallet 1.16.14 → 1.16.17
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/lib/index.d.ts +49 -118
- package/lib/index.js +118 -189
- package/package.json +20 -14
package/lib/index.d.ts
CHANGED
@@ -1,43 +1,50 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
import { BytesType } from '@ocap/util';
|
2
|
+
import { DidType, DIDType, DIDTypeStr, DIDTypeArg } from '@arcblock/did';
|
3
|
+
declare type KeyPairType = {
|
4
|
+
sk?: BytesType;
|
5
|
+
pk?: BytesType;
|
6
|
+
address?: string;
|
7
|
+
};
|
8
|
+
export declare type SerializedWallet = {
|
9
|
+
type: DIDTypeStr;
|
10
|
+
pk: string;
|
11
|
+
sk: string;
|
12
|
+
address: string;
|
13
|
+
};
|
14
|
+
export declare type WalletObject = {
|
15
|
+
type: DIDType;
|
16
|
+
secretKey: BytesType;
|
17
|
+
publicKey: BytesType;
|
18
|
+
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
|
+
};
|
28
|
+
export declare const WalletType: typeof DidType;
|
15
29
|
/**
|
16
|
-
*
|
30
|
+
* Generate an wallet instance that can be used to sign a message or verify a signature
|
17
31
|
*
|
18
32
|
* @public
|
19
33
|
* @static
|
20
|
-
* @param {
|
21
|
-
* @
|
22
|
-
* @
|
23
|
-
*
|
24
|
-
*
|
25
|
-
* const { types } = require('@ocap/mcrypto');
|
26
|
-
*
|
27
|
-
* const type = DidType({
|
28
|
-
* role: types.RoleType.ROLE_APPLICATION,
|
29
|
-
* pk: types.KeyType.ED25519,
|
30
|
-
* hash: types.HashType.SHA3,
|
31
|
-
* });
|
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
|
32
39
|
*/
|
33
|
-
declare function
|
40
|
+
export declare function Wallet(keyPair: KeyPairType, t?: DIDTypeArg): WalletObject;
|
34
41
|
/**
|
35
42
|
* Generate a wallet from secretKey
|
36
43
|
*
|
37
44
|
* @public
|
38
45
|
* @static
|
39
46
|
* @param {string} sk - the secret key, `hex encoded string`
|
40
|
-
* @param {
|
47
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
41
48
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
42
49
|
* @example
|
43
50
|
* const assert = require('assert');
|
@@ -54,30 +61,17 @@ declare function DidType(type?: DidType | string): object;
|
|
54
61
|
* assert.equal(signature, sig, "signature should match");
|
55
62
|
* assert.ok(wallet.verify(message, signature), "signature should be verified");
|
56
63
|
*/
|
57
|
-
declare function fromSecretKey(sk:
|
64
|
+
export declare function fromSecretKey(sk: BytesType, _type?: DIDTypeArg): WalletObject;
|
58
65
|
/**
|
59
66
|
* Generate a wallet from publicKey
|
60
67
|
*
|
61
68
|
* @public
|
62
69
|
* @static
|
63
70
|
* @param {string} pk - the public key, `hex encoded string`
|
64
|
-
* @param {
|
65
|
-
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
66
|
-
*/
|
67
|
-
declare function fromPublicKey(pk: string, _type?: string): WalletObject;
|
68
|
-
/**
|
69
|
-
* Generate a wallet by generating a random secretKey
|
70
|
-
*
|
71
|
-
* @public
|
72
|
-
* @static
|
73
|
-
* @param {DidType} [type='default'] - wallet type
|
71
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
74
72
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
75
|
-
* @example
|
76
|
-
* const { fromRandom } = require('@ocap/wallet');
|
77
|
-
* const wallet = fromRandom();
|
78
|
-
* // Do something with wallet
|
79
73
|
*/
|
80
|
-
declare function
|
74
|
+
export declare function fromPublicKey(pk: BytesType, _type?: DIDTypeArg): WalletObject;
|
81
75
|
/**
|
82
76
|
* Generate a wallet from address (did)
|
83
77
|
*
|
@@ -87,29 +81,21 @@ declare function fromRandom(_type?: string): WalletObject;
|
|
87
81
|
* @static
|
88
82
|
* @param {string} address - the wallet address
|
89
83
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
90
|
-
* @example
|
91
|
-
* const assert = require('assert');
|
92
|
-
* const { fromAddress } = require('@ocap/wallet');
|
93
|
-
*
|
94
|
-
* const address = 'zNKtCNqYWLYWYW3gWRA1vnRykfCBZYHZvzKr';
|
95
|
-
* const wallet = fromAddress(address);
|
96
|
-
* console.log(wallet.toJSON());
|
97
84
|
*/
|
98
|
-
declare function fromAddress(address: string): WalletObject;
|
85
|
+
export declare function fromAddress(address: string): WalletObject;
|
99
86
|
/**
|
100
|
-
*
|
87
|
+
* Generate a wallet by generating a random secretKey
|
101
88
|
*
|
102
89
|
* @public
|
103
90
|
* @static
|
104
|
-
* @param {
|
91
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
105
92
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
106
|
-
* @example
|
107
|
-
* const { fromJSON, fromRandom } = require('@ocap/wallet');
|
108
|
-
* const wallet = fromRandom();
|
109
|
-
* const wallet2 = fromJSON(wallet.toJSON());
|
110
|
-
* // wallet2 is identical to wallet
|
111
93
|
*/
|
112
|
-
declare function
|
94
|
+
export declare function fromRandom(_type?: DIDTypeArg): WalletObject;
|
95
|
+
/**
|
96
|
+
* Generating a wallet from a serialized json presentation of another wallet
|
97
|
+
*/
|
98
|
+
export declare function fromJSON(json: SerializedWallet): WalletObject;
|
113
99
|
/**
|
114
100
|
* Check if an object is valid wallet object
|
115
101
|
*
|
@@ -118,60 +104,5 @@ declare function fromJSON(json: any): WalletObject;
|
|
118
104
|
* @param {object} wallet
|
119
105
|
* @param {boolean} canSign - should the wallet support sign
|
120
106
|
*/
|
121
|
-
declare function isValid(wallet:
|
122
|
-
|
123
|
-
* @public
|
124
|
-
* @static
|
125
|
-
* @global
|
126
|
-
* @name WalletObject
|
127
|
-
* @typedef WalletObject
|
128
|
-
* @prop {DidType} type - Indicates the wallet type
|
129
|
-
* @prop {secretKey} secretKey - Wallet secretKey
|
130
|
-
* @prop {publicKey} publicKey - Wallet publicKey
|
131
|
-
* @prop {function} sign - Sign `data`, data is hashed using the `HashType` defined in type before signing
|
132
|
-
* @prop {function} verify - Verify `signature`, data is hashed using the `HashType` defined in type before verifying
|
133
|
-
* @prop {function} toAddress - Get wallet address without `did:abt:` prefix
|
134
|
-
* @prop {function} toJSON - Serialize wallet to json object, checkout {@link fromJSON} for deserialisation
|
135
|
-
*/
|
136
|
-
|
137
|
-
/**
|
138
|
-
* Generate an wallet instance that can be used to sign a message or verify a signature
|
139
|
-
*
|
140
|
-
* @public
|
141
|
-
* @static
|
142
|
-
* @param {object} keyPair - the key pair
|
143
|
-
* @param {string} keyPair.sk - the secretKey
|
144
|
-
* @param {string} keyPair.pk - the wallet publicKey
|
145
|
-
* @param {DidType} [type='default'] - wallet type
|
146
|
-
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
147
|
-
*/
|
148
|
-
declare function Wallet(keyPair: _Lib.T100, type?: typeof DidType): WalletObject;
|
149
|
-
declare const _Lib: _Lib.T101;
|
150
|
-
declare namespace _Lib {
|
151
|
-
export interface WalletObject {
|
152
|
-
type: typeof DidType;
|
153
|
-
secretKey: any;
|
154
|
-
publicKey: any;
|
155
|
-
sign: (...args: any[]) => any;
|
156
|
-
verify: (...args: any[]) => any;
|
157
|
-
toAddress: (...args: any[]) => any;
|
158
|
-
toJSON: (...args: any[]) => any;
|
159
|
-
}
|
160
|
-
export interface T100 {
|
161
|
-
sk: string;
|
162
|
-
pk: string;
|
163
|
-
}
|
164
|
-
export interface T101 {
|
165
|
-
fromSecretKey: typeof fromSecretKey;
|
166
|
-
fromPublicKey: typeof fromPublicKey;
|
167
|
-
fromRandom: typeof fromRandom;
|
168
|
-
fromAddress: typeof fromAddress;
|
169
|
-
fromDID: typeof fromAddress;
|
170
|
-
fromJSON: typeof fromJSON;
|
171
|
-
isValid: typeof isValid;
|
172
|
-
Wallet: typeof Wallet;
|
173
|
-
WalletType: typeof DidType;
|
174
|
-
DidType: typeof DidType;
|
175
|
-
}
|
176
|
-
}
|
177
|
-
export = _Lib;
|
107
|
+
export declare function isValid(wallet: WalletObject, canSign?: boolean): boolean;
|
108
|
+
export {};
|
package/lib/index.js
CHANGED
@@ -1,29 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
*/
|
9
|
-
const { getSigner, getHasher } = require('@ocap/mcrypto');
|
10
|
-
const { toAddress, fromPublicKey: DIDFromPublicKey, toTypeInfo, DidType } = require('@arcblock/did');
|
11
|
-
|
12
|
-
/**
|
13
|
-
* @public
|
14
|
-
* @static
|
15
|
-
* @global
|
16
|
-
* @name WalletObject
|
17
|
-
* @typedef WalletObject
|
18
|
-
* @prop {DidType} type - Indicates the wallet type
|
19
|
-
* @prop {secretKey} secretKey - Wallet secretKey
|
20
|
-
* @prop {publicKey} publicKey - Wallet publicKey
|
21
|
-
* @prop {function} sign - Sign `data`, data is hashed using the `HashType` defined in type before signing
|
22
|
-
* @prop {function} verify - Verify `signature`, data is hashed using the `HashType` defined in type before verifying
|
23
|
-
* @prop {function} toAddress - Get wallet address without `did:abt:` prefix
|
24
|
-
* @prop {function} toJSON - Serialize wallet to json object, checkout {@link fromJSON} for deserialisation
|
25
|
-
*/
|
26
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isValid = exports.fromJSON = exports.fromRandom = exports.fromAddress = exports.fromPublicKey = exports.fromSecretKey = exports.Wallet = exports.WalletType = void 0;
|
4
|
+
const util_1 = require("@ocap/util");
|
5
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
6
|
+
const did_1 = require("@arcblock/did");
|
7
|
+
exports.WalletType = did_1.DidType;
|
27
8
|
/**
|
28
9
|
* Generate an wallet instance that can be used to sign a message or verify a signature
|
29
10
|
*
|
@@ -32,98 +13,85 @@ const { toAddress, fromPublicKey: DIDFromPublicKey, toTypeInfo, DidType } = requ
|
|
32
13
|
* @param {object} keyPair - the key pair
|
33
14
|
* @param {string} keyPair.sk - the secretKey
|
34
15
|
* @param {string} keyPair.pk - the wallet publicKey
|
35
|
-
* @param {
|
16
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
36
17
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
37
18
|
*/
|
38
|
-
function Wallet(keyPair,
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
toAddress() {
|
106
|
-
return keyPair.pk ? DIDFromPublicKey(keyPair.pk, type) : keyPair.address;
|
107
|
-
},
|
108
|
-
|
109
|
-
toJSON() {
|
110
|
-
return {
|
111
|
-
type: DidType.toJSON(type),
|
112
|
-
sk: keyPair.sk,
|
113
|
-
pk: keyPair.pk,
|
114
|
-
address: this.address,
|
115
|
-
};
|
116
|
-
},
|
117
|
-
};
|
19
|
+
function Wallet(keyPair, t = 'default') {
|
20
|
+
const type = (0, did_1.DidType)(t);
|
21
|
+
const signer = (0, mcrypto_1.getSigner)(type.pk);
|
22
|
+
const hasher = (0, mcrypto_1.getHasher)(type.hash);
|
23
|
+
return {
|
24
|
+
type,
|
25
|
+
secretKey: keyPair.sk,
|
26
|
+
publicKey: keyPair.pk,
|
27
|
+
address: keyPair.pk ? (0, did_1.fromPublicKey)(keyPair.pk, type) : keyPair.address,
|
28
|
+
hash(data, round = 1) {
|
29
|
+
return hasher(data, round);
|
30
|
+
},
|
31
|
+
sign(data, hashBeforeSign = true) {
|
32
|
+
if (!keyPair.sk) {
|
33
|
+
throw new Error('Cannot sign data without a secretKey');
|
34
|
+
}
|
35
|
+
if (hashBeforeSign) {
|
36
|
+
const hash = hasher(data, 1);
|
37
|
+
return signer.sign(hash, keyPair.sk);
|
38
|
+
}
|
39
|
+
return signer.sign(data, keyPair.sk);
|
40
|
+
},
|
41
|
+
verify(data, signature, hashBeforeVerify = true) {
|
42
|
+
if (!keyPair.pk) {
|
43
|
+
throw new Error('Cannot verify data without a publicKey');
|
44
|
+
}
|
45
|
+
const hash = hashBeforeVerify ? hasher(data, 1) : data;
|
46
|
+
return signer.verify(hash, signature, keyPair.pk);
|
47
|
+
},
|
48
|
+
ethHash(data) {
|
49
|
+
if (typeof signer.ethHash !== 'function') {
|
50
|
+
throw new Error('ethHash is not supported by signer');
|
51
|
+
}
|
52
|
+
return signer.ethHash(data);
|
53
|
+
},
|
54
|
+
ethSign(data, hashBeforeSign = true) {
|
55
|
+
if (!keyPair.sk) {
|
56
|
+
throw new Error('Cannot sign data without a secretKey');
|
57
|
+
}
|
58
|
+
if (typeof signer.ethHash !== 'function') {
|
59
|
+
throw new Error('ethSign is not supported by signer');
|
60
|
+
}
|
61
|
+
if (hashBeforeSign) {
|
62
|
+
return signer.ethSign(signer.ethHash(data), keyPair.sk);
|
63
|
+
}
|
64
|
+
return signer.ethSign(data, keyPair.sk);
|
65
|
+
},
|
66
|
+
ethVerify(data, signature, hashBeforeVerify = true) {
|
67
|
+
if (typeof signer.ethHash !== 'function') {
|
68
|
+
throw new Error('ethVerify is not supported by signer');
|
69
|
+
}
|
70
|
+
const hash = hashBeforeVerify ? signer.ethHash(data) : data;
|
71
|
+
return signer.ethRecover(hash, signature) === this.address;
|
72
|
+
},
|
73
|
+
// deprecated
|
74
|
+
toAddress() {
|
75
|
+
return keyPair.pk ? (0, did_1.fromPublicKey)(keyPair.pk, type) : keyPair.address;
|
76
|
+
},
|
77
|
+
toJSON() {
|
78
|
+
return {
|
79
|
+
type: did_1.DidType.toJSON(type),
|
80
|
+
sk: (0, util_1.toHex)(keyPair.sk),
|
81
|
+
pk: (0, util_1.toHex)(keyPair.pk),
|
82
|
+
address: this.address,
|
83
|
+
};
|
84
|
+
},
|
85
|
+
};
|
118
86
|
}
|
119
|
-
|
87
|
+
exports.Wallet = Wallet;
|
120
88
|
/**
|
121
89
|
* Generate a wallet from secretKey
|
122
90
|
*
|
123
91
|
* @public
|
124
92
|
* @static
|
125
93
|
* @param {string} sk - the secret key, `hex encoded string`
|
126
|
-
* @param {
|
94
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
127
95
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
128
96
|
* @example
|
129
97
|
* const assert = require('assert');
|
@@ -141,24 +109,24 @@ function Wallet(keyPair, type = 'default') {
|
|
141
109
|
* assert.ok(wallet.verify(message, signature), "signature should be verified");
|
142
110
|
*/
|
143
111
|
function fromSecretKey(sk, _type = 'default') {
|
144
|
-
|
145
|
-
|
146
|
-
|
112
|
+
const type = (0, did_1.DidType)(_type);
|
113
|
+
const keyPair = { sk, pk: (0, mcrypto_1.getSigner)(type.pk).getPublicKey(sk) };
|
114
|
+
return Wallet(keyPair, type);
|
147
115
|
}
|
148
|
-
|
116
|
+
exports.fromSecretKey = fromSecretKey;
|
149
117
|
/**
|
150
118
|
* Generate a wallet from publicKey
|
151
119
|
*
|
152
120
|
* @public
|
153
121
|
* @static
|
154
122
|
* @param {string} pk - the public key, `hex encoded string`
|
155
|
-
* @param {
|
123
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
156
124
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
157
125
|
*/
|
158
126
|
function fromPublicKey(pk, _type = 'default') {
|
159
|
-
|
127
|
+
return Wallet({ pk }, (0, did_1.DidType)(_type));
|
160
128
|
}
|
161
|
-
|
129
|
+
exports.fromPublicKey = fromPublicKey;
|
162
130
|
/**
|
163
131
|
* Generate a wallet from address (did)
|
164
132
|
*
|
@@ -168,55 +136,34 @@ function fromPublicKey(pk, _type = 'default') {
|
|
168
136
|
* @static
|
169
137
|
* @param {string} address - the wallet address
|
170
138
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
171
|
-
* @example
|
172
|
-
* const assert = require('assert');
|
173
|
-
* const { fromAddress } = require('@ocap/wallet');
|
174
|
-
*
|
175
|
-
* const address = 'zNKtCNqYWLYWYW3gWRA1vnRykfCBZYHZvzKr';
|
176
|
-
* const wallet = fromAddress(address);
|
177
|
-
* console.log(wallet.toJSON());
|
178
139
|
*/
|
179
140
|
function fromAddress(address) {
|
180
|
-
|
141
|
+
return Wallet({ address: (0, did_1.toAddress)(address) }, (0, did_1.toTypeInfo)(address));
|
181
142
|
}
|
182
|
-
|
143
|
+
exports.fromAddress = fromAddress;
|
183
144
|
/**
|
184
145
|
* Generate a wallet by generating a random secretKey
|
185
146
|
*
|
186
147
|
* @public
|
187
148
|
* @static
|
188
|
-
* @param {
|
149
|
+
* @param {DIDTypeArg} [type='default'] - wallet type
|
189
150
|
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
190
|
-
* @example
|
191
|
-
* const { fromRandom } = require('@ocap/wallet');
|
192
|
-
* const wallet = fromRandom();
|
193
|
-
* // Do something with wallet
|
194
151
|
*/
|
195
152
|
function fromRandom(_type = 'default') {
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
153
|
+
const type = (0, did_1.DidType)(_type);
|
154
|
+
const signer = (0, mcrypto_1.getSigner)(type.pk);
|
155
|
+
const keyPair = signer.genKeyPair();
|
156
|
+
return Wallet({ sk: keyPair.secretKey, pk: keyPair.publicKey }, type);
|
200
157
|
}
|
201
|
-
|
158
|
+
exports.fromRandom = fromRandom;
|
202
159
|
/**
|
203
160
|
* Generating a wallet from a serialized json presentation of another wallet
|
204
|
-
*
|
205
|
-
* @public
|
206
|
-
* @static
|
207
|
-
* @param {object} json - json presentation of a wallet
|
208
|
-
* @returns {WalletObject} wallet object that can be used to sign/verify/getAddress
|
209
|
-
* @example
|
210
|
-
* const { fromJSON, fromRandom } = require('@ocap/wallet');
|
211
|
-
* const wallet = fromRandom();
|
212
|
-
* const wallet2 = fromJSON(wallet.toJSON());
|
213
|
-
* // wallet2 is identical to wallet
|
214
161
|
*/
|
215
162
|
function fromJSON(json) {
|
216
|
-
|
217
|
-
|
163
|
+
const type = did_1.DidType.fromJSON(json.type);
|
164
|
+
return Wallet(json, type);
|
218
165
|
}
|
219
|
-
|
166
|
+
exports.fromJSON = fromJSON;
|
220
167
|
/**
|
221
168
|
* Check if an object is valid wallet object
|
222
169
|
*
|
@@ -226,47 +173,29 @@ function fromJSON(json) {
|
|
226
173
|
* @param {boolean} canSign - should the wallet support sign
|
227
174
|
*/
|
228
175
|
function isValid(wallet, canSign = true) {
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
if (!wallet.type || !wallet.publicKey) {
|
246
|
-
return false;
|
247
|
-
}
|
248
|
-
|
249
|
-
if (canSign) {
|
250
|
-
if (!wallet.secretKey) {
|
251
|
-
return false;
|
176
|
+
if (!wallet || typeof wallet !== 'object') {
|
177
|
+
return false;
|
178
|
+
}
|
179
|
+
if (typeof wallet.verify !== 'function') {
|
180
|
+
return false;
|
181
|
+
}
|
182
|
+
if (typeof wallet.toAddress !== 'function') {
|
183
|
+
return false;
|
184
|
+
}
|
185
|
+
if (typeof wallet.toJSON !== 'function') {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
if (!wallet.type || !wallet.publicKey) {
|
189
|
+
return false;
|
252
190
|
}
|
253
|
-
if (
|
254
|
-
|
191
|
+
if (canSign) {
|
192
|
+
if (!wallet.secretKey) {
|
193
|
+
return false;
|
194
|
+
}
|
195
|
+
if (typeof wallet.sign !== 'function') {
|
196
|
+
return false;
|
197
|
+
}
|
255
198
|
}
|
256
|
-
|
257
|
-
|
258
|
-
return true;
|
199
|
+
return true;
|
259
200
|
}
|
260
|
-
|
261
|
-
module.exports = {
|
262
|
-
fromSecretKey,
|
263
|
-
fromPublicKey,
|
264
|
-
fromRandom,
|
265
|
-
fromAddress,
|
266
|
-
fromDID: fromAddress,
|
267
|
-
fromJSON,
|
268
|
-
isValid,
|
269
|
-
Wallet,
|
270
|
-
WalletType: DidType,
|
271
|
-
DidType,
|
272
|
-
};
|
201
|
+
exports.isValid = isValid;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ocap/wallet",
|
3
|
-
"version": "1.16.
|
3
|
+
"version": "1.16.17",
|
4
4
|
"description": "Utility function to create and use an forge compatible crypto wallet",
|
5
5
|
"keywords": [
|
6
6
|
"crypto",
|
@@ -21,34 +21,40 @@
|
|
21
21
|
"homepage": "https://github.com/ArcBlock/asset-chain/tree/master/core/forge-wallet",
|
22
22
|
"license": "Apache-2.0",
|
23
23
|
"main": "lib/index.js",
|
24
|
+
"typings": "lib/index.d.ts",
|
24
25
|
"files": [
|
25
26
|
"lib"
|
26
27
|
],
|
27
28
|
"devDependencies": {
|
28
|
-
"
|
29
|
-
"
|
29
|
+
"@arcblock/eslint-config-ts": "0.2.2",
|
30
|
+
"@types/jest": "^28.1.0",
|
31
|
+
"@types/node": "^17.0.38",
|
32
|
+
"eslint": "^8.17.0",
|
33
|
+
"jest": "^28.1.0",
|
34
|
+
"ts-jest": "^28.0.3",
|
35
|
+
"typescript": "^4.7.3"
|
30
36
|
},
|
31
37
|
"repository": {
|
32
38
|
"type": "git",
|
33
39
|
"url": "git+https://github.com/ArcBlock/asset-chain.git"
|
34
40
|
},
|
35
41
|
"scripts": {
|
36
|
-
"lint": "eslint tests
|
37
|
-
"lint:fix": "eslint --fix tests
|
38
|
-
"docs": "yarn gen-dts && yarn gen-docs && yarn cleanup-docs && yarn format-docs",
|
39
|
-
"cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
|
40
|
-
"gen-docs": "jsdoc2md lib/index.js > docs/README.md",
|
41
|
-
"gen-dts": "j2d lib/index.js",
|
42
|
-
"format-docs": "remark . -o",
|
42
|
+
"lint": "eslint tests src",
|
43
|
+
"lint:fix": "eslint --fix tests src",
|
43
44
|
"test": "jest --forceExit --detectOpenHandles",
|
44
|
-
"coverage": "yarn test -- --coverage"
|
45
|
+
"coverage": "yarn test -- --coverage",
|
46
|
+
"clean": "rm -fr lib",
|
47
|
+
"prebuild": "npm run clean",
|
48
|
+
"build": "tsc",
|
49
|
+
"build:watch": "npm run build -- -w"
|
45
50
|
},
|
46
51
|
"bugs": {
|
47
52
|
"url": "https://github.com/ArcBlock/asset-chain/issues"
|
48
53
|
},
|
49
54
|
"dependencies": {
|
50
|
-
"@arcblock/did": "1.16.
|
51
|
-
"@ocap/mcrypto": "1.16.
|
55
|
+
"@arcblock/did": "1.16.17",
|
56
|
+
"@ocap/mcrypto": "1.16.17",
|
57
|
+
"@ocap/util": "1.16.17"
|
52
58
|
},
|
53
|
-
"gitHead": "
|
59
|
+
"gitHead": "489ce5e03bce27ddcd535390228b11ab56e7a2e3"
|
54
60
|
}
|