@dignetwork/dig-sdk 0.0.1-alpha.174 → 0.0.1-alpha.177

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.
@@ -0,0 +1,23 @@
1
+ declare class Udi {
2
+ readonly chainName: string;
3
+ readonly storeId: Buffer;
4
+ readonly rootHash: Buffer | null;
5
+ readonly resourceKey: string | null;
6
+ static readonly nid: string;
7
+ static readonly namespace: string;
8
+ constructor(chainName: string, storeId: string | Buffer, rootHash?: string | Buffer | null, resourceKey?: string | null);
9
+ static convertToBuffer(input: string | Buffer): Buffer;
10
+ static isHex(input: string): boolean;
11
+ static isBase32(input: string): boolean;
12
+ withRootHash(rootHash: string | Buffer | null): Udi;
13
+ withResourceKey(resourceKey: string | null): Udi;
14
+ static fromUrn(urn: string): Udi;
15
+ toUrn(encoding?: 'hex' | 'base32'): string;
16
+ bufferToString(buffer: Buffer, encoding: 'hex' | 'base32'): string;
17
+ equals(other: Udi): boolean;
18
+ toString(): string;
19
+ clone(): Udi;
20
+ hashCode(): string;
21
+ }
22
+ export { Udi };
23
+ //# sourceMappingURL=Udi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Udi.d.ts","sourceRoot":"","sources":["../../src/utils/Udi.ts"],"names":[],"mappings":"AAWA,cAAM,GAAG;IACL,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAS;IACpC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAoB;gBAGjD,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,GAAE,MAAM,GAAG,MAAM,GAAG,IAAW,EACvC,WAAW,GAAE,MAAM,GAAG,IAAW;IAWrC,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAgBtD,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIpC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIvC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,GAAG;IAInD,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,GAAG;IAIhD,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;IA4BhC,KAAK,CAAC,QAAQ,GAAE,KAAK,GAAG,QAAgB,GAAG,MAAM;IAgBjD,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM;IAMlE,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAS3B,QAAQ,IAAI,MAAM;IAIlB,KAAK,IAAI,GAAG;IAIZ,QAAQ,IAAI,MAAM;CAKrB;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Udi = void 0;
27
+ const urns = __importStar(require("urns"));
28
+ const crypto_1 = require("crypto");
29
+ const hi_base32_1 = require("hi-base32");
30
+ //
31
+ // This class encapsulates the concept of a Universal Data Identifier (UDI) which is a
32
+ // standardized way to identify resources across the distributed DIG mesh network.
33
+ // The UDI is formatted as follows:
34
+ // urn:dig:chainName:storeId:rootHash/resourceKey
35
+ // The UDI can be used to uniquely identify resources across the DIG network.
36
+ //
37
+ class Udi {
38
+ constructor(chainName, storeId, rootHash = null, resourceKey = null) {
39
+ if (!storeId) {
40
+ throw new Error("storeId cannot be empty");
41
+ }
42
+ this.chainName = chainName || "chia";
43
+ this.storeId = Udi.convertToBuffer(storeId);
44
+ this.rootHash = rootHash ? Udi.convertToBuffer(rootHash) : null;
45
+ this.resourceKey = resourceKey;
46
+ }
47
+ static convertToBuffer(input) {
48
+ if (Buffer.isBuffer(input)) {
49
+ return input;
50
+ }
51
+ if (Udi.isHex(input)) {
52
+ return Buffer.from(input, 'hex');
53
+ }
54
+ if (Udi.isBase32(input)) {
55
+ return Buffer.from((0, hi_base32_1.decode)(input, false)); // Decode as UTF-8
56
+ }
57
+ throw new Error("Invalid input encoding. Must be 32-byte hex or Base32 string.");
58
+ }
59
+ static isHex(input) {
60
+ return /^[a-fA-F0-9]{64}$/.test(input);
61
+ }
62
+ static isBase32(input) {
63
+ return /^[a-z2-7]{52}$/.test(input.toLowerCase());
64
+ }
65
+ withRootHash(rootHash) {
66
+ return new Udi(this.chainName, this.storeId, rootHash, this.resourceKey);
67
+ }
68
+ withResourceKey(resourceKey) {
69
+ return new Udi(this.chainName, this.storeId, this.rootHash, resourceKey);
70
+ }
71
+ static fromUrn(urn) {
72
+ const parsedUrn = urns.parseURN(urn);
73
+ if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
74
+ throw new Error(`Invalid nid: ${parsedUrn.nid}`);
75
+ }
76
+ const parts = parsedUrn.nss.split(':');
77
+ if (parts.length < 2) {
78
+ throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
79
+ }
80
+ const chainName = parts[0];
81
+ const storeId = parts[1].split('/')[0];
82
+ let rootHash = null;
83
+ if (parts.length > 2) {
84
+ rootHash = parts[2].split('/')[0];
85
+ }
86
+ const pathParts = parsedUrn.nss.split('/');
87
+ let resourceKey = null;
88
+ if (pathParts.length > 1) {
89
+ resourceKey = pathParts.slice(1).join('/');
90
+ }
91
+ return new Udi(chainName, storeId, rootHash, resourceKey);
92
+ }
93
+ toUrn(encoding = 'hex') {
94
+ const storeIdStr = this.bufferToString(this.storeId, encoding);
95
+ let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
96
+ if (this.rootHash) {
97
+ const rootHashStr = this.bufferToString(this.rootHash, encoding);
98
+ urn += `:${rootHashStr}`;
99
+ }
100
+ if (this.resourceKey) {
101
+ urn += `/${this.resourceKey}`;
102
+ }
103
+ return urn;
104
+ }
105
+ bufferToString(buffer, encoding) {
106
+ return encoding === 'hex'
107
+ ? buffer.toString('hex')
108
+ : (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, '');
109
+ }
110
+ equals(other) {
111
+ return (this.storeId.equals(other.storeId) &&
112
+ this.chainName === other.chainName &&
113
+ (this.rootHash && other.rootHash ? this.rootHash.equals(other.rootHash) : this.rootHash === other.rootHash) &&
114
+ this.resourceKey === other.resourceKey);
115
+ }
116
+ toString() {
117
+ return this.toUrn();
118
+ }
119
+ clone() {
120
+ return new Udi(this.chainName, this.storeId, this.rootHash, this.resourceKey);
121
+ }
122
+ hashCode() {
123
+ const hash = (0, crypto_1.createHash)('sha256');
124
+ hash.update(this.toUrn());
125
+ return hash.digest('hex');
126
+ }
127
+ }
128
+ exports.Udi = Udi;
129
+ Udi.nid = "dig";
130
+ Udi.namespace = `urn:${Udi.nid}`;
@@ -14,4 +14,5 @@ export * from './Environment';
14
14
  export * from './promiseUtils';
