@arcblock/did 1.27.16 → 1.28.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/esm/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
- import { types } from '@ocap/mcrypto';
2
- import { toAddress, toDid, BytesType } from '@ocap/util';
3
- import { DidType, DIDType, DIDTypeShortcut, DIDTypeStr, DIDTypeArg, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, fromTypeInfo, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr } from './type';
4
- import { DID_PREFIX, toStrictHex } from './util';
1
+ import { DIDType, DIDTypeArg, DIDTypeShortcut, DIDTypeStr, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr } from "./type.js";
2
+ import { DID_PREFIX, toStrictHex } from "./util.js";
3
+ import { types } from "@ocap/mcrypto";
4
+ import { BytesType, toAddress, toDid } from "@ocap/util";
5
+
6
+ //#region src/index.d.ts
7
+
5
8
  /**
6
9
  * Gen DID from private key and type config
7
10
  *
@@ -54,4 +57,5 @@ declare const isFromPublicKey: (did: string, pk: BytesType) => boolean;
54
57
  * @returns {boolean}
55
58
  */
56
59
  declare const isValid: (did: string) => boolean;
57
- export { DIDType, DIDTypeStr, DIDTypeArg, DIDTypeShortcut, types, toStrictHex, fromSecretKey, fromPublicKey, fromPublicKeyHash, fromHash, toAddress, toDid, isFromPublicKey, isValid, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr, fromTypeInfo, DidType, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, };
60
+ //#endregion
61
+ export { type DIDType, type DIDTypeArg, type DIDTypeShortcut, type DIDTypeStr, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, isEthereumDid, isEthereumType, isFromPublicKey, isValid, toAddress, toDid, toStrictHex, toTypeInfo, toTypeInfoStr, types };
package/esm/index.js CHANGED
@@ -1,123 +1,95 @@
1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
- /* eslint-disable @typescript-eslint/ban-ts-comment */
3
- import { types, getSigner, getHasher } from '@ocap/mcrypto';
4
- import { stripHexPrefix, toAddress, toBase58, toDid } from '@ocap/util';
5
- import { DidType, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr, } from './type';
6
- import { DID_PREFIX, toBytes, toStrictHex } from './util';
1
+ import { DID_PREFIX, toBytes, toStrictHex } from "./util.js";
2
+ import { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr } from "./type.js";
3
+ import { getHasher, getSigner, types } from "@ocap/mcrypto";
4
+ import { stripHexPrefix, toAddress, toBase58, toDid } from "@ocap/util";
5
+
6
+ //#region src/index.ts
7
7
  /**
8
- * Gen DID from private key and type config
9
- *
10
- * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#create-did
11
- *
12
- * @public
13
- * @static
14
- * @param {string} sk - hex encoded secret key string
15
- * @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
16
- * @returns {string} DID string
17
- */
8
+ * Gen DID from private key and type config
9
+ *
10
+ * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#create-did
11
+ *
12
+ * @public
13
+ * @static
14
+ * @param {string} sk - hex encoded secret key string
15
+ * @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
16
+ * @returns {string} DID string
17
+ */
18
18
  const fromSecretKey = (sk, type) => {
19
- const info = DidType(type);
20
- const pub = getSigner(info.pk).getPublicKey(sk);
21
- return fromPublicKey(pub.indexOf('0x') === 0 ? pub : `0x${pub}`, info);
19
+ const info = DidType(type);
20
+ const pub = getSigner(info.pk).getPublicKey(sk);
21
+ return fromPublicKey(pub.indexOf("0x") === 0 ? pub : `0x${pub}`, info);
22
22
  };
23
23
  /**
24
- * Gen DID from public key and type config
25
- *
26
- * @public
27
- * @static
28
- * @param {string} pk - hex encoded public key
29
- * @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
30
- * @returns {string} DID string
31
- */
24
+ * Gen DID from public key and type config
25
+ *
26
+ * @public
27
+ * @static
28
+ * @param {string} pk - hex encoded public key
29
+ * @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
30
+ * @returns {string} DID string
31
+ */
32
32
  const fromPublicKey = (pk, type) => {
33
- const info = DidType(type);
34
- const hashFn = getHasher(info.hash);
35
- const pkHash = hashFn(pk, 1);
36
- return fromPublicKeyHash(pkHash, info);
33
+ const info = DidType(type);
34
+ return fromPublicKeyHash(getHasher(info.hash)(pk, 1), info);
37
35
  };
