@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/lib/type.js
CHANGED
|
@@ -1,243 +1,190 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const pick_1 = __importDefault(require("lodash/pick"));
|
|
14
|
-
const mcrypto_1 = require("@ocap/mcrypto");
|
|
15
|
-
const util_2 = require("./util");
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
const require_util = require('./util.js');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_util = require("@ocap/util");
|
|
5
|
+
let lodash_upperFirst = require("lodash/upperFirst");
|
|
6
|
+
lodash_upperFirst = require_rolldown_runtime.__toESM(lodash_upperFirst);
|
|
7
|
+
let lodash_isEqual = require("lodash/isEqual");
|
|
8
|
+
lodash_isEqual = require_rolldown_runtime.__toESM(lodash_isEqual);
|
|
9
|
+
let lodash_pick = require("lodash/pick");
|
|
10
|
+
lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
|
|
11
|
+
|
|
12
|
+
//#region src/type.ts
|
|
16
13
|
const mapping = {
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
pk: "key",
|
|
15
|
+
address: "encoding"
|
|
19
16
|
};
|
|
20
17
|
const DID_TYPE_ARCBLOCK = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
role: _ocap_mcrypto.types.RoleType.ROLE_ACCOUNT,
|
|
19
|
+
pk: _ocap_mcrypto.types.KeyType.ED25519,
|
|
20
|
+
hash: _ocap_mcrypto.types.HashType.SHA3,
|
|
21
|
+
address: _ocap_mcrypto.types.EncodingType.BASE58
|
|
25
22
|
};
|
|
26
|
-
exports.DID_TYPE_ARCBLOCK = DID_TYPE_ARCBLOCK;
|
|
27
23
|
const DID_TYPE_PASSKEY = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
role: _ocap_mcrypto.types.RoleType.ROLE_PASSKEY,
|
|
25
|
+
pk: _ocap_mcrypto.types.KeyType.PASSKEY,
|
|
26
|
+
hash: _ocap_mcrypto.types.HashType.SHA2,
|
|
27
|
+
address: _ocap_mcrypto.types.EncodingType.BASE58
|
|
32
28
|
};
|
|
33
|
-
exports.DID_TYPE_PASSKEY = DID_TYPE_PASSKEY;
|
|
34
29
|
const DID_TYPE_ETHEREUM = {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
role: _ocap_mcrypto.types.RoleType.ROLE_ACCOUNT,
|
|
31
|
+
pk: _ocap_mcrypto.types.KeyType.ETHEREUM,
|
|
32
|
+
hash: _ocap_mcrypto.types.HashType.KECCAK,
|
|
33
|
+
address: _ocap_mcrypto.types.EncodingType.BASE16
|
|
39
34
|
};
|
|
40
|
-
exports.DID_TYPE_ETHEREUM = DID_TYPE_ETHEREUM;
|
|
41
35
|
const isEthereumType = (type) => {
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
const props = [
|
|
37
|
+
"pk",
|
|
38
|
+
"hash",
|
|
39
|
+
"address"
|
|
40
|
+
];
|
|
41
|
+
return (0, lodash_isEqual.default)((0, lodash_pick.default)(type, props), (0, lodash_pick.default)(DID_TYPE_ETHEREUM, props));
|
|
44
42
|
};
|
|
45
|
-
exports.isEthereumType = isEthereumType;
|
|
46
43
|
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
* Checks if the given string is an address
|
|
45
|
+
*
|
|
46
|
+
* @method isEthereumDid
|
|
47
|
+
* @param {String} address the given HEX address
|
|
48
|
+
* @return {Boolean}
|
|
49
|
+
*/
|
|
53
50
|
const isEthereumDid = (did) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
// If it's ALL lowercase or ALL upppercase
|
|
60
|
-
if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
// Otherwise check each case
|
|
64
|
-
return checkAddressChecksum(address);
|
|
51
|
+
const address = (0, _ocap_util.toAddress)(did);
|
|
52
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) return false;
|
|
53
|
+
if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) return true;
|
|
54
|
+
return checkAddressChecksum(address);
|
|
65
55
|
};
|
|
66
|
-
exports.isEthereumDid = isEthereumDid;
|
|
67
56
|
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
* Checks if the given string is a checksummed address
|
|
58
|
+
*
|
|
59
|
+
* @method checkAddressChecksum
|
|
60
|
+
* @param {String} address the given HEX address
|
|
61
|
+
* @return {Boolean}
|
|
62
|
+
*/
|
|
74
63
|
const checkAddressChecksum = (address) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// the nth letter should be uppercase if the nth digit of casemap is 1
|
|
80
|
-
if ((parseInt(addressHash[i], 16) > 7 && origin[i].toUpperCase() !== origin[i]) ||
|
|
81
|
-
(parseInt(addressHash[i], 16) <= 7 && origin[i].toLowerCase() !== origin[i])) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return true;
|
|
64
|
+
const origin = address.replace(/^0x/i, "");
|
|
65
|
+
const addressHash = _ocap_mcrypto.Hasher.Keccak.hash256(origin.toLowerCase()).replace(/^0x/i, "");
|
|
66
|
+
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;
|
|
67
|
+
return true;
|
|
86
68
|
};
|
|
87
69
|
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
70
|
+
* Converts to a checksum address
|
|
71
|
+
*
|
|
72
|
+
* @method toChecksumAddress
|
|
73
|
+
* @param {String} address the given HEX address
|
|
74
|
+
* @return {String}
|
|
75
|
+
*/
|
|
94
76
|
const toChecksumAddress = (address) => {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
let checksumAddress = '0x';
|
|
104
|
-
for (let i = 0; i < lower.length; i++) {
|
|
105
|
-
// If ith character is 8 to f then make it uppercase
|
|
106
|
-
if (parseInt(addressHash[i], 16) > 7) {
|
|
107
|
-
checksumAddress += lower[i].toUpperCase();
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
checksumAddress += lower[i];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return checksumAddress;
|
|
77
|
+
if (typeof address === "undefined") return "";
|
|
78
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);
|
|
79
|
+
const lower = address.toLowerCase().replace(/^0x/i, "");
|
|
80
|
+
const addressHash = _ocap_mcrypto.Hasher.Keccak.hash256(lower).replace(/^0x/i, "");
|
|
81
|
+
let checksumAddress = "0x";
|
|
82
|
+
for (let i = 0; i < lower.length; i++) if (parseInt(addressHash[i], 16) > 7) checksumAddress += lower[i].toUpperCase();
|
|
83
|
+
else checksumAddress += lower[i];
|
|
84
|
+
return checksumAddress;
|
|
114
85
|
};
|
|
115
|
-
exports.toChecksumAddress = toChecksumAddress;
|
|
116
86
|
/**
|
|
117
|
-
|
|
118
|
-
|
|
87
|
+
* Create an wallet type object that be used as param to create a new wallet
|
|
88
|
+
*/
|
|
119
89
|
function DidType(type = {}) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
mcrypto_1.types.RoleType.ROLE_SWAP,
|
|
149
|
-
];
|
|
150
|
-
if (sha2Roles.includes(output.role)) {
|
|
151
|
-
output.hash = mcrypto_1.types.HashType.SHA2;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return output;
|
|
90
|
+
let input = {};
|
|
91
|
+
if (["default", "arcblock"].includes(type)) input = DID_TYPE_ARCBLOCK;
|
|
92
|
+
else if (["eth", "ethereum"].includes(type)) input = DID_TYPE_ETHEREUM;
|
|
93
|
+
else if (["passkey"].includes(type)) input = DID_TYPE_PASSKEY;
|
|
94
|
+
else input = {
|
|
95
|
+
...DID_TYPE_ARCBLOCK,
|
|
96
|
+
...type
|
|
97
|
+
};
|
|
98
|
+
const { role, pk, hash, address } = input;
|
|
99
|
+
Object.keys(input).forEach((x) => {
|
|
100
|
+
const key = (0, lodash_upperFirst.default)(`${mapping[x] || x}Type`);
|
|
101
|
+
if (Object.values(_ocap_mcrypto.types[key]).includes(input[x]) === false) throw new Error(`Unsupported ${x} type: ${input[x]}`);
|
|
102
|
+
});
|
|
103
|
+
const output = {
|
|
104
|
+
role,
|
|
105
|
+
pk,
|
|
106
|
+
hash,
|
|
107
|
+
address
|
|
108
|
+
};
|
|
109
|
+
if (isEthereumType(output) === false) {
|
|
110
|
+
if ([
|
|
111
|
+
_ocap_mcrypto.types.RoleType.ROLE_NODE,
|
|
112
|
+
_ocap_mcrypto.types.RoleType.ROLE_VALIDATOR,
|
|
113
|
+
_ocap_mcrypto.types.RoleType.ROLE_TETHER,
|
|
114
|
+
_ocap_mcrypto.types.RoleType.ROLE_SWAP
|
|
115
|
+
].includes(output.role)) output.hash = _ocap_mcrypto.types.HashType.SHA2;
|
|
116
|
+
}
|
|
117
|
+
return output;
|
|
155
118
|
}
|
|
156
119
|
DidType.toJSON = (type) => Object.keys(type).reduce((acc, x) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// @ts-ignore
|
|
161
|
-
const typeValues = Object.values(mcrypto_1.types[key]);
|
|
162
|
-
// @ts-ignore
|
|
163
|
-
acc[x] = typeStr[typeValues.indexOf(type[x])];
|
|
164
|
-
return acc;
|
|
120
|
+
const key = (0, lodash_upperFirst.default)(`${mapping[x] || x}Type`);
|
|
121
|
+
acc[x] = Object.keys(_ocap_mcrypto.types[key])[Object.values(_ocap_mcrypto.types[key]).indexOf(type[x])];
|
|
122
|
+
return acc;
|
|
165
123
|
}, {});
|
|
166
124
|
DidType.fromJSON = (json) => Object.keys(json).reduce((acc, x) => {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const typeValues = Object.values(mcrypto_1.types[key]);
|
|
172
|
-
// @ts-ignore
|
|
173
|
-
acc[x] = typeValues[typeStr.indexOf(json[x])];
|
|
174
|
-
return acc;
|
|
125
|
+
const key = (0, lodash_upperFirst.default)(`${mapping[x] || x}Type`);
|
|
126
|
+
const typeStr = Object.keys(_ocap_mcrypto.types[key]);
|
|
127
|
+
acc[x] = Object.values(_ocap_mcrypto.types[key])[typeStr.indexOf(json[x])];
|
|
128
|
+
return acc;
|
|
175
129
|
}, {});
|
|
176
130
|
/**
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
131
|
+
* Convert type info object to hex string
|
|
132
|
+
*
|
|
133
|
+
* @public
|
|
134
|
+
* @static
|
|
135
|
+
* @param {object} type - wallet type, {@see @arcblock/did#DidType}
|
|
136
|
+
* @returns string
|
|
137
|
+
*/
|
|
184
138
|
const fromTypeInfo = (type) => {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const hashBits = (0, util_2.toBits)(info.hash, 5);
|
|
189
|
-
const infoBits = `${roleBits}${keyBits}${hashBits}`;
|
|
190
|
-
const infoHex = (0, util_1.stripHexPrefix)((0, util_1.numberToHex)(parseInt(infoBits, 2)));
|
|
191
|
-
return (0, util_2.toStrictHex)(infoHex, 4);
|
|
139
|
+
const info = DidType(type);
|
|
140
|
+
const infoBits = `${require_util.toBits(info.role, 6)}${require_util.toBits(info.pk, 5)}${require_util.toBits(info.hash, 5)}`;
|
|
141
|
+
return require_util.toStrictHex((0, _ocap_util.stripHexPrefix)((0, _ocap_util.numberToHex)(parseInt(infoBits, 2))), 4);
|
|
192
142
|
};
|
|
193
|
-
exports.fromTypeInfo = fromTypeInfo;
|
|
194
143
|
/**
|
|
195
|
-
|
|
196
|
-
|
|
144
|
+
* Get type info from did
|
|
145
|
+
*/
|
|
197
146
|
const toTypeInfo = (did) => {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const enums = Object.values(mcrypto_1.types[`${(0, upperFirst_1.default)(mapping[x] || x)}Type`]);
|
|
222
|
-
if (enums.includes(type[x]) === false) {
|
|
223
|
-
delete type[x];
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
return type;
|
|
227
|
-
}
|
|
228
|
-
catch {
|
|
229
|
-
return type;
|
|
230
|
-
}
|
|
147
|
+
let type = {};
|
|
148
|
+
try {
|
|
149
|
+
if (isEthereumDid(did)) type = DID_TYPE_ETHEREUM;
|
|
150
|
+
else {
|
|
151
|
+
const typeBytes = require_util.toBytes(did).slice(0, 2);
|
|
152
|
+
const typeBits = require_util.toBits(new _ocap_util.BN(require_util.toStrictHex(Buffer.from(typeBytes).toString("hex")), 16), 16);
|
|
153
|
+
const roleBits = typeBits.slice(0, 6);
|
|
154
|
+
const keyBits = typeBits.slice(6, 11);
|
|
155
|
+
const hashBits = typeBits.slice(11, 16);
|
|
156
|
+
type = {
|
|
157
|
+
role: parseInt(roleBits, 2),
|
|
158
|
+
pk: parseInt(keyBits, 2),
|
|
159
|
+
hash: parseInt(hashBits, 2),
|
|
160
|
+
address: (0, _ocap_util.isHex)((0, _ocap_util.toAddress)(did)) ? _ocap_mcrypto.types.EncodingType.BASE16 : _ocap_mcrypto.types.EncodingType.BASE58
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
Object.keys(type).forEach((x) => {
|
|
164
|
+
if (Object.values(_ocap_mcrypto.types[`${(0, lodash_upperFirst.default)(mapping[x] || x)}Type`]).includes(type[x]) === false) delete type[x];
|
|
165
|
+
});
|
|
166
|
+
return type;
|
|
167
|
+
} catch {
|
|
168
|
+
return type;
|
|
169
|
+
}
|
|
231
170
|
};
|
|
232
|
-
exports.toTypeInfo = toTypeInfo;
|
|
233
171
|
const toTypeInfoStr = (did) => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return acc;
|
|
241
|
-
}, {});
|
|
172
|
+
const type = toTypeInfo(did);
|
|
173
|
+
return Object.keys(type).reduce((acc, x) => {
|
|
174
|
+
const enums = _ocap_mcrypto.types[`${(0, lodash_upperFirst.default)(mapping[x] || x)}Type`];
|
|
175
|
+
acc[x] = Object.keys(enums).find((d) => enums[d] === type[x]);
|
|
176
|
+
return acc;
|
|
177
|
+
}, {});
|
|
242
178
|
};
|
|
243
|
-
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
exports.DID_TYPE_ARCBLOCK = DID_TYPE_ARCBLOCK;
|
|
182
|
+
exports.DID_TYPE_ETHEREUM = DID_TYPE_ETHEREUM;
|
|
183
|
+
exports.DID_TYPE_PASSKEY = DID_TYPE_PASSKEY;
|
|
184
|
+
exports.DidType = DidType;
|
|
185
|
+
exports.fromTypeInfo = fromTypeInfo;
|
|
186
|
+
exports.isEthereumDid = isEthereumDid;
|
|
187
|
+
exports.isEthereumType = isEthereumType;
|
|
188
|
+
exports.toChecksumAddress = toChecksumAddress;
|
|
189
|
+
exports.toTypeInfo = toTypeInfo;
|
|
190
|
+
exports.toTypeInfoStr = toTypeInfoStr;
|
package/lib/util.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { BN } from
|
|
1
|
+
import { BN } from "@ocap/util";
|
|
2
|
+
|
|
3
|
+
//#region src/util.d.ts
|
|
2
4
|
declare const DID_PREFIX = "did:abt:";
|
|
3
5
|
/**
|
|
4
6
|
* Convert did to bytes with length of 26
|
|
@@ -18,5 +20,6 @@ declare const toBits: (number: string | number | BN, length: number) => string;
|
|
|
18
20
|
* @param {number} length
|
|
19
21
|
* @returns {string} hex
|
|
20
22
|
*/
|
|
21
|
-
declare const toStrictHex: (hex: string, length?: number) =>
|
|
22
|
-
|
|
23
|
+
declare const toStrictHex: (hex: string, length?: number) => string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { DID_PREFIX, toBits, toBytes, toStrictHex };
|
package/lib/util.js
CHANGED
|
@@ -1,54 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const padStart_1 = __importDefault(require("lodash/padStart"));
|
|
9
|
-
const util_1 = require("@ocap/util");
|
|
10
|
-
const DID_PREFIX = 'did:abt:';
|
|
11
|
-
exports.DID_PREFIX = DID_PREFIX;
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
let _ocap_util = require("@ocap/util");
|
|
3
|
+
let lodash_padStart = require("lodash/padStart");
|
|
4
|
+
lodash_padStart = require_rolldown_runtime.__toESM(lodash_padStart);
|
|
5
|
+
|
|
6
|
+
//#region src/util.ts
|
|
7
|
+
const DID_PREFIX = "did:abt:";
|
|
12
8
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
* Convert did to bytes with length of 26
|
|
10
|
+
*
|
|
11
|
+
* @param {string} did
|
|
12
|
+
* @returns {Buffer}
|
|
13
|
+
*/
|
|
18
14
|
const toBytes = (did) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return bytes;
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
throw new Error(`Cannot convert DID string to byte array: ${err.message}`);
|
|
32
|
-
}
|
|
15
|
+
try {
|
|
16
|
+
if ((0, _ocap_util.isHexStrict)(did)) return Buffer.from((0, _ocap_util.stripHexPrefix)(did), "hex");
|
|
17
|
+
let bytes = (0, _ocap_util.fromBase58)(did.replace(DID_PREFIX, ""));
|
|
18
|
+
while (bytes.length < 26) bytes = Buffer.concat([Buffer.from([0]), bytes]);
|
|
19
|
+
return bytes;
|
|
20
|
+
} catch (err) {
|
|
21
|
+
throw new Error(`Cannot convert DID string to byte array: ${err.message}`);
|
|
22
|
+
}
|
|
33
23
|
};
|
|
34
|
-
exports.toBytes = toBytes;
|
|
35
24
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const toBits = (number, length) => (0,
|
|
39
|
-
exports.toBits = toBits;
|
|
25
|
+
* Convert number to bit string with predefined length
|
|
26
|
+
*/
|
|
27
|
+
const toBits = (number, length) => (0, lodash_padStart.default)((0, _ocap_util.toBN)(number).toString(2), length, "0");
|
|
40
28
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
* Ensure the hex length is even number, 2, 4, 6, 8
|
|
30
|
+
*
|
|
31
|
+
* @param {string} hex
|
|
32
|
+
* @param {number} length
|
|
33
|
+
* @returns {string} hex
|
|
34
|
+
*/
|
|
47
35
|
const toStrictHex = (hex, length) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
return str.length % 2 === 0 ? str : `0${str}`;
|
|
36
|
+
const str = hex.replace(/^0x/i, "");
|
|
37
|
+
if (typeof length === "number" && length % 2 === 0) return (0, lodash_padStart.default)(hex, length, "0");
|
|
38
|
+
return str.length % 2 === 0 ? str : `0${str}`;
|
|
53
39
|
};
|
|
54
|
-
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.DID_PREFIX = DID_PREFIX;
|
|
43
|
+
exports.toBits = toBits;
|
|
44
|
+
exports.toBytes = toBytes;
|
|
45
|
+
exports.toStrictHex = toStrictHex;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/did",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.28.1",
|
|
4
4
|
"description": "Javascript lib to work with ArcBlock DID",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arcblock",
|
|
@@ -21,8 +21,22 @@
|
|
|
21
21
|
],
|
|
22
22
|
"homepage": "https://www.arcblock.io/docs/blockchain-utils",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
|
+
"sideEffects": false,
|
|
24
25
|
"main": "./lib/index.js",
|
|
25
|
-
"
|
|
26
|
+
"module": "./esm/index.js",
|
|
27
|
+
"types": "./esm/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./esm/index.d.ts",
|
|
31
|
+
"import": "./esm/index.js",
|
|
32
|
+
"default": "./lib/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./lib/*": {
|
|
35
|
+
"types": "./lib/*",
|
|
36
|
+
"import": "./esm/*",
|
|
37
|
+
"default": "./lib/*.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
26
40
|
"files": [
|
|
27
41
|
"lib",
|
|
28
42
|
"esm"
|
|
@@ -34,6 +48,7 @@
|
|
|
34
48
|
"eslint": "^8.57.0",
|
|
35
49
|
"jest": "^29.7.0",
|
|
36
50
|
"ts-jest": "^29.2.5",
|
|
51
|
+
"tsdown": "^0.18.4",
|
|
37
52
|
"type-fest": "^3.1.0",
|
|
38
53
|
"typescript": "^5.6.2"
|
|
39
54
|
},
|
|
@@ -48,19 +63,15 @@
|
|
|
48
63
|
"bn.js": "^5.2.1",
|
|
49
64
|
"debug": "^4.3.6",
|
|
50
65
|
"lodash": "^4.17.21",
|
|
51
|
-
"@ocap/mcrypto": "1.
|
|
52
|
-
"@ocap/util": "1.
|
|
66
|
+
"@ocap/mcrypto": "1.28.1",
|
|
67
|
+
"@ocap/util": "1.28.1"
|
|
53
68
|
},
|
|
54
69
|
"scripts": {
|
|
55
70
|
"lint": "eslint src tests",
|
|
56
71
|
"lint:fix": "npm run lint -- --fix",
|
|
57
72
|
"test": "jest --forceExit --detectOpenHandles",
|
|
58
73
|
"coverage": "npm run test -- --coverage",
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
62
|
-
"build:esm": "tsc -p tsconfig.esm.json",
|
|
63
|
-
"build": "npm run build:cjs && npm run build:esm",
|
|
64
|
-
"build:watch": "npm run build -- -w"
|
|
74
|
+
"build": "tsdown",
|
|
75
|
+
"build:watch": "tsdown -w"
|
|
65
76
|
}
|
|
66
77
|
}
|