@arcblock/did 1.27.16 → 1.28.1
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 +9 -5
- package/esm/index.js +78 -106
- package/esm/lodash.d.ts +4 -0
- package/esm/type.d.ts +13 -14
- package/esm/type.js +143 -194
- package/esm/util.d.ts +6 -3
- package/esm/util.js +33 -38
- package/lib/_virtual/rolldown_runtime.js +29 -0
- package/lib/index.d.ts +9 -5
- package/lib/index.js +98 -128
- package/lib/lodash.d.ts +4 -0
- package/lib/type.d.ts +13 -14
- package/lib/type.js +156 -209
- package/lib/util.d.ts +6 -3
- package/lib/util.js +38 -47
- package/package.json +21 -10
package/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { stripHexPrefix, toAddress, toBase58, toDid } from
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
34
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
89
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
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 };
|
package/esm/lodash.d.ts
ADDED
package/esm/type.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
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) =>
|
|
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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
import { BN, numberToHex, stripHexPrefix, toAddress
|
|
4
|
-
import upperFirst from
|
|
5
|
-
import isEqual from
|
|
6
|
-
import pick from
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
10
|
+
pk: "key",
|
|
11
|
+
address: "encoding"
|
|
12
12
|
};
|
|
13
13
|
const DID_TYPE_ARCBLOCK = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
105
|
-
|
|
83
|
+
* Create an wallet type object that be used as param to create a new wallet
|
|
84
|
+
*/
|
|
106
85
|
function DidType(type = {}) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
182
|
-
|
|
140
|
+
* Get type info from did
|
|
141
|
+
*/
|
|
183
142
|
const toTypeInfo = (did) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
export { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr };
|