38
36
  const fromPublicKeyHash = (buffer, type) => {
39
- const info = DidType(type);
40
- const pkHash = stripHexPrefix(buffer).slice(0, 40); // 20 bytes
41
- const hashFn = getHasher(info.hash);
42
- const typeHex = fromTypeInfo(info);
43
- const checksum = stripHexPrefix(hashFn(`0x${typeHex}${pkHash}`, 1)).slice(0, 8); // 4 bytes
44
- const didHash = `0x${typeHex}${pkHash}${checksum}`;
45
- // ethereum-compatible address, this address does not contain any type info
46
- // but we can infer from the address itself
47
- if (isEthereumType(info)) {
48
- return toChecksumAddress(`0x${buffer.slice(-40)}`);
49
- }
50
- // default forge-compatible did
51
- if (info.address === types.EncodingType.BASE58) {
52
- return toBase58(didHash);
53
- }
54
- // fallback base16 encoding
55
- return didHash;
37
+ const info = DidType(type);
38
+ const pkHash = stripHexPrefix(buffer).slice(0, 40);
39
+ const hashFn = getHasher(info.hash);
40
+ const typeHex = fromTypeInfo(info);
41
+ const didHash = `0x${typeHex}${pkHash}${stripHexPrefix(hashFn(`0x${typeHex}${pkHash}`, 1)).slice(0, 8)}`;
42
+ if (isEthereumType(info)) return toChecksumAddress(`0x${buffer.slice(-40)}`);
43
+ if (info.address === types.EncodingType.BASE58) return toBase58(didHash);
44
+ return didHash;
56
45
  };
