@ocap/wallet 1.18.166 → 1.19.1

Sign up to get free protection for your applications and to get access to all the features.
package/esm/index.d.ts ADDED
@@ -0,0 +1,89 @@
1
+ import { EncodingType, BytesType } from '@ocap/util';
2
+ import { DidType, DIDType, DIDTypeStr, DIDTypeArg } from '@arcblock/did';
3
+ type KeyPairType<T extends BytesType = string> = {
4
+ sk?: T;
5
+ pk?: T;
6
+ address?: string;
7
+ };
8
+ export type SerializedWallet = {
9
+ type: DIDTypeStr;
10
+ pk: string;
11
+ sk: string;
12
+ address: string;
13
+ };
14
+ export interface WalletObject<T extends BytesType = string> {
15
+ type: DIDType;
16
+ secretKey: T;
17
+ publicKey: T;
18
+ address: string;
19
+ hash(data: BytesType, round?: number, encoding?: 'hex'): string;
20
+ hash(data: BytesType, round?: number, encoding?: 'base16'): string;
21
+ hash(data: BytesType, round?: number, encoding?: 'base58'): string;
22
+ hash(data: BytesType, round?: number, encoding?: 'base64'): string;
23
+ hash(data: BytesType, round?: number, encoding?: 'buffer'): Buffer;
24
+ hash(data: BytesType, round?: number, encoding?: 'Uint8Array'): Uint8Array;
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;
33
+ verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
34
+ ethHash(data: string): string;
35
+ ethSign(data: string, hashBeforeSign?: boolean): string;
36
+ ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): boolean;
37
+ toJSON(): SerializedWallet;
38
+ /**
39
+ * @deprecated ES6: use `wallet.address` instead
40
+ */
41
+ toAddress(): string;
42
+ }
43
+ export declare const WalletType: typeof DidType;
44
+ /**
45
+ * Generate an wallet instance that can be used to sign a message or verify a signature
46
+ */
47
+ export declare function Wallet<T extends BytesType = string>(keyPair: KeyPairType<T>, t?: DIDTypeArg): WalletObject<T>;
48
+ /**
49
+ * Generate a wallet from secretKey
50
+ *
51
+ * @example
52
+ * const assert = require('assert');
53
+ * const { fromSecretKey } = require('@ocap/wallet');
54
+ *
55
+ * const sk =
56
+ * '0xD67C071B6F51D2B61180B9B1AA9BE0DD0704619F0E30453AB4A592B036EDE644E4852B7091317E3622068E62A5127D1FB0D4AE2FC50213295E10652D2F0ABFC7';
57
+ * const sig =
58
+ * '0x08a102851c38c072e42756c1cc70938b5499c8e9358dfe5f383823f56cdb282ffda60fcd581a02c6c673069e5afc0bf09abbe3639b61b84d64fd58ef9f083003';
59
+ *
60
+ * const wallet = fromSecretKey(sk, type);
61
+ * const message = 'data to sign';
62
+ * const signature = wallet.sign(message);
63
+ * assert.equal(signature, sig, "signature should match");
64
+ * assert.ok(wallet.verify(message, signature), "signature should be verified");
65
+ */
66
+ export declare function fromSecretKey<T extends BytesType = string>(sk: T, _type?: DIDTypeArg): WalletObject<T>;
67
+ /**
68
+ * Generate a wallet from publicKey
69
+ */
70
+ export declare function fromPublicKey<T extends BytesType = string>(pk: T, _type?: DIDTypeArg): WalletObject<T>;
71
+ /**
72
+ * Generate a wallet from address (did)
73
+ *
74
+ * Since we do not know the publicKey and secretKey, this kind of wallet cannot be used for signing and verifying
75
+ */
76
+ export declare function fromAddress<T extends BytesType = string>(address: string): WalletObject<T>;
77
+ /**
78
+ * Generate a wallet by generating a random secretKey
79
+ */
80
+ export declare function fromRandom<T extends BytesType = string>(_type?: DIDTypeArg): WalletObject<T>;
81
+ /**
82
+ * Generating a wallet from a serialized json presentation of another wallet
83
+ */
84
+ export declare function fromJSON<T extends BytesType = string>(json: SerializedWallet): WalletObject<T>;
85
+ /**
86
+ * Check if an object is valid wallet object
87
+ */
88
+ export declare function isValid(wallet: WalletObject, canSign?: boolean): boolean;
89
+ export {};
package/esm/index.js ADDED
@@ -0,0 +1,161 @@
1
+ import { toHex } from '@ocap/util';
2
+ import { getSigner, getHasher } from '@ocap/mcrypto';
3
+ import { toAddress, fromPublicKey as DIDFromPublicKey, toTypeInfo, DidType, } from '@arcblock/did';
4
+ export const WalletType = DidType;
5
+ /**
6
+ * Generate an wallet instance that can be used to sign a message or verify a signature
7
+ */
8
+ export function Wallet(keyPair, t = 'default') {
9
+ const type = DidType(t);
10
+ const signer = getSigner(type.pk);
11
+ const hasher = getHasher(type.hash);
12
+ return {
13
+ type,
14
+ secretKey: keyPair.sk,
15
+ publicKey: keyPair.pk,
16
+ address: keyPair.pk ? DIDFromPublicKey(keyPair.pk, type) : keyPair.address,
17
+ // @ts-ignore
18
+ hash(data, round = 1, encoding = 'hex') {
19
+ return hasher(data, round, encoding);
20
+ },
21
+ // @ts-ignore
22
+ sign(data, hashBeforeSign = true, encoding = 'hex') {
23
+ if (!keyPair.sk) {
24
+ throw new Error('Cannot sign data without a secretKey');
25
+ }
26
+ if (hashBeforeSign) {
27
+ const hash = hasher(data, 1);
28
+ return signer.sign(hash, keyPair.sk, encoding);
29
+ }
30
+ return signer.sign(data, keyPair.sk, encoding);
31
+ },
32
+ // eslint-disable-next-line require-await
33
+ async verify(data, signature, hashBeforeVerify = true, extra) {
34
+ if (!keyPair.pk) {
35
+ throw new Error('Cannot verify data without a publicKey');
36
+ }
37
+ const hash = hashBeforeVerify ? hasher(data, 1) : data;
38
+ return signer.verify(hash, signature, keyPair.pk, extra);
39
+ },
40
+ ethHash(data) {
41
+ if (typeof signer.ethHash !== 'function') {
42
+ throw new Error('ethHash is not supported by signer');
43
+ }
44
+ return signer.ethHash(data);
45
+ },
46
+ ethSign(data, hashBeforeSign = true) {
47
+ if (!keyPair.sk) {
48
+ throw new Error('Cannot sign data without a secretKey');
49
+ }
50
+ if (typeof signer.ethHash !== 'function') {
51
+ throw new Error('ethSign is not supported by signer');
52
+ }
53
+ if (hashBeforeSign) {
54
+ return signer.ethSign(signer.ethHash(data), toHex(keyPair.sk));
55
+ }
56
+ return signer.ethSign(data, toHex(keyPair.sk));
57
+ },
58
+ ethVerify(data, signature, hashBeforeVerify = true) {
59
+ if (typeof signer.ethHash !== 'function') {
60
+ throw new Error('ethVerify is not supported by signer');
61
+ }
62
+ const hash = hashBeforeVerify ? signer.ethHash(data) : data;
63
+ return signer.ethRecover(hash, signature) === this.address;
64
+ },
65
+ // deprecated
66
+ toAddress() {
67
+ return keyPair.pk ? DIDFromPublicKey(keyPair.pk, type) : keyPair.address;
68
+ },
69
+ toJSON() {
70
+ return {
71
+ type: DidType.toJSON(type),
72
+ sk: toHex(keyPair.sk),
73
+ pk: toHex(keyPair.pk),
74
+ address: this.address,
75
+ };
76
+ },
77
+ };
78
+ }
79
+ /**
80
+ * Generate a wallet from secretKey
81
+ *
82
+ * @example
83
+ * const assert = require('assert');
84
+ * const { fromSecretKey } = require('@ocap/wallet');
85
+ *
86
+ * const sk =
87
+ * '0xD67C071B6F51D2B61180B9B1AA9BE0DD0704619F0E30453AB4A592B036EDE644E4852B7091317E3622068E62A5127D1FB0D4AE2FC50213295E10652D2F0ABFC7';
88
+ * const sig =
89
+ * '0x08a102851c38c072e42756c1cc70938b5499c8e9358dfe5f383823f56cdb282ffda60fcd581a02c6c673069e5afc0bf09abbe3639b61b84d64fd58ef9f083003';
90
+ *
91
+ * const wallet = fromSecretKey(sk, type);
92
+ * const message = 'data to sign';
93
+ * const signature = wallet.sign(message);
94
+ * assert.equal(signature, sig, "signature should match");
95
+ * assert.ok(wallet.verify(message, signature), "signature should be verified");
96
+ */
97
+ export function fromSecretKey(sk, _type = 'default') {
98
+ const type = DidType(_type);
99
+ const keyPair = { sk, pk: getSigner(type.pk).getPublicKey(sk) };
100
+ return Wallet(keyPair, type);
101
+ }
102
+ /**
103
+ * Generate a wallet from publicKey
104
+ */
105
+ export function fromPublicKey(pk, _type = 'default') {
106
+ return Wallet({ pk }, DidType(_type));
107
+ }
108
+ /**
109
+ * Generate a wallet from address (did)
110
+ *
111
+ * Since we do not know the publicKey and secretKey, this kind of wallet cannot be used for signing and verifying
112
+ */
113
+ export function fromAddress(address) {
114
+ return Wallet({ address: toAddress(address) }, toTypeInfo(address));
115
+ }
116
+ /**
117
+ * Generate a wallet by generating a random secretKey
118
+ */
119
+ export function fromRandom(_type = 'default') {
120
+ const type = DidType(_type);
121
+ const signer = getSigner(type.pk);
122
+ const keyPair = signer.genKeyPair();
123
+ return Wallet({ sk: keyPair.secretKey, pk: keyPair.publicKey }, type);
124
+ }
125
+ /**
126
+ * Generating a wallet from a serialized json presentation of another wallet
127
+ */
128
+ export function fromJSON(json) {
129
+ const type = DidType.fromJSON(json.type);
130
+ // @ts-ignore
131
+ return Wallet(json, type);
132
+ }
133
+ /**
134
+ * Check if an object is valid wallet object
135
+ */
136
+ export function isValid(wallet, canSign = true) {
137
+ if (!wallet || typeof wallet !== 'object') {
138
+ return false;
139
+ }
140
+ if (typeof wallet.verify !== 'function') {
141
+ return false;
142
+ }
143
+ if (typeof wallet.toAddress !== 'function') {
144
+ return false;
145
+ }
146
+ if (typeof wallet.toJSON !== 'function') {
147
+ return false;
148
+ }
149
+ if (!wallet.type || !wallet.publicKey) {
150
+ return false;
151
+ }
152
+ if (canSign) {
153
+ if (!wallet.secretKey) {
154
+ return false;
155
+ }
156
+ if (typeof wallet.sign !== 'function') {
157
+ return false;
158
+ }
159
+ }
160
+ return true;
161
+ }
package/lib/index.d.ts CHANGED
@@ -1,12 +1,11 @@
1
- /// <reference types="node" />
2
1
  import { EncodingType, BytesType } from '@ocap/util';
