@arcblock/did 1.6.3 → 1.6.10

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
4
4
  [![docs](https://img.shields.io/badge/powered%20by-arcblock-green.svg)](https://docs.arcblock.io)
5
- [![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+ [![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge\&utm_medium=badge\&utm_campaign=pr-badge)
6
6
 
7
7
  > Javascript library to manipulate ArcBlock DID: <https://github.com/ArcBlock/abt-did-spec>
8
8
 
@@ -49,4 +49,4 @@ console.log(`abt:did:${userDID}`);
49
49
 
50
50
  ## Documentation
51
51
 
52
- For full documentation, checkout [https://ocap-js.netlify.com](https://ocap-js.netlify.com/)
52
+ For full documentation, checkout [https://asset-chain.netlify.com](https://asset-chain.netlify.com/)
package/lib/index.d.ts CHANGED
@@ -1,5 +1,13 @@
1
1
  // Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme)
2
2
 
3
+ /**
4
+ * Ensure the hex length is even number, 2, 4, 6, 8
5
+ *
6
+ * @param {string} hex
7
+ * @param {number} length
8
+ * @returns {string} hex
9
+ */
10
+ declare function toStrictHex(hex: string, length: number): string;
3
11
  /**
4
12
  * Convert did to address: remove `did:abt:` prefix
5
13
  *
@@ -18,6 +26,34 @@ declare function toAddress(did: string): string;
18
26
  * @returns {string}
19
27
  */
20
28
  declare function toDid(address: any): string;
29
+ /**
30
+ * Checks if the given string is an address
31
+ *
32
+ * @method isEthereumDid
33
+ * @param {String} address the given HEX address
34
+ * @return {Boolean}
35
+ */
36
+ declare function isEthereumDid(did: any): boolean;
37
+ declare function isEthereumType(type: any): any;
38
+ /**
39
+ * Get type info from did (base58 format)
40
+ *
41
+ * @public
42
+ * @static
43
+ * @param {string} did - address string
44
+ * @param {boolean} [returnString=true]
45
+ * @returns {object} wallet type {@see @arcblock/did#DidType}
46
+ */
47
+ declare function toTypeInfo(did: string, returnString?: boolean): object;
48
+ /**
49
+ * Convert type info object to hex string
50
+ *
51
+ * @public
52
+ * @static
53
+ * @param {object} type - wallet type, {@see @arcblock/did#DidType}
54
+ * @returns string
55
+ */
56
+ declare function fromTypeInfo(type: object): string;
21
57
  /**
22
58
  * The structure of a forge wallet type
23
59
  *
@@ -48,21 +84,12 @@ declare function toDid(address: any): string;
48
84
  * hash: types.HashType.SHA3,
49
85
  * });
50
86
  */
51
- declare function DidType(type?: string | DidType_1): any;
52
- declare const _Lib: _Lib.T102;
87
+ declare function DidType(type?: DidType | string): object;
88
+ declare const _Lib: _Lib.T100;
53
89
  declare namespace _Lib {
54
90
  export interface T100 {
55
- [key: string]: any;
56
- }
57
- export interface T101 {
58
- role: any;
59
- pk: any;
60
- hash: any;
61
- address: any;
62
- }
63
- export interface T102 {
64
91
  types: any;
65
- toStrictHex: (hex: string, length: number) => string;
92
+ toStrictHex: typeof toStrictHex;
66
93
  fromSecretKey: (sk: string, type: any) => string;
67
94
  fromPublicKey: (pk: string, type: any) => string;
68
95
  fromPublicKeyHash: (buffer: any, type: any) => string;
@@ -71,11 +98,13 @@ declare namespace _Lib {
71
98
  toDid: typeof toDid;
72
99
  isFromPublicKey: (did: string, pk: string) => boolean;
73
100
  isValid: (did: string) => boolean;
74
- toTypeInfo: (did: string, returnString?: boolean) => any;
75
- fromTypeInfo: (type: any) => string;
101
+ isEthereumDid: typeof isEthereumDid;
102
+ isEthereumType: typeof isEthereumType;
103
+ toTypeInfo: typeof toTypeInfo;
104
+ fromTypeInfo: typeof fromTypeInfo;
76
105
  DidType: typeof DidType;
77
- DID_TYPE_FORGE: _Lib.T101;
78
- DID_TYPE_ETHEREUM: _Lib.T101;
106
+ DID_TYPE_ARCBLOCK: typeof DID_TYPE_ARCBLOCK;
107
+ DID_TYPE_ETHEREUM: typeof DID_TYPE_ETHEREUM;
79
108
  }
80
109
  }
81
110
  export = _Lib;
package/lib/index.js CHANGED
@@ -13,8 +13,8 @@ const {
13
13
  toTypeInfo,
14
14
  fromTypeInfo,
15
15
  isEthereumType,
16
- isEthereumAddress,
17
- DID_TYPE_FORGE,
16
+ isEthereumDid,
17
+ DID_TYPE_ARCBLOCK,
18
18
  DID_TYPE_ETHEREUM,
19
19
  toChecksumAddress,
20
20
  } = require('./type');
@@ -137,19 +137,22 @@ const isFromPublicKey = (did, pk) => {
137
137
  * @returns {boolean}
138
138
  */
139
139
  const isValid = (did) => {
140
- const { hash } = toTypeInfo(did);
140
+ const address = toAddress(did);
141
+ const { hash } = toTypeInfo(address);
142
+
141
143
  if (typeof hash === 'undefined') {
142
144
  return false;
143
145
  }
144
146
 
145
- if (isEthereumAddress(did)) {
147
+ if (isEthereumDid(address)) {
146
148
  return true;
147
149
  }
148
150
 
149
151
  const hashFn = getHasher(hash);
150
- const bytes = toBytes(did);
152
+ const bytes = toBytes(address);
151
153
  const bytesHex = toStrictHex(Buffer.from(bytes.slice(0, 22)).toString('hex'));
152
154
  const didChecksum = toStrictHex(Buffer.from(bytes.slice(22, 26)).toString('hex'));
155
+
153
156
  const checksum = stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
154
157
 
155
158
  return didChecksum === checksum;
@@ -170,9 +173,12 @@ module.exports = {
170
173
  isFromPublicKey,
171
174
  isValid,
172
175
 
176
+ isEthereumDid,
177
+ isEthereumType,
178
+
173
179
  toTypeInfo,
174
180
  fromTypeInfo,
175
181
  DidType,
176
- DID_TYPE_FORGE,
182
+ DID_TYPE_ARCBLOCK,
177
183
  DID_TYPE_ETHEREUM,
178
184
  };
package/lib/type.d.ts ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The structure of a forge wallet type
3
+ */
4
+ export type DidType = {
5
+ /**
6
+ * - Enum field to identify wallet role type
7
+ */
8
+ role: number;
9
+ /**
10
+ * - Crypto algorithm to derive publicKey from the secretKey
11
+ */
12
+ pk: number;
13
+ /**
14
+ * - Hash algorithm used to hash data before sign them
15
+ */
16
+ hash: number;
17
+ };
18
+ export namespace DID_TYPE_ARCBLOCK {
19
+ const role: any;
20
+ const pk: any;
21
+ const hash: any;
22
+ const address: any;
23
+ }
24
+ export namespace DID_TYPE_ETHEREUM {
25
+ const role_1: any;
26
+ export { role_1 as role };
27
+ const pk_1: any;
28
+ export { pk_1 as pk };
29
+ const hash_1: any;
30
+ export { hash_1 as hash };
31
+ const address_1: any;
32
+ export { address_1 as address };
33
+ }
34
+ /**
35
+ * Convert type info object to hex string
36
+ *
37
+ * @public
38
+ * @static
39
+ * @param {object} type - wallet type, {@see @arcblock/did#DidType}
40
+ * @returns string
41
+ */
42
+ export function fromTypeInfo(type: object): string;
43
+ /**
44
+ * Get type info from did (base58 format)
45
+ *
46
+ * @public
47
+ * @static
48
+ * @param {string} did - address string
49
+ * @param {boolean} [returnString=true]
50
+ * @returns {object} wallet type {@see @arcblock/did#DidType}
51
+ */
52
+ export function toTypeInfo(did: string, returnString?: boolean): object;
53
+ export function isEthereumType(type: any): any;
54
+ /**
55
+ * Checks if the given string is an address
56
+ *
57
+ * @method isEthereumDid
58
+ * @param {String} address the given HEX address
59
+ * @return {Boolean}
60
+ */
61
+ export function isEthereumDid(did: any): boolean;
62
+ /**
63
+ * Converts to a checksum address
64
+ *
65
+ * @method toChecksumAddress
66
+ * @param {String} address the given HEX address
67
+ * @return {String}
68
+ */
69
+ export function toChecksumAddress(address: string): string;
70
+ /**
71
+ * The structure of a forge wallet type
72
+ *
73
+ * @public
74
+ * @static
75
+ * @global
76
+ * @typedef {Object} DidType
77
+ * @prop {number} role - Enum field to identify wallet role type
78
+ * @prop {number} pk - Crypto algorithm to derive publicKey from the secretKey
79
+ * @prop {number} hash - Hash algorithm used to hash data before sign them
80
+ */
81
+ /**
82
+ * Create an wallet type object that be used as param to create a new wallet
83
+ *
84
+ * @public
85
+ * @static
86
+ * @param {DidType|string} [type='default']
87
+ * @returns {object}
88
+ * @example
89
+ * const assert = require('assert');
90
+ * const { DidType } = require('@arcblock/did');
91
+ * const { types } = require('@ocap/mcrypto');
92
+ *
93
+ * const type = DidType({
94
+ * role: types.RoleType.ROLE_APPLICATION,
95
+ * pk: types.KeyType.ED25519,
96
+ * hash: types.HashType.SHA3,
97
+ * });
98
+ */
99
+ export function DidType(type?: DidType | string): object;
100
+ export namespace DidType {
101
+ function toJSON(type: any): {};
102
+ function fromJSON(json: any): {};
103
+ }
package/lib/type.js CHANGED
@@ -1,9 +1,9 @@
1
- const BN = require('bn.js');
1
+ const { BN } = require('@ocap/util');
2
2
  const upperFirst = require('lodash/upperFirst');
3
3
  const isEqual = require('lodash/isEqual');
4
4
  const pick = require('lodash/pick');
5
5
  const { types, Hasher } = require('@ocap/mcrypto');
6
- const { numberToHex, stripHexPrefix } = require('@ocap/util');
6
+ const { numberToHex, stripHexPrefix, toAddress, isHex } = require('@ocap/util');
7
7
  const { toBits, toBytes, toStrictHex } = require('./util');
8
8
 
9
9
  // eslint-disable-next-line
@@ -14,7 +14,7 @@ const mapping = {
14
14
  address: 'encoding',
15
15
  };
16
16
 
17
- const DID_TYPE_FORGE = {
17
+ const DID_TYPE_ARCBLOCK = {
18
18
  role: types.RoleType.ROLE_ACCOUNT,
19
19
  pk: types.KeyType.ED25519,
20
20
  hash: types.HashType.SHA3,
@@ -36,11 +36,13 @@ const isEthereumType = (type) => {
36
36
  /**
37
37
  * Checks if the given string is an address
38
38
  *
39
- * @method isEthereumAddress
39
+ * @method isEthereumDid
40
40
  * @param {String} address the given HEX address
41
41
  * @return {Boolean}
42
42
  */
43
- const isEthereumAddress = (address) => {
43
+ const isEthereumDid = (did) => {
44
+ const address = toAddress(did);
45
+
44
46
  // check if it has the basic requirements of an address
45
47
  if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
46
48
  return false;
@@ -144,11 +146,11 @@ const toChecksumAddress = (address) => {
144
146
  function DidType(type = 'default') {
145
147
  let input = null;
146
148
  if (type === 'default' || type === 'forge') {
147
- input = DID_TYPE_FORGE;
149
+ input = DID_TYPE_ARCBLOCK;
148
150
  } else if (type === 'eth') {
149
151
  input = DID_TYPE_ETHEREUM;
150
152
  } else {
151
- input = { ...DID_TYPE_FORGE, ...type };
153
+ input = { ...DID_TYPE_ARCBLOCK, ...type };
152
154
  }
153
155
 
154
156
  const { role, pk, hash, address } = input;
@@ -227,7 +229,7 @@ const fromTypeInfo = (type) => {
227
229
  const toTypeInfo = (did, returnString = false) => {
228
230
  try {
229
231
  let type = null;
230
- if (isEthereumAddress(did)) {
232
+ if (isEthereumDid(did)) {
231
233
  type = DID_TYPE_ETHEREUM;
232
234
  } else {
233
235
  const bytes = toBytes(did);
@@ -241,6 +243,7 @@ const toTypeInfo = (did, returnString = false) => {
241
243
  role: parseInt(roleBits, 2),
242
244
  pk: parseInt(keyBits, 2),
243
245
  hash: parseInt(hashBits, 2),
246
+ address: isHex(toAddress(did)) ? types.EncodingType.BASE16 : types.EncodingType.BASE58,
244
247
  };
245
248
  }
246
249
 
@@ -266,12 +269,12 @@ const toTypeInfo = (did, returnString = false) => {
266
269
  };
267
270
 
268
271
  module.exports = {
269
- DID_TYPE_FORGE,
272
+ DID_TYPE_ARCBLOCK,
270
273
  DID_TYPE_ETHEREUM,
271
274
  fromTypeInfo,
272
275
  toTypeInfo,
273
276
  isEthereumType,
274
- isEthereumAddress,
277
+ isEthereumDid,
275
278
  toChecksumAddress,
276
279
  DidType,
277
280
  };
package/lib/util.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ export const DID_PREFIX: "did:abt:";
2
+ /**
3
+ * Convert number to bit string with predefined length
4
+ *
5
+ * @param {string} decimal
6
+ * @param {number} length
7
+ * @returns String
8
+ */
9
+ export function toBits(decimal: string, length: number): any;
10
+ /**
11
+ * Convert did to bytes with length of 26
12
+ *
13
+ * @param {string} did
14
+ * @returns {Buffer}
15
+ */
16
+ export function toBytes(did: string): Buffer;
17
+ /**
18
+ * Ensure the hex length is even number, 2, 4, 6, 8
19
+ *
20
+ * @param {string} hex
21
+ * @param {number} length
22
+ * @returns {string} hex
23
+ */
24
+ export function toStrictHex(hex: string, length: number): string;
package/lib/util.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const padStart = require('lodash/padStart');
2
- const { toBN, fromBase58 } = require('@ocap/util');
2
+ const { toBN, fromBase58, isHexStrict, stripHexPrefix } = require('@ocap/util');
3
3
 
4
4
  const DID_PREFIX = 'did:abt:';
5
5
 
@@ -11,6 +11,10 @@ const DID_PREFIX = 'did:abt:';
11
11
  */
12
12
  const toBytes = (did) => {
13
13
  try {
14
+ if (isHexStrict(did)) {
15
+ return Buffer.from(stripHexPrefix(did), 'hex');
16
+ }
17
+
14
18
  let bytes = fromBase58(did.replace(DID_PREFIX, ''));
15
19
  while (bytes.length < 26) {
16
20
  bytes = Buffer.concat([Buffer.from([0]), bytes]);
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@arcblock/did",
3
- "version": "1.6.3",
3
+ "version": "1.6.10",
4
4
  "description": "Javascript lib to work with ArcBlock DID",
5
5
  "keywords": [
6
- "forge",
7
6
  "arcblock",
8
7
  "blockchain",
9
8
  "did",
@@ -17,39 +16,42 @@
17
16
  "email": "shijun@arcblock.io",
18
17
  "url": "https://github.com/wangshijun"
19
18
  },
20
- "homepage": "https://github.com/ArcBlock/ocap-js/tree/master/did/did",
19
+ "contributors": [
20
+ "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
21
+ ],
22
+ "homepage": "https://github.com/ArcBlock/asset-chain/tree/master/did/did",
21
23
  "license": "Apache-2.0",
22
24
  "main": "lib/index.js",
23
25
  "files": [
24
26
  "lib"
25
27
  ],
26
28
  "devDependencies": {
27
- "jest": "^24.8.0"
29
+ "jest": "^27.3.1"
28
30
  },
29
31
  "repository": {
30
32
  "type": "git",
31
- "url": "git+https://github.com/ArcBlock/ocap-js.git"
33
+ "url": "git+https://github.com/ArcBlock/asset-chain.git"
32
34
  },
33
35
  "scripts": {
34
36
  "lint": "eslint tests lib",
35
37
  "lint:fix": "eslint --fix tests lib",
36
38
  "docs": "yarn gen-dts && yarn gen-docs && yarn cleanup-docs && yarn format-docs",
37
- "cleanup-docs": "node ../../tools/cleanup-docs.js docs/README.md $npm_package_name",
39
+ "cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
38
40
  "gen-docs": "jsdoc2md lib/index.js > docs/README.md",
39
41
  "gen-dts": "j2d lib/index.js",
40
42
  "format-docs": "remark . -o",
41
- "test": "node tools/jest.js",
43
+ "test": "jest --forceExit --detectOpenHandles",
42
44
  "coverage": "yarn test -- --coverage"
43
45
  },
44
46
  "bugs": {
45
- "url": "https://github.com/ArcBlock/ocap-js/issues"
47
+ "url": "https://github.com/ArcBlock/asset-chain/issues"
46
48
  },
47
49
  "dependencies": {
48
- "@ocap/mcrypto": "^1.6.3",
49
- "@ocap/util": "^1.6.3",
50
- "bn.js": "^5.1.3",
51
- "debug": "^4.3.1",
52
- "lodash": "^4.17.20"
50
+ "@ocap/mcrypto": "1.6.10",
51
+ "@ocap/util": "1.6.10",
52
+ "bn.js": "^5.2.0",
53
+ "debug": "^4.3.3",
54
+ "lodash": "^4.17.21"
53
55
  },
54
- "gitHead": "5246168dbbc268a3e6f95c3992d9d343647ca25b"
56
+ "gitHead": "ab272e8db3a15c6571cc7fae7cc3d3e0fdd4bdb1"
55
57
  }