57
46
  /**
58
- * Gen DID from an hex encoded hash and role type
59
- *
60
- * @public
61
- * @static
62
- * @param {string} hash - hex encoded hash
63
- * @param {enum} role - role type, {@see @ocap/mcrypto#types}
64
- * @returns {string} DID string
65
- */
47
+ * Gen DID from an hex encoded hash and role type
48
+ *
49
+ * @public
50
+ * @static
51
+ * @param {string} hash - hex encoded hash
52
+ * @param {enum} role - role type, {@see @ocap/mcrypto#types}
53
+ * @returns {string} DID string
54
+ */
66
55
  const fromHash = (hash, role = types.RoleType.ROLE_ACCOUNT) => {
67
- const roleKeys = Object.keys(types.RoleType);
68
- const roleValues = Object.values(types.RoleType);
69
- if (roleValues.indexOf(role) === -1) {
70
- throw new Error(`Unsupported role type ${role} when gen ddi from hash`);
71
- }
72
- const type = DidType({
73
- // @ts-ignore
74
- role: types.RoleType[roleKeys[roleValues.indexOf(role)]],
75
- });
76
- return fromPublicKeyHash(hash, type);
56
+ const roleKeys = Object.keys(types.RoleType);
57
+ const roleValues = Object.values(types.RoleType);
58
+ if (roleValues.indexOf(role) === -1) throw new Error(`Unsupported role type ${role} when gen ddi from hash`);
59
+ return fromPublicKeyHash(hash, DidType({ role: types.RoleType[roleKeys[roleValues.indexOf(role)]] }));
77
60
  };
78
61
  /**
79
- * Check if an DID is generated from a publicKey
80
- *
81
- * @public
82
- * @static
83
- * @param {string} did - string of the did, usually base58btc format
84
- * @param {string} pk - hex encoded publicKey string
85
- * @returns {boolean}
86
- */
62
+ * Check if an DID is generated from a publicKey
63
+ *
64
+ * @public
65
+ * @static
66
+ * @param {string} did - string of the did, usually base58btc format
67
+ * @param {string} pk - hex encoded publicKey string
68
+ * @returns {boolean}
69
+ */
87
70
  const isFromPublicKey = (did, pk) => {
88
- if (isValid(did) === false) {
89
- return false;
90
- }
91
- const type = toTypeInfo(did);
92
- const didNew = fromPublicKey(pk, type);
93
- const didClean = did.replace(DID_PREFIX, '');
94
- return didNew === didClean;
71
+ if (isValid(did) === false) return false;
72
+ return fromPublicKey(pk, toTypeInfo(did)) === did.replace(DID_PREFIX, "");
95
73
  };
96
74
  /**
97
- * Check if a DID string is valid
98
- *
99
- * @public
100
- * @static
101
- * @param {string} did - address string
102
- * @returns {boolean}
103
- */
75
+ * Check if a DID string is valid
76
+ *
77
+ * @public
78
+ * @static
79
+ * @param {string} did - address string
80
+ * @returns {boolean}
81
+ */
104
82
  const isValid = (did) => {
105
- if (!did) {
106
- return false;
107
- }
108
- const address = toAddress(String(did));
109
- const { hash } = toTypeInfo(address);
110
- if (typeof hash === 'undefined') {
111
- return false;
112
- }
113
- if (isEthereumDid(address)) {
114
- return true;
115
- }
116
- const hashFn = getHasher(hash);
117
- const bytes = toBytes(address);
118
- const bytesHex = toStrictHex(Buffer.from(bytes.slice(0, 22)).toString('hex'));
119
- const didChecksum = toStrictHex(Buffer.from(bytes.slice(22, 26)).toString('hex'));
120
- const checksum = stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
121
- return didChecksum === checksum;
83
+ if (!did) return false;
84
+ const address = toAddress(String(did));
85
+ const { hash } = toTypeInfo(address);
86
+ if (typeof hash === "undefined") return false;
87
+ if (isEthereumDid(address)) return true;
88
+ const hashFn = getHasher(hash);
89
+ const bytes = toBytes(address);
90
+ const bytesHex = toStrictHex(Buffer.from(bytes.slice(0, 22)).toString("hex"));
91
+ return toStrictHex(Buffer.from(bytes.slice(22, 26)).toString("hex")) === stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
122
92
  };
123
- export { types, toStrictHex, fromSecretKey, fromPublicKey, fromPublicKeyHash, fromHash, toAddress, toDid, isFromPublicKey, isValid, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr, fromTypeInfo, DidType, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, };
93
+
94
+ //#endregion
95
+ export { DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, isEthereumDid, isEthereumType, isFromPublicKey, isValid, toAddress, toDid, toStrictHex, toTypeInfo, toTypeInfoStr, types };
@@ -0,0 +1,5 @@
1
+ //#region src/lodash.d.ts
2
+ declare module 'lodash/isEqual';
3
+ declare module 'lodash/pick';
4
+ declare module 'lodash/padStart';
5
+ declare module 'lodash/upperFirst';
package/esm/type.d.ts CHANGED
@@ -1,17 +1,15 @@
1
- import type { LiteralUnion } from 'type-fest';
2
- export type DIDTypeKey = LiteralUnion<'role' | 'pk' | 'hash' | 'address', string>;
3
- export type DIDTypeShortcut = LiteralUnion<'default' | 'arcblock' | 'eth' | 'ethereum' | 'passkey', string>;
4
- export type DIDType = {
5
- [key in DIDTypeKey]?: number;
6
- };
7
- export type DIDTypeStr = {
8
- [key in DIDTypeKey]?: string;
9
- };
10
- export type DIDTypeArg = DIDTypeShortcut | DIDType;
1
+ import { LiteralUnion } from "type-fest";
2
+
3
+ //#region src/type.d.ts
4
+ type DIDTypeKey = LiteralUnion<'role' | 'pk' | 'hash' | 'address', string>;
5
+ type DIDTypeShortcut = LiteralUnion<'default' | 'arcblock' | 'eth' | 'ethereum' | 'passkey', string>;
6
+ type DIDType = { [key in DIDTypeKey]?: number };
7
+ type DIDTypeStr = { [key in DIDTypeKey]?: string };
8
+ type DIDTypeArg = DIDTypeShortcut | DIDType;
11
9
  declare const DID_TYPE_ARCBLOCK: DIDType;
12
10
  declare const DID_TYPE_PASSKEY: DIDType;
13
11
  declare const DID_TYPE_ETHEREUM: DIDType;
14
- declare const isEthereumType: (type: DIDType) => any;
12
+ declare const isEthereumType: (type: DIDType) => boolean;
15
13
  /**
16
14
  * Checks if the given string is an address
17
15
  *
@@ -33,8 +31,8 @@ declare const toChecksumAddress: (address: string) => string;
33
31
  */
34
32
  declare function DidType(type?: DIDTypeArg): DIDType;
35
33
  declare namespace DidType {
36
- var toJSON: (type: DIDType) => DIDTypeStr;
37
- var fromJSON: (json: DIDTypeStr) => DIDType;
34
+ var toJSON: (type: DIDType) => DIDTypeStr;
35
+ var fromJSON: (json: DIDTypeStr) => DIDType;
38
36
  }
39
37
  /**
40
38
  * Convert type info object to hex string
@@ -50,4 +48,5 @@ declare const fromTypeInfo: (type: DIDTypeArg) => string;
50
48
  */
51
49
  declare const toTypeInfo: (did: string) => DIDType;
52
50
  declare const toTypeInfoStr: (did: string) => DIDTypeStr;
53
- export { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, fromTypeInfo, toTypeInfo, toTypeInfoStr, isEthereumType, isEthereumDid, toChecksumAddress, DidType, };
51
+ //#endregion
52
+ export { DIDType, DIDTypeArg, DIDTypeKey, DIDTypeShortcut, DIDTypeStr, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr };
package/esm/type.js CHANGED
@@ -1,228 +1,177 @@
1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
- /* eslint-disable @typescript-eslint/ban-ts-comment */
3
- import { BN, numberToHex, stripHexPrefix, toAddress, isHex } from '@ocap/util';
4
- import upperFirst from 'lodash/upperFirst';
5
- import isEqual from 'lodash/isEqual';
6
- import pick from 'lodash/pick';
7
- import { types, Hasher } from '@ocap/mcrypto';
8
- import { toBits, toBytes, toStrictHex } from './util';
1
+ import { toBits, toBytes, toStrictHex } from "./util.js";
2
+ import { Hasher, types } from "@ocap/mcrypto";
3
+ import { BN, isHex, numberToHex, stripHexPrefix, toAddress } from "@ocap/util";
4
+ import upperFirst from "lodash/upperFirst.js";
5
+ import isEqual from "lodash/isEqual.js";
6
+ import pick from "lodash/pick.js";
7
+
8
+ //#region src/type.ts
9
9
  const mapping = {
10
- pk: 'key',
11
- address: 'encoding',
10
+ pk: "key",
11
+ address: "encoding"
12
12
  };
13
13
  const DID_TYPE_ARCBLOCK = {
14
- role: types.RoleType.ROLE_ACCOUNT,
15
- pk: types.KeyType.ED25519,
16
- hash: types.HashType.SHA3,
17
- address: types.EncodingType.BASE58,
14
+ role: types.RoleType.ROLE_ACCOUNT,
15
+ pk: types.KeyType.ED25519,
16
+ hash: types.HashType.SHA3,
17
+ address: types.EncodingType.BASE58
18
18
  };
19
19
  const DID_TYPE_PASSKEY = {
20
- role: types.RoleType.ROLE_PASSKEY,
21
- pk: types.KeyType.PASSKEY,
22
- hash: types.HashType.SHA2,
23
- address: types.EncodingType.BASE58,
20
+ role: types.RoleType.ROLE_PASSKEY,
21
+ pk: types.KeyType.PASSKEY,
22
+ hash: types.HashType.SHA2,
23
+ address: types.EncodingType.BASE58
24
24
  };
25
25
  const DID_TYPE_ETHEREUM = {
26
- role: types.RoleType.ROLE_ACCOUNT,
27
- pk: types.KeyType.ETHEREUM,
28
- hash: types.HashType.KECCAK,
29
- address: types.EncodingType.BASE16,
26
+ role: types.RoleType.ROLE_ACCOUNT,
27
+ pk: types.KeyType.ETHEREUM,
28
+ hash: types.HashType.KECCAK,
29
+ address: types.EncodingType.BASE16
30
30
  };
31
31
  const isEthereumType = (type) => {
32
- const props = ['pk', 'hash', 'address'];
33
- return isEqual(pick(type, props), pick(DID_TYPE_ETHEREUM, props));
32
+ const props = [
33
+ "pk",
34
+ "hash",
35
+ "address"
36
+ ];
37
+ return isEqual(pick(type, props), pick(DID_TYPE_ETHEREUM, props));
34
38
  };
35
39
  /**
36
- * Checks if the given string is an address
37
- *
38
- * @method isEthereumDid
39
- * @param {String} address the given HEX address
40
- * @return {Boolean}
41
- */
40
+ * Checks if the given string is an address
41
+ *
42
+ * @method isEthereumDid
43
+ * @param {String} address the given HEX address
44
+ * @return {Boolean}
45
+ */
42
46
  const isEthereumDid = (did) => {
43
- const address = toAddress(did);
44
- // check if it has the basic requirements of an address
45
- if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
46
- return false;
47
- }
48
- // If it's ALL lowercase or ALL upppercase
49
- if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
50
- return true;
51
- }
52
- // Otherwise check each case
53
- return checkAddressChecksum(address);
47
+ const address = toAddress(did);
48
+ if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) return false;
49
+ if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) return true;
50
+ return checkAddressChecksum(address);
54
51
  };