3
2
  import { DidType, DIDType, DIDTypeStr, DIDTypeArg } from '@arcblock/did';
4
- declare type KeyPairType<T extends BytesType = string> = {
3
+ type KeyPairType<T extends BytesType = string> = {
5
4
  sk?: T;
6
5
  pk?: T;
7
6
  address?: string;
8
7
  };
9
- export declare type SerializedWallet = {
8
+ export type SerializedWallet = {
10
9
  type: DIDTypeStr;
11
10
  pk: string;
12
11
  sk: string;
@@ -31,7 +30,7 @@ export interface WalletObject<T extends BytesType = string> {
31
30
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'buffer'): Buffer;
32
31
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Uint8Array;
33
32
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): BytesType;
34
- verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean): boolean;
33
+ verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
35
34
  ethHash(data: string): string;
36
35
  ethSign(data: string, hashBeforeSign?: boolean): string;
37
36
  ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): boolean;
package/lib/index.js CHANGED
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
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;
3
+ exports.WalletType = void 0;
4
+ exports.Wallet = Wallet;
5
+ exports.fromSecretKey = fromSecretKey;
6
+ exports.fromPublicKey = fromPublicKey;
7
+ exports.fromAddress = fromAddress;
8
+ exports.fromRandom = fromRandom;
9
+ exports.fromJSON = fromJSON;
10
+ exports.isValid = isValid;
4
11
  const util_1 = require("@ocap/util");
