@ocap/util 1.29.17 → 1.29.19
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.mts +12 -4
- package/esm/index.mjs +21 -7
- package/lib/index.cjs +21 -6
- package/lib/index.d.cts +12 -4
- package/package.json +2 -2
package/esm/index.d.mts
CHANGED
|
@@ -210,14 +210,22 @@ declare function toBase64(v: any, escape?: boolean): string;
|
|
|
210
210
|
*/
|
|
211
211
|
declare function fromBase64(v: string): Buffer;
|
|
212
212
|
/**
|
|
213
|
-
* Convert did to address: remove `did:
|
|
213
|
+
* Convert did to address: remove `did:{method}:` prefix.
|
|
214
|
+
* Strips only the first layer prefix (anchored regex).
|
|
214
215
|
*/
|
|
215
216
|
declare function toAddress(did: string): string;
|
|
216
217
|
/**
|
|
217
|
-
* Convert address to did: prepend `did:
|
|
218
|
+
* Convert address to did: prepend `did:{method}:` prefix.
|
|
219
|
+
* @param address - bare address or full DID
|
|
220
|
+
* @param method - DID method (default: 'abt')
|
|
218
221
|
*/
|
|
219
|
-
declare function toDid(address: string): string;
|
|
222
|
+
declare function toDid(address: string, method?: string): string;
|
|
220
223
|
declare function isSameDid(a: string, b: string): boolean;
|
|
224
|
+
/**
|
|
225
|
+
* Extract the method from a DID string.
|
|
226
|
+
* @returns The method string, or null if no prefix found
|
|
227
|
+
*/
|
|
228
|
+
declare function extractMethod(did: unknown): string | null;
|
|
221
229
|
declare function formatTxType(type: string): Capitalize<string>;
|
|
222
230
|
//#endregion
|
|
223
|
-
export { BN, BytesType, EncodingType, KeyPairType, UUID, bytesToHex, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
|
231
|
+
export { BN, BytesType, EncodingType, KeyPairType, UUID, bytesToHex, extractMethod, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
package/esm/index.mjs
CHANGED
|
@@ -15,7 +15,6 @@ import upperFirst from "lodash/upperFirst.js";
|
|
|
15
15
|
import utf8 from "utf8";
|
|
16
16
|
|
|
17
17
|
//#region src/index.ts
|
|
18
|
-
const DID_PREFIX = "did:abt:";
|
|
19
18
|
const zero = new BN(0);
|
|
20
19
|
const negative1 = new BN(-1);
|
|
21
20
|
const base58btc = {
|
|
@@ -406,24 +405,39 @@ function fromBase64(v) {
|
|
|
406
405
|
if (typeof v !== "string") throw new Error("fromBase64 requires input to be a string");
|
|
407
406
|
return Buffer.from(base64.unescape(v), "base64");
|
|
408
407
|
}
|
|
408
|
+
/** Pattern matching any did:{method}: prefix where method is [a-z0-9]+ */
|
|
409
|
+
const DID_PREFIX_PATTERN = /^did:[a-z0-9]+:/;
|
|
409
410
|
/**
|
|
410
|
-
* Convert did to address: remove `did:
|
|
411
|
+
* Convert did to address: remove `did:{method}:` prefix.
|
|
412
|
+
* Strips only the first layer prefix (anchored regex).
|
|
411
413
|
*/
|
|
412
414
|
function toAddress(did) {
|
|
413
|
-
return did.replace(
|
|
415
|
+
return did.replace(DID_PREFIX_PATTERN, "");
|
|
414
416
|
}
|
|
415
417
|
/**
|
|
416
|
-
* Convert address to did: prepend `did:
|
|
418
|
+
* Convert address to did: prepend `did:{method}:` prefix.
|
|
419
|
+
* @param address - bare address or full DID
|
|
420
|
+
* @param method - DID method (default: 'abt')
|
|
417
421
|
*/
|
|
418
|
-
function toDid(address) {
|
|
419
|
-
return
|
|
422
|
+
function toDid(address, method = "abt") {
|
|
423
|
+
return `did:${method}:${toAddress(address)}`;
|
|
420
424
|
}
|
|
421
425
|
function isSameDid(a, b) {
|
|
422
426
|
return toAddress(a).toLowerCase() === toAddress(b).toLowerCase();
|
|
423
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* Extract the method from a DID string.
|
|
430
|
+
* @returns The method string, or null if no prefix found
|
|
431
|
+
*/
|
|
432
|
+
function extractMethod(did) {
|
|
433
|
+
if (typeof did !== "string" || !did) return null;
|
|
434
|
+
const match = did.match(/^did:([a-z0-9]+):/);
|
|
435
|
+
if (match) return match[1];
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
424
438
|
function formatTxType(type) {
|
|
425
439
|
return upperFirst(camelCase(type));
|
|
426
440
|
}
|
|
427
441
|
|
|
428
442
|
//#endregion
|
|
429
|
-
export { BN, UUID, bytesToHex, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
|
443
|
+
export { BN, UUID, bytesToHex, extractMethod, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
package/lib/index.cjs
CHANGED
|
@@ -29,7 +29,6 @@ let utf8 = require("utf8");
|
|
|
29
29
|
utf8 = require_rolldown_runtime.__toESM(utf8);
|
|
30
30
|
|
|
31
31
|
//#region src/index.ts
|
|
32
|
-
const DID_PREFIX = "did:abt:";
|
|
33
32
|
const zero = new require_bn.BN(0);
|
|
34
33
|
const negative1 = new require_bn.BN(-1);
|
|
35
34
|
const base58btc = {
|
|
@@ -420,21 +419,36 @@ function fromBase64(v) {
|
|
|
420
419
|
if (typeof v !== "string") throw new Error("fromBase64 requires input to be a string");
|
|
421
420
|
return Buffer.from(base64_url.default.unescape(v), "base64");
|
|
422
421
|
}
|
|
422
|
+
/** Pattern matching any did:{method}: prefix where method is [a-z0-9]+ */
|
|
423
|
+
const DID_PREFIX_PATTERN = /^did:[a-z0-9]+:/;
|
|
423
424
|
/**
|
|
424
|
-
* Convert did to address: remove `did:
|
|
425
|
+
* Convert did to address: remove `did:{method}:` prefix.
|
|
426
|
+
* Strips only the first layer prefix (anchored regex).
|
|
425
427
|
*/
|
|
426
428
|
function toAddress(did) {
|
|
427
|
-
return did.replace(
|
|
429
|
+
return did.replace(DID_PREFIX_PATTERN, "");
|
|
428
430
|
}
|
|
429
431
|
/**
|
|
430
|
-
* Convert address to did: prepend `did:
|
|
432
|
+
* Convert address to did: prepend `did:{method}:` prefix.
|
|
433
|
+
* @param address - bare address or full DID
|
|
434
|
+
* @param method - DID method (default: 'abt')
|
|
431
435
|
*/
|
|
432
|
-
function toDid(address) {
|
|
433
|
-
return
|
|
436
|
+
function toDid(address, method = "abt") {
|
|
437
|
+
return `did:${method}:${toAddress(address)}`;
|
|
434
438
|
}
|
|
435
439
|
function isSameDid(a, b) {
|
|
436
440
|
return toAddress(a).toLowerCase() === toAddress(b).toLowerCase();
|
|
437
441
|
}
|
|
442
|
+
/**
|
|
443
|
+
* Extract the method from a DID string.
|
|
444
|
+
* @returns The method string, or null if no prefix found
|
|
445
|
+
*/
|
|
446
|
+
function extractMethod(did) {
|
|
447
|
+
if (typeof did !== "string" || !did) return null;
|
|
448
|
+
const match = did.match(/^did:([a-z0-9]+):/);
|
|
449
|
+
if (match) return match[1];
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
438
452
|
function formatTxType(type) {
|
|
439
453
|
return (0, lodash_upperFirst.default)((0, lodash_camelCase.default)(type));
|
|
440
454
|
}
|
|
@@ -443,6 +457,7 @@ function formatTxType(type) {
|
|
|
443
457
|
exports.BN = require_bn.BN;
|
|
444
458
|
exports.UUID = UUID;
|
|
445
459
|
exports.bytesToHex = bytesToHex;
|
|
460
|
+
exports.extractMethod = extractMethod;
|
|
446
461
|
exports.formatTxType = formatTxType;
|
|
447
462
|
exports.fromBase58 = fromBase58;
|
|
448
463
|
exports.fromBase64 = fromBase64;
|
package/lib/index.d.cts
CHANGED
|
@@ -210,14 +210,22 @@ declare function toBase64(v: any, escape?: boolean): string;
|
|
|
210
210
|
*/
|
|
211
211
|
declare function fromBase64(v: string): Buffer;
|
|
212
212
|
/**
|
|
213
|
-
* Convert did to address: remove `did:
|
|
213
|
+
* Convert did to address: remove `did:{method}:` prefix.
|
|
214
|
+
* Strips only the first layer prefix (anchored regex).
|
|
214
215
|
*/
|
|
215
216
|
declare function toAddress(did: string): string;
|
|
216
217
|
/**
|
|
217
|
-
* Convert address to did: prepend `did:
|
|
218
|
+
* Convert address to did: prepend `did:{method}:` prefix.
|
|
219
|
+
* @param address - bare address or full DID
|
|
220
|
+
* @param method - DID method (default: 'abt')
|
|
218
221
|
*/
|
|
219
|
-
declare function toDid(address: string): string;
|
|
222
|
+
declare function toDid(address: string, method?: string): string;
|
|
220
223
|
declare function isSameDid(a: string, b: string): boolean;
|
|
224
|
+
/**
|
|
225
|
+
* Extract the method from a DID string.
|
|
226
|
+
* @returns The method string, or null if no prefix found
|
|
227
|
+
*/
|
|
228
|
+
declare function extractMethod(did: unknown): string | null;
|
|
221
229
|
declare function formatTxType(type: string): Capitalize<string>;
|
|
222
230
|
//#endregion
|
|
223
|
-
export { BN, BytesType, EncodingType, KeyPairType, UUID, bytesToHex, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
|
231
|
+
export { BN, BytesType, EncodingType, KeyPairType, UUID, bytesToHex, extractMethod, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/util",
|
|
3
|
-
"version": "1.29.
|
|
3
|
+
"version": "1.29.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "utils shared across multiple forge js libs, works in both node.js and browser",
|
|
6
6
|
"keywords": [
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@ocap/types": "1.29.
|
|
15
|
+
"@ocap/types": "1.29.19",
|
|
16
16
|
"@types/bn.js": "^5.2.0",
|
|
17
17
|
"base64-url": "^2.3.3",
|
|
18
18
|
"bn.js": "5.2.3",
|