55
52
  /**
56
- * Checks if the given string is a checksummed address
57
- *
58
- * @method checkAddressChecksum
59
- * @param {String} address the given HEX address
60
- * @return {Boolean}
61
- */
53
+ * Checks if the given string is a checksummed address
54
+ *
55
+ * @method checkAddressChecksum
56
+ * @param {String} address the given HEX address
57
+ * @return {Boolean}
58
+ */
62
59
  const checkAddressChecksum = (address) => {
63
- // Check each case
64
- const origin = address.replace(/^0x/i, '');
65
- const addressHash = Hasher.Keccak.hash256(origin.toLowerCase()).replace(/^0x/i, '');
66
- for (let i = 0; i < 40; i++) {
67
- // the nth letter should be uppercase if the nth digit of casemap is 1
68
- if ((parseInt(addressHash[i], 16) > 7 && origin[i].toUpperCase() !== origin[i]) ||
69
- (parseInt(addressHash[i], 16) <= 7 && origin[i].toLowerCase() !== origin[i])) {
70
- return false;
71
- }
72
- }
73
- return true;
60
+ const origin = address.replace(/^0x/i, "");
61
+ const addressHash = Hasher.Keccak.hash256(origin.toLowerCase()).replace(/^0x/i, "");
62
+ for (let i = 0; i < 40; i++) if (parseInt(addressHash[i], 16) > 7 && origin[i].toUpperCase() !== origin[i] || parseInt(addressHash[i], 16) <= 7 && origin[i].toLowerCase() !== origin[i]) return false;
63
+ return true;
74
64
  };