5
12
  const mcrypto_1 = require("@ocap/mcrypto");
6
13
  const did_1 = require("@arcblock/did");
@@ -32,12 +39,13 @@ function Wallet(keyPair, t = 'default') {
32
39
  }
33
40
  return signer.sign(data, keyPair.sk, encoding);
34
41
  },
35
- verify(data, signature, hashBeforeVerify = true) {
42
+ // eslint-disable-next-line require-await
43
+ async verify(data, signature, hashBeforeVerify = true, extra) {
36
44
  if (!keyPair.pk) {
37
45
  throw new Error('Cannot verify data without a publicKey');
38
46
  }
39
47
  const hash = hashBeforeVerify ? hasher(data, 1) : data;
40
- return signer.verify(hash, signature, keyPair.pk);
48
+ return signer.verify(hash, signature, keyPair.pk, extra);
41
49
  },
42
50
  ethHash(data) {
43
51
  if (typeof signer.ethHash !== 'function') {
@@ -78,7 +86,6 @@ function Wallet(keyPair, t = 'default') {
78
86
  },
79
87
  };
80
88
  }
81
- exports.Wallet = Wallet;
82
89
  /**
83
90
  * Generate a wallet from secretKey
84
91
  *
@@ -102,14 +109,12 @@ function fromSecretKey(sk, _type = 'default') {
102
109
  const keyPair = { sk, pk: (0, mcrypto_1.getSigner)(type.pk).getPublicKey(sk) };
103
110
  return Wallet(keyPair, type);
104
111
  }
105
- exports.fromSecretKey = fromSecretKey;
106
112
  /**
107
113
  * Generate a wallet from publicKey
108
114
  */
109
115
  function fromPublicKey(pk, _type = 'default') {
110
116
  return Wallet({ pk }, (0, did_1.DidType)(_type));
111
117
  }
112
- exports.fromPublicKey = fromPublicKey;
113
118
  /**
114
119
  * Generate a wallet from address (did)
115
120
  *
@@ -118,7 +123,6 @@ exports.fromPublicKey = fromPublicKey;
118
123
  function fromAddress(address) {
119
124
  return Wallet({ address: (0, did_1.toAddress)(address) }, (0, did_1.toTypeInfo)(address));
120
125
  }
121
- exports.fromAddress = fromAddress;
122
126
  /**
123
127
  * Generate a wallet by generating a random secretKey
124
128
  */
@@ -128,7 +132,6 @@ function fromRandom(_type = 'default') {
128
132
  const keyPair = signer.genKeyPair();
129
133
  return Wallet({ sk: keyPair.secretKey, pk: keyPair.publicKey }, type);
130
134
  }
131
- exports.fromRandom = fromRandom;
132
135
  /**
133
136
  * Generating a wallet from a serialized json presentation of another wallet
134
137
  */
@@ -137,7 +140,6 @@ function fromJSON(json) {
137
140
  // @ts-ignore
138
141
  return Wallet(json, type);
139
142
  }
140
- exports.fromJSON = fromJSON;
141
143
  /**
142
144
  * Check if an object is valid wallet object
143
145
  */
@@ -167,4 +169,3 @@ function isValid(wallet, canSign = true) {
167
169
  }
168
170
  return true;
169
171
  }
