@arcblock/did 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 +31 -84
- package/lib/index.js +83 -127
- package/lib/type.d.ts +35 -81
- package/lib/type.js +175 -218
- package/lib/util.d.ts +10 -11
- package/lib/util.js +31 -37
- package/package.json +21 -15
package/lib/index.d.ts
CHANGED
|
@@ -1,110 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { types } from '@ocap/mcrypto';
|
|
2
|
+
import { toAddress, toDid, BytesType } from '@ocap/util';
|
|
3
|
+
import { DidType, DIDType, DIDTypeStr, DIDTypeArg, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, fromTypeInfo, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr } from './type';
|
|
4
|
+
import { toStrictHex } from './util';
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
6
|
+
* Gen DID from private key and type config
|
|
5
7
|
*
|
|
6
|
-
*
|
|
7
|
-
* @param {number} length
|
|
8
|
-
* @returns {string} hex
|
|
9
|
-
*/
|
|
10
|
-
declare function toStrictHex(hex: string, length: number): string;
|
|
11
|
-
/**
|
|
12
|
-
* Convert did to address: remove `did:abt:` prefix
|
|
8
|
+
* Spec: https://github.com/ArcBlock/ABT-DID-Protocol#create-did
|
|
13
9
|
*
|
|
14
10
|
* @public
|
|
15
11
|
* @static
|
|
16
|
-
* @param {string}
|
|
17
|
-
* @
|
|
12
|
+
* @param {string} sk - hex encoded secret key string
|
|
13
|
+
* @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
|
|
14
|
+
* @returns {string} DID string
|
|
18
15
|
*/
|
|
19
|
-
declare
|
|
16
|
+
declare const fromSecretKey: (sk: BytesType, type?: DIDTypeArg) => string;
|
|
20
17
|
/**
|
|
21
|
-
*
|
|
18
|
+
* Gen DID from public key and type config
|
|
22
19
|
*
|
|
23
20
|
* @public
|
|
24
21
|
* @static
|
|
25
|
-
* @param {string}
|
|
26
|
-
* @
|
|
22
|
+
* @param {string} pk - hex encoded public key
|
|
23
|
+
* @param {object} type - wallet type, {@see @ocap/wallet#WalletType}
|
|
24
|
+
* @returns {string} DID string
|
|
27
25
|
*/
|
|
28
|
-
declare
|
|
26
|
+
declare const fromPublicKey: (pk: BytesType, type?: DIDTypeArg) => string;
|
|
27
|
+
declare const fromPublicKeyHash: (buffer: string, type: DIDTypeArg) => string;
|
|
29
28
|
/**
|
|
30
|
-
*
|
|
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)
|
|
29
|
+
* Gen DID from an hex encoded hash and role type
|
|
40
30
|
*
|
|
41
31
|
* @public
|
|
42
32
|
* @static
|
|
43
|
-
* @param {string}
|
|
44
|
-
* @param {
|
|
45
|
-
* @returns {
|
|
33
|
+
* @param {string} hash - hex encoded hash
|
|
34
|
+
* @param {enum} role - role type, {@see @ocap/mcrypto#types}
|
|
35
|
+
* @returns {string} DID string
|
|
46
36
|
*/
|
|
47
|
-
declare
|
|
37
|
+
declare const fromHash: (hash: string, role?: number) => string;
|
|
48
38
|
/**
|
|
49
|
-
*
|
|
39
|
+
* Check if an DID is generated from a publicKey
|
|
50
40
|
*
|
|
51
41
|
* @public
|
|
52
42
|
* @static
|
|
53
|
-
* @param {
|
|
54
|
-
* @
|
|
43
|
+
* @param {string} did - string of the did, usually base58btc format
|
|
44
|
+
* @param {string} pk - hex encoded publicKey string
|
|
45
|
+
* @returns {boolean}
|
|
55
46
|
*/
|
|
56
|
-
declare
|
|
47
|
+
declare const isFromPublicKey: (did: string, pk: BytesType) => boolean;
|
|
57
48
|
/**
|
|
58
|
-
*
|
|
49
|
+
* Check if a DID string is valid
|
|
59
50
|
*
|
|
60
51
|
* @public
|
|
61
52
|
* @static
|
|
62
|
-
* @
|
|
63
|
-
* @
|
|
64
|
-
* @prop {number} role - Enum field to identify wallet role type
|
|
65
|
-
* @prop {number} pk - Crypto algorithm to derive publicKey from the secretKey
|
|
66
|
-
* @prop {number} hash - Hash algorithm used to hash data before sign them
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Create an wallet type object that be used as param to create a new wallet
|
|
71
|
-
*
|
|
72
|
-
* @public
|
|
73
|
-
* @static
|
|
74
|
-
* @param {DidType|string} [type='default']
|
|
75
|
-
* @returns {object}
|
|
76
|
-
* @example
|
|
77
|
-
* const assert = require('assert');
|
|
78
|
-
* const { DidType } = require('@arcblock/did');
|
|
79
|
-
* const { types } = require('@ocap/mcrypto');
|
|
80
|
-
*
|
|
81
|
-
* const type = DidType({
|
|
82
|
-
* role: types.RoleType.ROLE_APPLICATION,
|
|
83
|
-
* pk: types.KeyType.ED25519,
|
|
84
|
-
* hash: types.HashType.SHA3,
|
|
85
|
-
* });
|
|
53
|
+
* @param {string} did - address string
|
|
54
|
+
* @returns {boolean}
|
|
86
55
|
*/
|
|
87
|
-
declare
|
|
88
|
-
|
|
89
|
-
declare namespace _Lib {
|
|
90
|
-
export interface T100 {
|
|
91
|
-
types: any;
|
|
92
|
-
toStrictHex: typeof toStrictHex;
|
|
93
|
-
fromSecretKey: (sk: string, type: any) => string;
|
|
94
|
-
fromPublicKey: (pk: string, type: any) => string;
|
|
95
|
-
fromPublicKeyHash: (buffer: any, type: any) => string;
|
|
96
|
-
fromHash: (hash: string, role?: any) => string;
|
|
97
|
-
toAddress: typeof toAddress;
|
|
98
|
-
toDid: typeof toDid;
|
|
99
|
-
isFromPublicKey: (did: string, pk: string) => boolean;
|
|
100
|
-
isValid: (did: string) => boolean;
|
|
101
|
-
isEthereumDid: typeof isEthereumDid;
|
|
102
|
-
isEthereumType: typeof isEthereumType;
|
|
103
|
-
toTypeInfo: typeof toTypeInfo;
|
|
104
|
-
fromTypeInfo: typeof fromTypeInfo;
|
|
105
|
-
DidType: typeof DidType;
|
|
106
|
-
DID_TYPE_ARCBLOCK: typeof DID_TYPE_ARCBLOCK;
|
|
107
|
-
DID_TYPE_ETHEREUM: typeof DID_TYPE_ETHEREUM;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
export = _Lib;
|
|
56
|
+
declare const isValid: (did: string) => boolean;
|
|
57
|
+
export { DIDType, DIDTypeStr, DIDTypeArg, types, toStrictHex, fromSecretKey, fromPublicKey, fromPublicKeyHash, fromHash, toAddress, toDid, isFromPublicKey, isValid, isEthereumDid, isEthereumType, toTypeInfo, toTypeInfoStr, fromTypeInfo, DidType, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, };
|
package/lib/index.js
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line
|
|
23
|
-
const debug = require('debug')(require('../package.json').name);
|
|
24
|
-
|
|
25
|
-
const { types, getSigner, getHasher } = Mcrypto;
|
|
26
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DID_TYPE_ETHEREUM = exports.DID_TYPE_ARCBLOCK = exports.DidType = exports.fromTypeInfo = exports.toTypeInfoStr = exports.toTypeInfo = exports.isEthereumType = exports.isEthereumDid = exports.isValid = exports.isFromPublicKey = exports.toDid = exports.toAddress = exports.fromHash = exports.fromPublicKeyHash = exports.fromPublicKey = exports.fromSecretKey = exports.toStrictHex = exports.types = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
5
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
6
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
7
|
+
Object.defineProperty(exports, "types", { enumerable: true, get: function () { return mcrypto_1.types; } });
|
|
8
|
+
const util_1 = require("@ocap/util");
|
|
9
|
+
Object.defineProperty(exports, "toAddress", { enumerable: true, get: function () { return util_1.toAddress; } });
|
|
10
|
+
Object.defineProperty(exports, "toDid", { enumerable: true, get: function () { return util_1.toDid; } });
|
|
11
|
+
const type_1 = require("./type");
|
|
12
|
+
Object.defineProperty(exports, "DidType", { enumerable: true, get: function () { return type_1.DidType; } });
|
|
13
|
+
Object.defineProperty(exports, "DID_TYPE_ARCBLOCK", { enumerable: true, get: function () { return type_1.DID_TYPE_ARCBLOCK; } });
|
|
14
|
+
Object.defineProperty(exports, "DID_TYPE_ETHEREUM", { enumerable: true, get: function () { return type_1.DID_TYPE_ETHEREUM; } });
|
|
15
|
+
Object.defineProperty(exports, "fromTypeInfo", { enumerable: true, get: function () { return type_1.fromTypeInfo; } });
|
|
16
|
+
Object.defineProperty(exports, "isEthereumDid", { enumerable: true, get: function () { return type_1.isEthereumDid; } });
|
|
17
|
+
Object.defineProperty(exports, "isEthereumType", { enumerable: true, get: function () { return type_1.isEthereumType; } });
|
|
18
|
+
Object.defineProperty(exports, "toTypeInfo", { enumerable: true, get: function () { return type_1.toTypeInfo; } });
|
|
19
|
+
Object.defineProperty(exports, "toTypeInfoStr", { enumerable: true, get: function () { return type_1.toTypeInfoStr; } });
|
|
20
|
+
const util_2 = require("./util");
|
|
21
|
+
Object.defineProperty(exports, "toStrictHex", { enumerable: true, get: function () { return util_2.toStrictHex; } });
|
|
27
22
|
/**
|
|
28
23
|
* Gen DID from private key and type config
|
|
29
24
|
*
|
|
@@ -36,12 +31,11 @@ const { types, getSigner, getHasher } = Mcrypto;
|
|
|
36
31
|
* @returns {string} DID string
|
|
37
32
|
*/
|
|
38
33
|
const fromSecretKey = (sk, type) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return fromPublicKey(pub.indexOf('0x') === 0 ? pub : `0x${pub}`, info);
|
|
34
|
+
const info = (0, type_1.DidType)(type);
|
|
35
|
+
const pub = (0, mcrypto_1.getSigner)(info.pk).getPublicKey(sk);
|
|
36
|
+
return fromPublicKey(pub.indexOf('0x') === 0 ? pub : `0x${pub}`, info);
|
|
43
37
|
};
|
|
44
|
-
|
|
38
|
+
exports.fromSecretKey = fromSecretKey;
|
|
45
39
|
/**
|
|
46
40
|
* Gen DID from public key and type config
|
|
47
41
|
*
|
|
@@ -52,37 +46,32 @@ const fromSecretKey = (sk, type) => {
|
|
|
52
46
|
* @returns {string} DID string
|
|
53
47
|
*/
|
|
54
48
|
const fromPublicKey = (pk, type) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return fromPublicKeyHash(pkHash, info);
|
|
49
|
+
const info = (0, type_1.DidType)(type);
|
|
50
|
+
const hashFn = (0, mcrypto_1.getHasher)(info.hash);
|
|
51
|
+
const pkHash = hashFn(pk, 1);
|
|
52
|
+
return fromPublicKeyHash(pkHash, info);
|
|
60
53
|
};
|
|
61
|
-
|
|
54
|
+
exports.fromPublicKey = fromPublicKey;
|
|
62
55
|
const fromPublicKeyHash = (buffer, type) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// fallback base16 encoding
|
|
83
|
-
return didHash;
|
|
56
|
+
const info = (0, type_1.DidType)(type);
|
|
57
|
+
const pkHash = (0, util_1.stripHexPrefix)(buffer).slice(0, 40); // 20 bytes
|
|
58
|
+
const hashFn = (0, mcrypto_1.getHasher)(info.hash);
|
|
59
|
+
const typeHex = (0, type_1.fromTypeInfo)(info);
|
|
60
|
+
const checksum = (0, util_1.stripHexPrefix)(hashFn(`0x${typeHex}${pkHash}`, 1)).slice(0, 8); // 4 bytes
|
|
61
|
+
const didHash = `0x${typeHex}${pkHash}${checksum}`;
|
|
62
|
+
// ethereum-compatible address, this address does not contain any type info
|
|
63
|
+
// but we can infer from the address itself
|
|
64
|
+
if ((0, type_1.isEthereumType)(info)) {
|
|
65
|
+
return (0, type_1.toChecksumAddress)(`0x${buffer.slice(-40)}`);
|
|
66
|
+
}
|
|
67
|
+
// default forge-compatible did
|
|
68
|
+
if (info.address === mcrypto_1.types.EncodingType.BASE58) {
|
|
69
|
+
return (0, util_1.toBase58)(didHash);
|
|
70
|
+
}
|
|
71
|
+
// fallback base16 encoding
|
|
72
|
+
return didHash;
|
|
84
73
|
};
|
|
85
|
-
|
|
74
|
+
exports.fromPublicKeyHash = fromPublicKeyHash;
|
|
86
75
|
/**
|
|
87
76
|
* Gen DID from an hex encoded hash and role type
|
|
88
77
|
*
|
|
@@ -92,21 +81,19 @@ const fromPublicKeyHash = (buffer, type) => {
|
|
|
92
81
|
* @param {enum} role - role type, {@see @ocap/mcrypto#types}
|
|
93
82
|
* @returns {string} DID string
|
|
94
83
|
*/
|
|
95
|
-
const fromHash = (hash, role = types.RoleType.ROLE_ACCOUNT) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// debug('fromHash', { hash, role, type });
|
|
107
|
-
return fromPublicKeyHash(hash, type);
|
|
84
|
+
const fromHash = (hash, role = mcrypto_1.types.RoleType.ROLE_ACCOUNT) => {
|
|
85
|
+
const roleKeys = Object.keys(mcrypto_1.types.RoleType);
|
|
86
|
+
const roleValues = Object.values(mcrypto_1.types.RoleType);
|
|
87
|
+
if (roleValues.indexOf(role) === -1) {
|
|
88
|
+
throw new Error(`Unsupported role type ${role} when gen ddi from hash`);
|
|
89
|
+
}
|
|
90
|
+
const type = (0, type_1.DidType)({
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
role: mcrypto_1.types.RoleType[roleKeys[roleValues.indexOf(role)]],
|
|
93
|
+
});
|
|
94
|
+
return fromPublicKeyHash(hash, type);
|
|
108
95
|
};
|
|
109
|
-
|
|
96
|
+
exports.fromHash = fromHash;
|
|
110
97
|
/**
|
|
111
98
|
* Check if an DID is generated from a publicKey
|
|
112
99
|
*
|
|
@@ -117,17 +104,15 @@ const fromHash = (hash, role = types.RoleType.ROLE_ACCOUNT) => {
|
|
|
117
104
|
* @returns {boolean}
|
|
118
105
|
*/
|
|
119
106
|
const isFromPublicKey = (did, pk) => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return didNew === didClean;
|
|
107
|
+
if (isValid(did) === false) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const type = (0, type_1.toTypeInfo)(did);
|
|
111
|
+
const didNew = fromPublicKey(pk, type);
|
|
112
|
+
const didClean = did.replace(util_2.DID_PREFIX, '');
|
|
113
|
+
return didNew === didClean;
|
|
129
114
|
};
|
|
130
|
-
|
|
115
|
+
exports.isFromPublicKey = isFromPublicKey;
|
|
131
116
|
/**
|
|
132
117
|
* Check if a DID string is valid
|
|
133
118
|
*
|
|
@@ -137,48 +122,19 @@ const isFromPublicKey = (did, pk) => {
|
|
|
137
122
|
* @returns {boolean}
|
|
138
123
|
*/
|
|
139
124
|
const isValid = (did) => {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const didChecksum = toStrictHex(Buffer.from(bytes.slice(22, 26)).toString('hex'));
|
|
155
|
-
|
|
156
|
-
const checksum = stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
|
|
157
|
-
|
|
158
|
-
return didChecksum === checksum;
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
module.exports = {
|
|
162
|
-
types,
|
|
163
|
-
|
|
164
|
-
toStrictHex,
|
|
165
|
-
fromSecretKey,
|
|
166
|
-
fromPublicKey,
|
|
167
|
-
fromPublicKeyHash,
|
|
168
|
-
fromHash,
|
|
169
|
-
|
|
170
|
-
toAddress,
|
|
171
|
-
toDid,
|
|
172
|
-
|
|
173
|
-
isFromPublicKey,
|
|
174
|
-
isValid,
|
|
175
|
-
|
|
176
|
-
isEthereumDid,
|
|
177
|
-
isEthereumType,
|
|
178
|
-
|
|
179
|
-
toTypeInfo,
|
|
180
|
-
fromTypeInfo,
|
|
181
|
-
DidType,
|
|
182
|
-
DID_TYPE_ARCBLOCK,
|
|
183
|
-
DID_TYPE_ETHEREUM,
|
|
125
|
+
const address = (0, util_1.toAddress)(did);
|
|
126
|
+
const { hash } = (0, type_1.toTypeInfo)(address);
|
|
127
|
+
if (typeof hash === 'undefined') {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if ((0, type_1.isEthereumDid)(address)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
const hashFn = (0, mcrypto_1.getHasher)(hash);
|
|
134
|
+
const bytes = (0, util_2.toBytes)(address);
|
|
135
|
+
const bytesHex = (0, util_2.toStrictHex)(Buffer.from(bytes.slice(0, 22)).toString('hex'));
|
|
136
|
+
const didChecksum = (0, util_2.toStrictHex)(Buffer.from(bytes.slice(22, 26)).toString('hex'));
|
|
137
|
+
const checksum = (0, util_1.stripHexPrefix)(hashFn(`0x${bytesHex}`, 1)).slice(0, 8);
|
|
138
|
+
return didChecksum === checksum;
|
|
184
139
|
};
|
|
140
|
+
exports.isValid = isValid;
|
package/lib/type.d.ts
CHANGED
|
@@ -1,56 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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;
|
|
1
|
+
export declare type DIDType = {
|
|
2
|
+
role?: number;
|
|
3
|
+
pk?: number;
|
|
4
|
+
hash?: number;
|
|
5
|
+
address?: number;
|
|
6
|
+
[key: string]: number;
|
|
17
7
|
};
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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;
|
|
8
|
+
export declare type DIDTypeStr = {
|
|
9
|
+
role?: string;
|
|
10
|
+
pk?: string;
|
|
11
|
+
hash?: string;
|
|
12
|
+
address?: string;
|
|
13
|
+
[key: string]: string;
|
|
14
|
+
};
|
|
15
|
+
export declare type DIDTypeArg = 'default' | 'eth' | 'forge' | DIDType;
|
|
16
|
+
declare const DID_TYPE_ARCBLOCK: DIDType;
|
|
17
|
+
declare const DID_TYPE_ETHEREUM: DIDType;
|
|
18
|
+
declare const isEthereumType: (type: DIDType) => boolean;
|
|
54
19
|
/**
|
|
55
20
|
* Checks if the given string is an address
|
|
56
21
|
*
|
|
@@ -58,7 +23,7 @@ export function isEthereumType(type: any): any;
|
|
|
58
23
|
* @param {String} address the given HEX address
|
|
59
24
|
* @return {Boolean}
|
|
60
25
|
*/
|
|
61
|
-
|
|
26
|
+
declare const isEthereumDid: (did: string) => boolean;
|
|
62
27
|
/**
|
|
63
28
|
* Converts to a checksum address
|
|
64
29
|
*
|
|
@@ -66,38 +31,27 @@ export function isEthereumDid(did: any): boolean;
|
|
|
66
31
|
* @param {String} address the given HEX address
|
|
67
32
|
* @return {String}
|
|
68
33
|
*/
|
|
69
|
-
|
|
34
|
+
declare const toChecksumAddress: (address: string) => string;
|
|
70
35
|
/**
|
|
71
|
-
*
|
|
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
|
|
36
|
+
* Create an wallet type object that be used as param to create a new wallet
|
|
80
37
|
*/
|
|
38
|
+
declare function DidType(type?: DIDTypeArg): DIDType;
|
|
39
|
+
declare namespace DidType {
|
|
40
|
+
var toJSON: (type: DIDType) => DIDTypeStr;
|
|
41
|
+
var fromJSON: (json: DIDTypeStr) => DIDType;
|
|
42
|
+
}
|
|
81
43
|
/**
|
|
82
|
-
*
|
|
44
|
+
* Convert type info object to hex string
|
|
83
45
|
*
|
|
84
46
|
* @public
|
|
85
47
|
* @static
|
|
86
|
-
* @param {
|
|
87
|
-
* @returns
|
|
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
|
-
* });
|
|
48
|
+
* @param {object} type - wallet type, {@see @arcblock/did#DidType}
|
|
49
|
+
* @returns string
|
|
98
50
|
*/
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
51
|
+
declare const fromTypeInfo: (type: DIDTypeArg) => string;
|
|
52
|
+
/**
|
|
53
|
+
* Get type info from did
|
|
54
|
+
*/
|
|
55
|
+
declare const toTypeInfo: (did: string) => DIDType;
|
|
56
|
+
declare const toTypeInfoStr: (did: string) => DIDTypeStr;
|
|
57
|
+
export { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, fromTypeInfo, toTypeInfo, toTypeInfoStr, isEthereumType, isEthereumDid, toChecksumAddress, DidType, };
|
package/lib/type.js
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DidType = exports.toChecksumAddress = exports.isEthereumDid = exports.isEthereumType = exports.toTypeInfoStr = exports.toTypeInfo = exports.fromTypeInfo = exports.DID_TYPE_ETHEREUM = exports.DID_TYPE_ARCBLOCK = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
8
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
9
|
+
const util_1 = require("@ocap/util");
|
|
10
|
+
const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
|
|
11
|
+
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
|
12
|
+
const pick_1 = __importDefault(require("lodash/pick"));
|
|
13
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
14
|
+
const util_2 = require("./util");
|
|
12
15
|
const mapping = {
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
pk: 'key',
|
|
17
|
+
address: 'encoding',
|
|
15
18
|
};
|
|
16
|
-
|
|
17
19
|
const DID_TYPE_ARCBLOCK = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
role: mcrypto_1.types.RoleType.ROLE_ACCOUNT,
|
|
21
|
+
pk: mcrypto_1.types.KeyType.ED25519,
|
|
22
|
+
hash: mcrypto_1.types.HashType.SHA3,
|
|
23
|
+
address: mcrypto_1.types.EncodingType.BASE58,
|
|
22
24
|
};
|
|
23
|
-
|
|
25
|
+
exports.DID_TYPE_ARCBLOCK = DID_TYPE_ARCBLOCK;
|
|
24
26
|
const DID_TYPE_ETHEREUM = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
role: mcrypto_1.types.RoleType.ROLE_ACCOUNT,
|
|
28
|
+
pk: mcrypto_1.types.KeyType.ETHEREUM,
|
|
29
|
+
hash: mcrypto_1.types.HashType.KECCAK,
|
|
30
|
+
address: mcrypto_1.types.EncodingType.BASE16,
|
|
29
31
|
};
|
|
30
|
-
|
|
32
|
+
exports.DID_TYPE_ETHEREUM = DID_TYPE_ETHEREUM;
|
|
31
33
|
const isEthereumType = (type) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
const props = ['pk', 'hash', 'address'];
|
|
35
|
+
return (0, isEqual_1.default)((0, pick_1.default)(type, props), (0, pick_1.default)(DID_TYPE_ETHEREUM, props));
|
|
34
36
|
};
|
|
35
|
-
|
|
37
|
+
exports.isEthereumType = isEthereumType;
|
|
36
38
|
/**
|
|
37
39
|
* Checks if the given string is an address
|
|
38
40
|
*
|
|
@@ -41,22 +43,19 @@ const isEthereumType = (type) => {
|
|
|
41
43
|
* @return {Boolean}
|
|
42
44
|
*/
|
|
43
45
|
const isEthereumDid = (did) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// Otherwise check each case
|
|
57
|
-
return checkAddressChecksum(address);
|
|
46
|
+
const address = (0, util_1.toAddress)(did);
|
|
47
|
+
// check if it has the basic requirements of an address
|
|
48
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
// If it's ALL lowercase or ALL upppercase
|
|
52
|
+
if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// Otherwise check each case
|
|
56
|
+
return checkAddressChecksum(address);
|
|
58
57
|
};
|
|
59
|
-
|
|
58
|
+
exports.isEthereumDid = isEthereumDid;
|
|
60
59
|
/**
|
|
61
60
|
* Checks if the given string is a checksummed address
|
|
62
61
|
*
|
|
@@ -65,22 +64,18 @@ const isEthereumDid = (did) => {
|
|
|
65
64
|
* @return {Boolean}
|
|
66
65
|
*/
|
|
67
66
|
const checkAddressChecksum = (address) => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
) {
|
|
78
|
-
return false;
|
|
67
|
+
// Check each case
|
|
68
|
+
const origin = address.replace(/^0x/i, '');
|
|
69
|
+
const addressHash = mcrypto_1.Hasher.Keccak.hash256(origin.toLowerCase()).replace(/^0x/i, '');
|
|
70
|
+
for (let i = 0; i < 40; i++) {
|
|
71
|
+
// the nth letter should be uppercase if the nth digit of casemap is 1
|
|
72
|
+
if ((parseInt(addressHash[i], 16) > 7 && origin[i].toUpperCase() !== origin[i]) ||
|
|
73
|
+
(parseInt(addressHash[i], 16) <= 7 && origin[i].toLowerCase() !== origin[i])) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
79
76
|
}
|
|
80
|
-
|
|
81
|
-
return true;
|
|
77
|
+
return true;
|
|
82
78
|
};
|
|
83
|
-
|
|
84
79
|
/**
|
|
85
80
|
* Converts to a checksum address
|
|
86
81
|
*
|
|
@@ -89,114 +84,89 @@ const checkAddressChecksum = (address) => {
|
|
|
89
84
|
* @return {String}
|
|
90
85
|
*/
|
|
91
86
|
const toChecksumAddress = (address) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
checksumAddress += lower[i];
|
|
87
|
+
if (typeof address === 'undefined') {
|
|
88
|
+
return '';
|
|
89
|
+
}
|
|
90
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
91
|
+
throw new Error(`Given address "${address}" is not a valid Ethereum address.`);
|
|
92
|
+
}
|
|
93
|
+
const lower = address.toLowerCase().replace(/^0x/i, '');
|
|
94
|
+
const addressHash = mcrypto_1.Hasher.Keccak.hash256(lower).replace(/^0x/i, '');
|
|
95
|
+
let checksumAddress = '0x';
|
|
96
|
+
for (let i = 0; i < lower.length; i++) {
|
|
97
|
+
// If ith character is 8 to f then make it uppercase
|
|
98
|
+
if (parseInt(addressHash[i], 16) > 7) {
|
|
99
|
+
checksumAddress += lower[i].toUpperCase();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
checksumAddress += lower[i];
|
|
103
|
+
}
|
|
110
104
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return checksumAddress;
|
|
105
|
+
return checksumAddress;
|
|
114
106
|
};
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* The structure of a forge wallet type
|
|
118
|
-
*
|
|
119
|
-
* @public
|
|
120
|
-
* @static
|
|
121
|
-
* @global
|
|
122
|
-
* @typedef {Object} DidType
|
|
123
|
-
* @prop {number} role - Enum field to identify wallet role type
|
|
124
|
-
* @prop {number} pk - Crypto algorithm to derive publicKey from the secretKey
|
|
125
|
-
* @prop {number} hash - Hash algorithm used to hash data before sign them
|
|
126
|
-
*/
|
|
127
|
-
|
|
107
|
+
exports.toChecksumAddress = toChecksumAddress;
|
|
128
108
|
/**
|
|
129
109
|
* Create an wallet type object that be used as param to create a new wallet
|
|
130
|
-
*
|
|
131
|
-
* @public
|
|
132
|
-
* @static
|
|
133
|
-
* @param {DidType|string} [type='default']
|
|
134
|
-
* @returns {object}
|
|
135
|
-
* @example
|
|
136
|
-
* const assert = require('assert');
|
|
137
|
-
* const { DidType } = require('@arcblock/did');
|
|
138
|
-
* const { types } = require('@ocap/mcrypto');
|
|
139
|
-
*
|
|
140
|
-
* const type = DidType({
|
|
141
|
-
* role: types.RoleType.ROLE_APPLICATION,
|
|
142
|
-
* pk: types.KeyType.ED25519,
|
|
143
|
-
* hash: types.HashType.SHA3,
|
|
144
|
-
* });
|
|
145
110
|
*/
|
|
146
|
-
function DidType(type =
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
input = { ...DID_TYPE_ARCBLOCK, ...type };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const { role, pk, hash, address } = input;
|
|
157
|
-
Object.keys(input).forEach((x) => {
|
|
158
|
-
const key = upperFirst(`${mapping[x] || x}Type`);
|
|
159
|
-
if (Object.values(types[key]).includes(input[x]) === false) {
|
|
160
|
-
throw new Error(`Unsupported ${x} type: ${input[x]}`);
|
|
111
|
+
function DidType(type = {}) {
|
|
112
|
+
let input = null;
|
|
113
|
+
if (['default', 'forge'].includes(type)) {
|
|
114
|
+
input = DID_TYPE_ARCBLOCK;
|
|
115
|
+
}
|
|
116
|
+
else if (['eth', 'ethereum'].includes(type)) {
|
|
117
|
+
input = DID_TYPE_ETHEREUM;
|
|
161
118
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const output = { role, pk, hash, address };
|
|
165
|
-
|
|
166
|
-
// Overwrite hash methods for some role type if we are not eth-type
|
|
167
|
-
if (isEthereumType(output) === false) {
|
|
168
|
-
const sha2Roles = [
|
|
169
|
-
types.RoleType.ROLE_NODE,
|
|
170
|
-
types.RoleType.ROLE_VALIDATOR,
|
|
171
|
-
types.RoleType.ROLE_TETHER,
|
|
172
|
-
types.RoleType.ROLE_SWAP,
|
|
173
|
-
];
|
|
174
|
-
if (sha2Roles.includes(output.role)) {
|
|
175
|
-
output.hash = types.HashType.SHA2;
|
|
119
|
+
else {
|
|
120
|
+
input = Object.assign(Object.assign({}, DID_TYPE_ARCBLOCK), type);
|
|
176
121
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
122
|
+
const { role, pk, hash, address } = input;
|
|
123
|
+
Object.keys(input).forEach((x) => {
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
const key = (0, upperFirst_1.default)(`${mapping[x] || x}Type`);
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
if (Object.values(mcrypto_1.types[key]).includes(input[x]) === false) {
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
throw new Error(`Unsupported ${x} type: ${input[x]}`);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
const output = { role, pk, hash, address };
|
|
133
|
+
// Overwrite hash methods for some role type if we are not eth-type
|
|
134
|
+
if (isEthereumType(output) === false) {
|
|
135
|
+
const sha2Roles = [
|
|
136
|
+
mcrypto_1.types.RoleType.ROLE_NODE,
|
|
137
|
+
mcrypto_1.types.RoleType.ROLE_VALIDATOR,
|
|
138
|
+
mcrypto_1.types.RoleType.ROLE_TETHER,
|
|
139
|
+
mcrypto_1.types.RoleType.ROLE_SWAP,
|
|
140
|
+
];
|
|
141
|
+
if (sha2Roles.includes(output.role)) {
|
|
142
|
+
output.hash = mcrypto_1.types.HashType.SHA2;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return output;
|
|
180
146
|
}
|
|
181
|
-
|
|
182
|
-
DidType.toJSON = (type) =>
|
|
183
|
-
|
|
184
|
-
const key =
|
|
185
|
-
|
|
186
|
-
const
|
|
147
|
+
exports.DidType = DidType;
|
|
148
|
+
DidType.toJSON = (type) => Object.keys(type).reduce((acc, x) => {
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
const key = (0, upperFirst_1.default)(`${mapping[x] || x}Type`);
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
const typeStr = Object.keys(mcrypto_1.types[key]);
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
const typeValues = Object.values(mcrypto_1.types[key]);
|
|
155
|
+
// @ts-ignore
|
|
187
156
|
acc[x] = typeStr[typeValues.indexOf(type[x])];
|
|
188
157
|
return acc;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const typeStr = Object.keys(types[key]);
|
|
195
|
-
|
|
158
|
+
}, {});
|
|
159
|
+
DidType.fromJSON = (json) => Object.keys(json).reduce((acc, x) => {
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
const key = (0, upperFirst_1.default)(`${mapping[x] || x}Type`);
|
|
162
|
+
// @ts-ignore
|
|
163
|
+
const typeStr = Object.keys(mcrypto_1.types[key]);
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
const typeValues = Object.values(mcrypto_1.types[key]);
|
|
166
|
+
// @ts-ignore
|
|
196
167
|
acc[x] = typeValues[typeStr.indexOf(json[x])];
|
|
197
168
|
return acc;
|
|
198
|
-
|
|
199
|
-
|
|
169
|
+
}, {});
|
|
200
170
|
/**
|
|
201
171
|
* Convert type info object to hex string
|
|
202
172
|
*
|
|
@@ -206,75 +176,62 @@ DidType.fromJSON = (json) =>
|
|
|
206
176
|
* @returns string
|
|
207
177
|
*/
|
|
208
178
|
const fromTypeInfo = (type) => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// debug('fromTypeInfo', info, roleBits, hashBits, infoBits, infoHex);
|
|
217
|
-
return toStrictHex(infoHex, 4);
|
|
179
|
+
const info = DidType(type);
|
|
180
|
+
const roleBits = (0, util_2.toBits)(info.role, 6);
|
|
181
|
+
const keyBits = (0, util_2.toBits)(info.pk, 5);
|
|
182
|
+
const hashBits = (0, util_2.toBits)(info.hash, 5);
|
|
183
|
+
const infoBits = `${roleBits}${keyBits}${hashBits}`;
|
|
184
|
+
const infoHex = (0, util_1.stripHexPrefix)((0, util_1.numberToHex)(parseInt(infoBits, 2)));
|
|
185
|
+
return (0, util_2.toStrictHex)(infoHex, 4);
|
|
218
186
|
};
|
|
219
|
-
|
|
187
|
+
exports.fromTypeInfo = fromTypeInfo;
|
|
220
188
|
/**
|
|
221
|
-
* Get type info from did
|
|
222
|
-
*
|
|
223
|
-
* @public
|
|
224
|
-
* @static
|
|
225
|
-
* @param {string} did - address string
|
|
226
|
-
* @param {boolean} [returnString=true]
|
|
227
|
-
* @returns {object} wallet type {@see @arcblock/did#DidType}
|
|
189
|
+
* Get type info from did
|
|
228
190
|
*/
|
|
229
|
-
const toTypeInfo = (did
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
191
|
+
const toTypeInfo = (did) => {
|
|
192
|
+
let type = {};
|
|
193
|
+
try {
|
|
194
|
+
if (isEthereumDid(did)) {
|
|
195
|
+
type = DID_TYPE_ETHEREUM;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
const bytes = (0, util_2.toBytes)(did);
|
|
199
|
+
const typeBytes = bytes.slice(0, 2);
|
|
200
|
+
const typeHex = (0, util_2.toStrictHex)(Buffer.from(typeBytes).toString('hex'));
|
|
201
|
+
const typeBits = (0, util_2.toBits)(new util_1.BN(typeHex, 16), 16);
|
|
202
|
+
const roleBits = typeBits.slice(0, 6);
|
|
203
|
+
const keyBits = typeBits.slice(6, 11);
|
|
204
|
+
const hashBits = typeBits.slice(11, 16);
|
|
205
|
+
type = {
|
|
206
|
+
role: parseInt(roleBits, 2),
|
|
207
|
+
pk: parseInt(keyBits, 2),
|
|
208
|
+
hash: parseInt(hashBits, 2),
|
|
209
|
+
address: (0, util_1.isHex)((0, util_1.toAddress)(did)) ? mcrypto_1.types.EncodingType.BASE16 : mcrypto_1.types.EncodingType.BASE58,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
// remove unsupported types
|
|
213
|
+
Object.keys(type).forEach((x) => {
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
const enums = Object.values(mcrypto_1.types[`${(0, upperFirst_1.default)(mapping[x] || x)}Type`]);
|
|
216
|
+
if (enums.includes(type[x]) === false) {
|
|
217
|
+
delete type[x];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
return type;
|
|
221
|
+
}
|
|
222
|
+
catch (_a) {
|
|
223
|
+
return type;
|
|
248
224
|
}
|
|
249
|
-
|
|
250
|
-
// remove unsupported types
|
|
251
|
-
Object.keys(type).forEach((x) => {
|
|
252
|
-
const enums = Object.values(types[`${upperFirst(mapping[x] || x)}Type`]);
|
|
253
|
-
if (enums.includes(type[x]) === false) {
|
|
254
|
-
delete type[x];
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
const typeStr = Object.keys(type).reduce((acc, x) => {
|
|
259
|
-
const enums = types[`${upperFirst(mapping[x] || x)}Type`];
|
|
260
|
-
acc[x] = Object.keys(enums).find((d) => enums[d] === type[x]);
|
|
261
|
-
return acc;
|
|
262
|
-
}, {});
|
|
263
|
-
|
|
264
|
-
return returnString ? typeStr : type;
|
|
265
|
-
} catch (err) {
|
|
266
|
-
debug('AbtDid.toTypeInfo.decodeError', err);
|
|
267
|
-
return {};
|
|
268
|
-
}
|
|
269
225
|
};
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
226
|
+
exports.toTypeInfo = toTypeInfo;
|
|
227
|
+
const toTypeInfoStr = (did) => {
|
|
228
|
+
const type = toTypeInfo(did);
|
|
229
|
+
return Object.keys(type).reduce((acc, x) => {
|
|
230
|
+
// @ts-ignore
|
|
231
|
+
const enums = mcrypto_1.types[`${(0, upperFirst_1.default)(mapping[x] || x)}Type`];
|
|
232
|
+
// @ts-ignore
|
|
233
|
+
acc[x] = Object.keys(enums).find((d) => enums[d] === type[x]);
|
|
234
|
+
return acc;
|
|
235
|
+
}, {});
|
|
280
236
|
};
|
|
237
|
+
exports.toTypeInfoStr = toTypeInfoStr;
|
package/lib/util.d.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* @param {string} decimal
|
|
6
|
-
* @param {number} length
|
|
7
|
-
* @returns String
|
|
8
|
-
*/
|
|
9
|
-
export function toBits(decimal: string, length: number): any;
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BN } from '@ocap/util';
|
|
3
|
+
declare const DID_PREFIX = "did:abt:";
|
|
10
4
|
/**
|
|
11
5
|
* Convert did to bytes with length of 26
|
|
12
6
|
*
|
|
13
7
|
* @param {string} did
|
|
14
8
|
* @returns {Buffer}
|
|
15
9
|
*/
|
|
16
|
-
|
|
10
|
+
declare const toBytes: (did: string) => Buffer;
|
|
11
|
+
/**
|
|
12
|
+
* Convert number to bit string with predefined length
|
|
13
|
+
*/
|
|
14
|
+
declare const toBits: (number: string | number | BN, length: number) => string;
|
|
17
15
|
/**
|
|
18
16
|
* Ensure the hex length is even number, 2, 4, 6, 8
|
|
19
17
|
*
|
|
@@ -21,4 +19,5 @@ export function toBytes(did: string): Buffer;
|
|
|
21
19
|
* @param {number} length
|
|
22
20
|
* @returns {string} hex
|
|
23
21
|
*/
|
|
24
|
-
|
|
22
|
+
declare const toStrictHex: (hex: string, length?: number) => string;
|
|
23
|
+
export { DID_PREFIX, toBits, toBytes, toStrictHex };
|
package/lib/util.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toStrictHex = exports.toBytes = exports.toBits = exports.DID_PREFIX = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
|
+
const padStart_1 = __importDefault(require("lodash/padStart"));
|
|
9
|
+
const util_1 = require("@ocap/util");
|
|
4
10
|
const DID_PREFIX = 'did:abt:';
|
|
5
|
-
|
|
11
|
+
exports.DID_PREFIX = DID_PREFIX;
|
|
6
12
|
/**
|
|
7
13
|
* Convert did to bytes with length of 26
|
|
8
14
|
*
|
|
@@ -10,31 +16,27 @@ const DID_PREFIX = 'did:abt:';
|
|
|
10
16
|
* @returns {Buffer}
|
|
11
17
|
*/
|
|
12
18
|
const toBytes = (did) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
try {
|
|
20
|
+
if ((0, util_1.isHexStrict)(did)) {
|
|
21
|
+
return Buffer.from((0, util_1.stripHexPrefix)(did), 'hex');
|
|
22
|
+
}
|
|
23
|
+
let bytes = (0, util_1.fromBase58)(did.replace(DID_PREFIX, ''));
|
|
24
|
+
while (bytes.length < 26) {
|
|
25
|
+
bytes = Buffer.concat([Buffer.from([0]), bytes]);
|
|
26
|
+
}
|
|
27
|
+
return bytes;
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
bytes = Buffer.concat([Buffer.from([0]), bytes]);
|
|
29
|
+
catch (err) {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
throw new Error(`Cannot convert DID string to byte array: ${err.message}`);
|
|
21
32
|
}
|
|
22
|
-
|
|
23
|
-
return bytes;
|
|
24
|
-
} catch (err) {
|
|
25
|
-
throw new Error(`Cannot convert DID string to byte array: ${err.message}`);
|
|
26
|
-
}
|
|
27
33
|
};
|
|
28
|
-
|
|
34
|
+
exports.toBytes = toBytes;
|
|
29
35
|
/**
|
|
30
36
|
* Convert number to bit string with predefined length
|
|
31
|
-
*
|
|
32
|
-
* @param {string} decimal
|
|
33
|
-
* @param {number} length
|
|
34
|
-
* @returns String
|
|
35
37
|
*/
|
|
36
|
-
const toBits = (
|
|
37
|
-
|
|
38
|
+
const toBits = (number, length) => (0, padStart_1.default)((0, util_1.toBN)(number).toString(2), length, '0');
|
|
39
|
+
exports.toBits = toBits;
|
|
38
40
|
/**
|
|
39
41
|
* Ensure the hex length is even number, 2, 4, 6, 8
|
|
40
42
|
*
|
|
@@ -43,18 +45,10 @@ const toBits = (decimal, length) => padStart(toBN(decimal).toString(2), length,
|
|
|
43
45
|
* @returns {string} hex
|
|
44
46
|
*/
|
|
45
47
|
const toStrictHex = (hex, length) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return str.length % 2 === 0 ? str : `0${str}`;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
module.exports = {
|
|
56
|
-
DID_PREFIX,
|
|
57
|
-
toBits,
|
|
58
|
-
toBytes,
|
|
59
|
-
toStrictHex,
|
|
48
|
+
const str = hex.replace(/^0x/i, '');
|
|
49
|
+
if (typeof length === 'number' && length % 2 === 0) {
|
|
50
|
+
return (0, padStart_1.default)(hex, length, '0');
|
|
51
|
+
}
|
|
52
|
+
return str.length % 2 === 0 ? str : `0${str}`;
|
|
60
53
|
};
|
|
54
|
+
exports.toStrictHex = toStrictHex;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/did",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.17",
|
|
4
4
|
"description": "Javascript lib to work with ArcBlock DID",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arcblock",
|
|
@@ -22,36 +22,42 @@
|
|
|
22
22
|
"homepage": "https://github.com/ArcBlock/asset-chain/tree/master/did/did",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
24
|
"main": "lib/index.js",
|
|
25
|
+
"typings": "lib/index.d.ts",
|
|
25
26
|
"files": [
|
|
26
27
|
"lib"
|
|
27
28
|
],
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
+
"@arcblock/eslint-config-ts": "0.2.2",
|
|
31
|
+
"@types/jest": "^28.1.1",
|
|
32
|
+
"@types/node": "^17.0.42",
|
|
33
|
+
"eslint": "^8.17.0",
|
|
34
|
+
"jest": "^28.1.1",
|
|
35
|
+
"ts-jest": "^28.0.4",
|
|
36
|
+
"typescript": "^4.7.3"
|
|
30
37
|
},
|
|
31
38
|
"repository": {
|
|
32
39
|
"type": "git",
|
|
33
40
|
"url": "git+https://github.com/ArcBlock/asset-chain.git"
|
|
34
41
|
},
|
|
35
42
|
"scripts": {
|
|
36
|
-
"lint": "eslint tests
|
|
37
|
-
"lint:fix": "
|
|
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",
|
|
43
|
+
"lint": "eslint src tests",
|
|
44
|
+
"lint:fix": "npm run lint -- --fix",
|
|
43
45
|
"test": "jest --forceExit --detectOpenHandles",
|
|
44
|
-
"coverage": "yarn test -- --coverage"
|
|
46
|
+
"coverage": "yarn test -- --coverage",
|
|
47
|
+
"clean": "rm -fr lib",
|
|
48
|
+
"prebuild": "npm run clean",
|
|
49
|
+
"build": "tsc",
|
|
50
|
+
"build:watch": "npm run build -- -w"
|
|
45
51
|
},
|
|
46
52
|
"bugs": {
|
|
47
53
|
"url": "https://github.com/ArcBlock/asset-chain/issues"
|
|
48
54
|
},
|
|
49
55
|
"dependencies": {
|
|
50
|
-
"@ocap/mcrypto": "1.16.
|
|
51
|
-
"@ocap/util": "1.16.
|
|
52
|
-
"bn.js": "^5.2.
|
|
53
|
-
"debug": "^4.3.
|
|
56
|
+
"@ocap/mcrypto": "1.16.17",
|
|
57
|
+
"@ocap/util": "1.16.17",
|
|
58
|
+
"bn.js": "^5.2.1",
|
|
59
|
+
"debug": "^4.3.4",
|
|
54
60
|
"lodash": "^4.17.21"
|
|
55
61
|
},
|
|
56
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "489ce5e03bce27ddcd535390228b11ab56e7a2e3"
|
|
57
63
|
}
|