75
65
  /**
76
- * Converts to a checksum address
77
- *
78
- * @method toChecksumAddress
79
- * @param {String} address the given HEX address
80
- * @return {String}
81
- */
66
+ * Converts to a checksum address
67
+ *
68
+ * @method toChecksumAddress
69
+ * @param {String} address the given HEX address
70
+ * @return {String}
71
+ */
82
72
  const toChecksumAddress = (address) => {
83
- if (typeof address === 'undefined') {
84
- return '';
85
- }
86
- if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
87
- throw new Error(`Given address "${address}" is not a valid Ethereum address.`);
88
- }
89
- const lower = address.toLowerCase().replace(/^0x/i, '');
90
- const addressHash = Hasher.Keccak.hash256(lower).replace(/^0x/i, '');
91
- let checksumAddress = '0x';
92
- for (let i = 0; i < lower.length; i++) {
93
- // If ith character is 8 to f then make it uppercase
94
- if (parseInt(addressHash[i], 16) > 7) {
95
- checksumAddress += lower[i].toUpperCase();
96
- }
97
- else {
98
- checksumAddress += lower[i];
99
- }
100
- }
101
- return checksumAddress;
73
+ if (typeof address === "undefined") return "";
74
+ if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);
75
+ const lower = address.toLowerCase().replace(/^0x/i, "");
76
+ const addressHash = Hasher.Keccak.hash256(lower).replace(/^0x/i, "");
77
+ let checksumAddress = "0x";
78
+ for (let i = 0; i < lower.length; i++) if (parseInt(addressHash[i], 16) > 7) checksumAddress += lower[i].toUpperCase();
79
+ else checksumAddress += lower[i];
80
+ return checksumAddress;
102
81
  };