170
- exports.isValid = isValid;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/wallet",
3
- "version": "1.18.166",
3
+ "version": "1.19.1",
4
4
  "description": "Utility function to create and use an forge compatible crypto wallet",
5
5
  "keywords": [
6
6
  "crypto",
@@ -20,19 +20,28 @@
20
20
  ],
21
21
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/forge-wallet",
22
22
  "license": "Apache-2.0",
23
- "main": "lib/index.js",
24
- "typings": "lib/index.d.ts",
23
+ "main": "./lib/index.js",
24
+ "module": "./lib/index.js",
25
+ "types": "./esm/index.d.ts",
25
26
  "files": [
26
- "lib"
27
+ "lib",
28
+ "esm"
27
29
  ],
30
+ "exports": {
31
+ ".": {
32
+ "import": "./esm/index.js",
33
+ "require": "./lib/index.js",
34
+ "default": "./esm/index.js"
35
+ }
36
+ },
28
37
  "devDependencies": {
29
- "@arcblock/eslint-config-ts": "0.2.3",
30
- "@types/jest": "^29.5.12",
31
- "@types/node": "^17.0.45",
32
- "eslint": "^8.25.0",
38
+ "@arcblock/eslint-config-ts": "0.3.3",
39
+ "@types/jest": "^29.5.13",
40
+ "@types/node": "^22.7.5",
41
+ "eslint": "^8.57.0",
33
42
  "jest": "^29.7.0",
34
43
  "ts-jest": "^29.2.5",
35
- "typescript": "^4.8.4"
44
+ "typescript": "^5.6.2"
36
45
  },