15
15
  export * from './PeerRanker';
16
16
  export * from './DigCache';
17
+ export * from './Udi';
17
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC"}
@@ -30,3 +30,4 @@ __exportStar(require("./Environment"), exports);
30
30
  __exportStar(require("./promiseUtils"), exports);
31
31
  __exportStar(require("./PeerRanker"), exports);
32
32
  __exportStar(require("./DigCache"), exports);
33
+ __exportStar(require("./Udi"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.174",
3
+ "version": "0.0.1-alpha.177",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -41,6 +41,7 @@
41
41
  "crypto-js": "^4.2.0",
42
42
  "figures": "^6.1.0",
43
43
  "fs-extra": "^11.2.0",
44
+ "hi-base32": "^0.5.1",
44
45
  "ignore": "^5.3.2",
45
46
  "inquirer": "^10.2.2",
46
47
  "lodash": "^4.17.21",
@@ -53,7 +54,9 @@
53
54
  "proper-lockfile": "^4.1.2",
54
55
  "superagent": "^10.1.0",
55
56
  "redis": "^4.7.0",
56
- "unzipper": "^0.12.3"
57
+ "superagent": "^10.0.0",
58
+ "unzipper": "^0.12.3",
59
+ "urns": "^0.6.0"
57
60
  },
58
61
  "devDependencies": {
59
62
  "@types/archiver": "^6.0.2",