103
82
  /**
104
- * Create an wallet type object that be used as param to create a new wallet
105
- */
83
+ * Create an wallet type object that be used as param to create a new wallet
84
+ */
106
85
  function DidType(type = {}) {
107
- let input = {};
108
- if (['default', 'arcblock'].includes(type)) {
109
- input = DID_TYPE_ARCBLOCK;
110
- }
111
- else if (['eth', 'ethereum'].includes(type)) {
112
- input = DID_TYPE_ETHEREUM;
113
- }
114
- else if (['passkey'].includes(type)) {
115
- input = DID_TYPE_PASSKEY;
116
- }
117
- else {
118
- input = { ...DID_TYPE_ARCBLOCK, ...type };
119
- }
120
- const { role, pk, hash, address } = input;
121
- Object.keys(input).forEach((x) => {
122
- const key = upperFirst(`${mapping[x] || x}Type`);
123
- // @ts-ignore
124
- if (Object.values(types[key]).includes(input[x]) === false) {
125
- throw new Error(`Unsupported ${x} type: ${input[x]}`);
126
- }
127
- });
128
- const output = { role, pk, hash, address };
129
- // Overwrite hash methods for some role type if we are not eth-type
130
- if (isEthereumType(output) === false) {
131
- const sha2Roles = [
132
- types.RoleType.ROLE_NODE,
133
- types.RoleType.ROLE_VALIDATOR,
134
- types.RoleType.ROLE_TETHER,
135
- types.RoleType.ROLE_SWAP,
136
- ];
137
- if (sha2Roles.includes(output.role)) {
138
- output.hash = types.HashType.SHA2;
139
- }
140
- }
141
- return output;
86
+ let input = {};
87
+ if (["default", "arcblock"].includes(type)) input = DID_TYPE_ARCBLOCK;
88
+ else if (["eth", "ethereum"].includes(type)) input = DID_TYPE_ETHEREUM;
89
+ else if (["passkey"].includes(type)) input = DID_TYPE_PASSKEY;
90
+ else input = {
91
+ ...DID_TYPE_ARCBLOCK,
92
+ ...type
93
+ };
94
+ const { role, pk, hash, address } = input;
95
+ Object.keys(input).forEach((x) => {
96
+ const key = upperFirst(`${mapping[x] || x}Type`);
97
+ if (Object.values(types[key]).includes(input[x]) === false) throw new Error(`Unsupported ${x} type: ${input[x]}`);
98
+ });
99
+ const output = {
100
+ role,
101
+ pk,
102
+ hash,
103
+ address
104
+ };
105
+ if (isEthereumType(output) === false) {
106
+ if ([
107
+ types.RoleType.ROLE_NODE,
108
+ types.RoleType.ROLE_VALIDATOR,
109
+ types.RoleType.ROLE_TETHER,
110
+ types.RoleType.ROLE_SWAP
111
+ ].includes(output.role)) output.hash = types.HashType.SHA2;
112
+ }
113
+ return output;
142
114
  }
143
115
  DidType.toJSON = (type) => Object.keys(type).reduce((acc, x) => {
144
- const key = upperFirst(`${mapping[x] || x}Type`);
145
- // @ts-ignore
146
- const typeStr = Object.keys(types[key]);
147
- // @ts-ignore
148
- const typeValues = Object.values(types[key]);
149
- // @ts-ignore
150
- acc[x] = typeStr[typeValues.indexOf(type[x])];
151
- return acc;
116
+ const key = upperFirst(`${mapping[x] || x}Type`);
117
+ acc[x] = Object.keys(types[key])[Object.values(types[key]).indexOf(type[x])];
118
+ return acc;
152
119
  }, {});
153
120
  DidType.fromJSON = (json) => Object.keys(json).reduce((acc, x) => {
154
- const key = upperFirst(`${mapping[x] || x}Type`);
155
- // @ts-ignore
156
- const typeStr = Object.keys(types[key]);
157
- // @ts-ignore
158
- const typeValues = Object.values(types[key]);
159
- // @ts-ignore
160
- acc[x] = typeValues[typeStr.indexOf(json[x])];
161
- return acc;
121
+ const key = upperFirst(`${mapping[x] || x}Type`);
122
+ const typeStr = Object.keys(types[key]);
123
+ acc[x] = Object.values(types[key])[typeStr.indexOf(json[x])];
124
+ return acc;
162
125
  }, {});