37
46
  "repository": {
38
47
  "type": "git",
@@ -43,18 +52,20 @@
43
52
  "lint:fix": "eslint --fix tests src",
44
53
  "test": "jest --forceExit --detectOpenHandles",
45
54
  "coverage": "npm run test -- --coverage",
46
- "clean": "rm -fr lib",
55
+ "clean": "rm -fr lib esm",
47
56
  "prebuild": "npm run clean",
48
- "build": "tsc",
57
+ "build:cjs": "tsc -p tsconfig.cjs.json",
58
+ "build:esm": "tsc -p tsconfig.esm.json",
59
+ "build": "npm run build:cjs && npm run build:esm",
49
60
  "build:watch": "npm run build -- -w"
50
61
  },
51
62
  "bugs": {
52
63
  "url": "https://github.com/ArcBlock/blockchain/issues"
53
64
  },
54
65
  "dependencies": {
55
- "@arcblock/did": "1.18.166",
56
- "@ocap/mcrypto": "1.18.166",
57
- "@ocap/util": "1.18.166"
66
+ "@arcblock/did": "1.19.1",
67
+ "@ocap/mcrypto": "1.19.1",
68
+ "@ocap/util": "1.19.1"
58
69
  },
59
- "gitHead": "58c8356b3b8c238728560e4c3fef6ed1704d3ac4"
70
+ "gitHead": "21184488172c6c824ebd1714f728ff2aee4a3ac0"
60
71
  }