163
126
  /**
164
- * Convert type info object to hex string
165
- *
166
- * @public
167
- * @static
168
- * @param {object} type - wallet type, {@see @arcblock/did#DidType}
169
- * @returns string
170
- */
127
+ * Convert type info object to hex string
128
+ *
129
+ * @public
130
+ * @static
131
+ * @param {object} type - wallet type, {@see @arcblock/did#DidType}
132
+ * @returns string
133
+ */
171
134
  const fromTypeInfo = (type) => {
172
- const info = DidType(type);
173
- const roleBits = toBits(info.role, 6);
174
- const keyBits = toBits(info.pk, 5);
175
- const hashBits = toBits(info.hash, 5);
176
- const infoBits = `${roleBits}${keyBits}${hashBits}`;
177
- const infoHex = stripHexPrefix(numberToHex(parseInt(infoBits, 2)));
178
- return toStrictHex(infoHex, 4);
135
+ const info = DidType(type);
136
+ const infoBits = `${toBits(info.role, 6)}${toBits(info.pk, 5)}${toBits(info.hash, 5)}`;
137
+ return toStrictHex(stripHexPrefix(numberToHex(parseInt(infoBits, 2))), 4);
179
138
  };
180
139
  /**
181
- * Get type info from did
182
- */
140
+ * Get type info from did
141
+ */
183
142
  const toTypeInfo = (did) => {
184
- let type = {};
185
- try {
186
- if (isEthereumDid(did)) {
187
- type = DID_TYPE_ETHEREUM;
188
- }
189
- else {
190
- const bytes = toBytes(did);
191
- const typeBytes = bytes.slice(0, 2);
192
- const typeHex = toStrictHex(Buffer.from(typeBytes).toString('hex'));
193
- const typeBits = toBits(new BN(typeHex, 16), 16);
194
- const roleBits = typeBits.slice(0, 6);
195
- const keyBits = typeBits.slice(6, 11);
196
- const hashBits = typeBits.slice(11, 16);
197
- type = {
198
- role: parseInt(roleBits, 2),
199
- pk: parseInt(keyBits, 2),
200
- hash: parseInt(hashBits, 2),
201
- address: isHex(toAddress(did)) ? types.EncodingType.BASE16 : types.EncodingType.BASE58,
202
- };
203
- }
204
- // remove unsupported types
205
- Object.keys(type).forEach((x) => {
206
- // @ts-ignore
207
- const enums = Object.values(types[`${upperFirst(mapping[x] || x)}Type`]);
208
- if (enums.includes(type[x]) === false) {
209
- delete type[x];
210
- }
211
- });
212
- return type;
213
- }
214
- catch {
215
- return type;
216
- }
143
+ let type = {};
144
+ try {
145
+ if (isEthereumDid(did)) type = DID_TYPE_ETHEREUM;
146
+ else {
147
+ const typeBytes = toBytes(did).slice(0, 2);
148
+ const typeBits = toBits(new BN(toStrictHex(Buffer.from(typeBytes).toString("hex")), 16), 16);
149
+ const roleBits = typeBits.slice(0, 6);
150
+ const keyBits = typeBits.slice(6, 11);
151
+ const hashBits = typeBits.slice(11, 16);
152
+ type = {
153
+ role: parseInt(roleBits, 2),
154
+ pk: parseInt(keyBits, 2),
155
+ hash: parseInt(hashBits, 2),
156
+ address: isHex(toAddress(did)) ? types.EncodingType.BASE16 : types.EncodingType.BASE58
157
+ };
158
+ }
159
+ Object.keys(type).forEach((x) => {
160
+ if (Object.values(types[`${upperFirst(mapping[x] || x)}Type`]).includes(type[x]) === false) delete type[x];
161
+ });
162
+ return type;
163
+ } catch {
164
+ return type;
165
+ }
217
166
  };
218
167
  const toTypeInfoStr = (did) => {
219
- const type = toTypeInfo(did);
220
- return Object.keys(type).reduce((acc, x) => {
221
- // @ts-ignore
222
- const enums = types[`${upperFirst(mapping[x] || x)}Type`];
223
- // @ts-ignore
224
- acc[x] = Object.keys(enums).find((d) => enums[d] === type[x]);
225
- return acc;
226
- }, {});
168
+ const type = toTypeInfo(did);
169
+ return Object.keys(type).reduce((acc, x) => {
170
+ const enums = types[`${upperFirst(mapping[x] || x)}Type`];
171
+ acc[x] = Object.keys(enums).find((d) => enums[d] === type[x]);
172
+ return acc;
173
+ }, {});
227
174
  };
228
- export { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, fromTypeInfo, toTypeInfo, toTypeInfoStr, isEthereumType, isEthereumDid, toChecksumAddress, DidType, };
175
+
176
+ //#endregion
177
+ export { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr };