@ddd-qc/cell-proxy 0.26.1 → 0.26.2
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/dist/ZomeProxy.d.ts +2 -0
- package/dist/ZomeProxy.js +12 -22
- package/dist/ZomeProxy.js.map +1 -1
- package/dist/hash.d.ts +108 -0
- package/dist/hash.js +12 -4
- package/dist/hash.js.map +1 -1
- package/package.json +1 -1
package/dist/ZomeProxy.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ export declare abstract class ZomeProxy extends ZomeProxy_base {
|
|
|
29
29
|
protected _cellProxy: CellProxy;
|
|
30
30
|
/** Ctor */
|
|
31
31
|
constructor(_cellProxy: CellProxy, zomeName?: ZomeName);
|
|
32
|
+
/** */
|
|
33
|
+
private validateFunctionName;
|
|
32
34
|
/** Helper for calling a zome function on its zome */
|
|
33
35
|
call(fnName: FunctionName, payload: any, maybeSecret?: CapSecret, timeout?: number): Promise<any>;
|
|
34
36
|
/** Helper for calling a zome function on its zome */
|
package/dist/ZomeProxy.js
CHANGED
|
@@ -18,42 +18,32 @@ export class ZomeProxy extends CellMixin(ZomeSpecific) {
|
|
|
18
18
|
}
|
|
19
19
|
this._cell = _cellProxy.cell;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
/** */
|
|
22
|
+
validateFunctionName(fnName) {
|
|
23
|
+
//const fnNames: FunctionName[] = this.fnNames.map(([a, b]) => b);
|
|
24
|
+
//if (!fnNames.includes(fnName)) {
|
|
25
|
+
if (!this.constructor.FN_NAMES.includes(fnName)) {
|
|
26
|
+
throw Error(`Function "${fnName}()" not part of zome "${this.zomeName}"`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
29
|
/** Helper for calling a zome function on its zome */
|
|
30
30
|
async call(fnName, payload, maybeSecret, timeout) {
|
|
31
31
|
//console.log("ZomeProxy.call", this.zomeName)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
const cap_secret = maybeSecret
|
|
36
|
-
? maybeSecret
|
|
37
|
-
// : this._signingProps
|
|
38
|
-
// ? this._signingProps.capSecret
|
|
39
|
-
: null;
|
|
32
|
+
this.validateFunctionName(fnName);
|
|
33
|
+
const cap_secret = maybeSecret ? maybeSecret : null;
|
|
40
34
|
return this._cellProxy.callZome(this.zomeName, fnName, payload, cap_secret, timeout);
|
|
41
35
|
}
|
|
42
36
|
/** Helper for calling a zome function on its zome */
|
|
43
37
|
async callBlocking(fnName, payload, maybeSecret, timeout) {
|
|
44
38
|
//console.log("ZomeProxy.call", this.zomeName)
|
|
45
|
-
|
|
46
|
-
return Promise.reject(`Function "${fnName}()" not part of zome "${this.zomeName}"`);
|
|
47
|
-
}
|
|
39
|
+
this.validateFunctionName(fnName);
|
|
48
40
|
const cap_secret = maybeSecret ? maybeSecret : null;
|
|
49
41
|
return this._cellProxy.callZomeBlocking(this.zomeName, fnName, payload, cap_secret, timeout);
|
|
50
42
|
}
|
|
51
43
|
/** Helper for calling a zome function on its zome */
|
|
52
44
|
async callZomeBlockPostCommit(entryType, fnName, payload, maybeSecret, timeout) {
|
|
53
45
|
//console.log("ZomeProxy.call", this.zomeName)
|
|
54
|
-
|
|
55
|
-
return Promise.reject(`Function "${fnName}()" not part of zome "${this.zomeName}"`);
|
|
56
|
-
}
|
|
46
|
+
this.validateFunctionName(fnName);
|
|
57
47
|
const cap_secret = maybeSecret ? maybeSecret : null;
|
|
58
48
|
return this._cellProxy.callZomeBlockPostCommit(entryType, this.zomeName, fnName, payload, cap_secret, timeout);
|
|
59
49
|
}
|
package/dist/ZomeProxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ZomeProxy.js","sourceRoot":"","sources":["../src/ZomeProxy.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAE,YAAY,EAAC,MAAM,U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U,EAAE,OAAO,CAAC,CAAC;IACjH,CAAC;IAGD,MAAM;IACN,KAAK,CAAC,QAAQ;QACZ,OAAO,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrD,CAAC;IAED,MAAM;IACN,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IAED,MAAM;IACN,KAAK,CAAC,aAAa,CAAC,QAAkB;QACpC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IACjD,CAAC;CACF","sourcesContent":["import {CapSecret, FunctionName, ZomeName} from \"@holochain/client\";\nimport {CellMixin, ZomeSpecific} from \"./mixins\";\nimport {CellProxy} from \"./CellProxy\";\nimport {DnaInfo, EntryDef, ZomeInfo} from \"./types\";\nimport {Dictionary} from \"./utils\";\n\nexport type ZomeProxyConstructor = {new(cellProxy: CellProxy, zomeName?: ZomeName): ZomeProxy} & typeof ZomeSpecific;\n\n\n/**\n * ABC for representing the zome function bindings of a Zome.\n * It holds the zomeName and reference to a CellProxy.\n */\nexport abstract class ZomeProxy extends CellMixin(ZomeSpecific) {\n\n /** Ctor */\n constructor(protected _cellProxy: CellProxy, zomeName?: ZomeName) {\n super();\n if (zomeName) {\n this.zomeName = zomeName;\n } else {\n if (!this.defaultZomeName) {\n throw Error(\"zomeName not defined in ZomeProxy subclass \" + this.constructor.name);\n }\n }\n this._cell = _cellProxy.cell;\n }\n\n\n /** */\n private validateFunctionName(fnName: FunctionName): void {\n //const fnNames: FunctionName[] = this.fnNames.map(([a, b]) => b);\n //if (!fnNames.includes(fnName)) {\n if (!(this.constructor as any).FN_NAMES.includes(fnName)) {\n throw Error(`Function \"${fnName}()\" not part of zome \"${this.zomeName}\"`);\n }\n }\n\n /** Helper for calling a zome function on its zome */\n async call(fnName: FunctionName, payload: any, maybeSecret?: CapSecret, timeout?: number): Promise<any> {\n //console.log(\"ZomeProxy.call\", this.zomeName)\n this.validateFunctionName(fnName);\n const cap_secret = maybeSecret ? maybeSecret : null;\n return this._cellProxy.callZome(this.zomeName, fnName, payload, cap_secret, timeout);\n }\n\n\n /** Helper for calling a zome function on its zome */\n protected async callBlocking(fnName: FunctionName, payload: any, maybeSecret?: CapSecret, timeout?: number): Promise<any> {\n //console.log(\"ZomeProxy.call\", this.zomeName)\n this.validateFunctionName(fnName);\n const cap_secret = maybeSecret ? maybeSecret : null;\n return this._cellProxy.callZomeBlocking(this.zomeName, fnName, payload, cap_secret, timeout);\n }\n\n\n /** Helper for calling a zome function on its zome */\n protected async callZomeBlockPostCommit(entryType: string, fnName: FunctionName, payload: any, maybeSecret?: CapSecret, timeout?: number): Promise<any> {\n //console.log(\"ZomeProxy.call\", this.zomeName)\n this.validateFunctionName(fnName);\n const cap_secret = maybeSecret ? maybeSecret : null;\n return this._cellProxy.callZomeBlockPostCommit(entryType, this.zomeName, fnName, payload, cap_secret, timeout);\n }\n\n\n /** */\n async zomeInfo(): Promise<ZomeInfo> {\n return this._cellProxy.callZomeInfo(this.zomeName);\n }\n\n /** */\n async dnaInfo(): Promise<DnaInfo> {\n return this._cellProxy.callDnaInfo(this.zomeName);\n }\n\n /** */\n async callEntryDefs(zomeName: ZomeName): Promise<Dictionary<EntryDef>> {\n return this._cellProxy.callEntryDefs(zomeName);\n }\n}\n"]}
|
package/dist/hash.d.ts
CHANGED
|
@@ -53,6 +53,24 @@ export declare function createHolochainId(hashType: HoloHashType): {
|
|
|
53
53
|
toString(): string;
|
|
54
54
|
print(): string;
|
|
55
55
|
};
|
|
56
|
+
/** */
|
|
57
|
+
from<T extends {
|
|
58
|
+
readonly b64: string;
|
|
59
|
+
readonly type: HoloHashType;
|
|
60
|
+
readonly hash: Uint8Array;
|
|
61
|
+
/** First 8 chars of the Core */
|
|
62
|
+
readonly short: string;
|
|
63
|
+
toString(): string;
|
|
64
|
+
print(): string;
|
|
65
|
+
}>(start: T): {
|
|
66
|
+
readonly b64: string;
|
|
67
|
+
readonly type: HoloHashType;
|
|
68
|
+
readonly hash: Uint8Array;
|
|
69
|
+
/** First 8 chars of the Core */
|
|
70
|
+
readonly short: string;
|
|
71
|
+
toString(): string;
|
|
72
|
+
print(): string;
|
|
73
|
+
};
|
|
56
74
|
};
|
|
57
75
|
declare const ActionId_base: {
|
|
58
76
|
new (input: string | Uint8Array): {
|
|
@@ -64,6 +82,24 @@ declare const ActionId_base: {
|
|
|
64
82
|
toString(): string;
|
|
65
83
|
print(): string;
|
|
66
84
|
};
|
|
85
|
+
/** */
|
|
86
|
+
from<T extends {
|
|
87
|
+
readonly b64: string;
|
|
88
|
+
readonly type: HoloHashType;
|
|
89
|
+
readonly hash: Uint8Array;
|
|
90
|
+
/** First 8 chars of the Core */
|
|
91
|
+
readonly short: string;
|
|
92
|
+
toString(): string;
|
|
93
|
+
print(): string;
|
|
94
|
+
}>(start: T): {
|
|
95
|
+
readonly b64: string;
|
|
96
|
+
readonly type: HoloHashType;
|
|
97
|
+
readonly hash: Uint8Array;
|
|
98
|
+
/** First 8 chars of the Core */
|
|
99
|
+
readonly short: string;
|
|
100
|
+
toString(): string;
|
|
101
|
+
print(): string;
|
|
102
|
+
};
|
|
67
103
|
};
|
|
68
104
|
export declare class ActionId extends ActionId_base {
|
|
69
105
|
}
|
|
@@ -77,6 +113,24 @@ declare const AgentId_base: {
|
|
|
77
113
|
toString(): string;
|
|
78
114
|
print(): string;
|
|
79
115
|
};
|
|
116
|
+
/** */
|
|
117
|
+
from<T extends {
|
|
118
|
+
readonly b64: string;
|
|
119
|
+
readonly type: HoloHashType;
|
|
120
|
+
readonly hash: Uint8Array;
|
|
121
|
+
/** First 8 chars of the Core */
|
|
122
|
+
readonly short: string;
|
|
123
|
+
toString(): string;
|
|
124
|
+
print(): string;
|
|
125
|
+
}>(start: T): {
|
|
126
|
+
readonly b64: string;
|
|
127
|
+
readonly type: HoloHashType;
|
|
128
|
+
readonly hash: Uint8Array;
|
|
129
|
+
/** First 8 chars of the Core */
|
|
130
|
+
readonly short: string;
|
|
131
|
+
toString(): string;
|
|
132
|
+
print(): string;
|
|
133
|
+
};
|
|
80
134
|
};
|
|
81
135
|
export declare class AgentId extends AgentId_base {
|
|
82
136
|
}
|
|
@@ -90,6 +144,24 @@ declare const DnaId_base: {
|
|
|
90
144
|
toString(): string;
|
|
91
145
|
print(): string;
|
|
92
146
|
};
|
|
147
|
+
/** */
|
|
148
|
+
from<T extends {
|
|
149
|
+
readonly b64: string;
|
|
150
|
+
readonly type: HoloHashType;
|
|
151
|
+
readonly hash: Uint8Array;
|
|
152
|
+
/** First 8 chars of the Core */
|
|
153
|
+
readonly short: string;
|
|
154
|
+
toString(): string;
|
|
155
|
+
print(): string;
|
|
156
|
+
}>(start: T): {
|
|
157
|
+
readonly b64: string;
|
|
158
|
+
readonly type: HoloHashType;
|
|
159
|
+
readonly hash: Uint8Array;
|
|
160
|
+
/** First 8 chars of the Core */
|
|
161
|
+
readonly short: string;
|
|
162
|
+
toString(): string;
|
|
163
|
+
print(): string;
|
|
164
|
+
};
|
|
93
165
|
};
|
|
94
166
|
export declare class DnaId extends DnaId_base {
|
|
95
167
|
}
|
|
@@ -103,6 +175,24 @@ declare const EntryId_base: {
|
|
|
103
175
|
toString(): string;
|
|
104
176
|
print(): string;
|
|
105
177
|
};
|
|
178
|
+
/** */
|
|
179
|
+
from<T extends {
|
|
180
|
+
readonly b64: string;
|
|
181
|
+
readonly type: HoloHashType;
|
|
182
|
+
readonly hash: Uint8Array;
|
|
183
|
+
/** First 8 chars of the Core */
|
|
184
|
+
readonly short: string;
|
|
185
|
+
toString(): string;
|
|
186
|
+
print(): string;
|
|
187
|
+
}>(start: T): {
|
|
188
|
+
readonly b64: string;
|
|
189
|
+
readonly type: HoloHashType;
|
|
190
|
+
readonly hash: Uint8Array;
|
|
191
|
+
/** First 8 chars of the Core */
|
|
192
|
+
readonly short: string;
|
|
193
|
+
toString(): string;
|
|
194
|
+
print(): string;
|
|
195
|
+
};
|
|
106
196
|
};
|
|
107
197
|
export declare class EntryId extends EntryId_base {
|
|
108
198
|
}
|
|
@@ -116,6 +206,24 @@ declare const ExternalId_base: {
|
|
|
116
206
|
toString(): string;
|
|
117
207
|
print(): string;
|
|
118
208
|
};
|
|
209
|
+
/** */
|
|
210
|
+
from<T extends {
|
|
211
|
+
readonly b64: string;
|
|
212
|
+
readonly type: HoloHashType;
|
|
213
|
+
readonly hash: Uint8Array;
|
|
214
|
+
/** First 8 chars of the Core */
|
|
215
|
+
readonly short: string;
|
|
216
|
+
toString(): string;
|
|
217
|
+
print(): string;
|
|
218
|
+
}>(start: T): {
|
|
219
|
+
readonly b64: string;
|
|
220
|
+
readonly type: HoloHashType;
|
|
221
|
+
readonly hash: Uint8Array;
|
|
222
|
+
/** First 8 chars of the Core */
|
|
223
|
+
readonly short: string;
|
|
224
|
+
toString(): string;
|
|
225
|
+
print(): string;
|
|
226
|
+
};
|
|
119
227
|
};
|
|
120
228
|
export declare class ExternalId extends ExternalId_base {
|
|
121
229
|
}
|
package/dist/hash.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { decodeHashFromBase64, encodeHashToBase64 } from "@holochain/client";
|
|
2
|
-
//import {AbstractConstructor, CellMixin, Empty, GConstructor, ZomeMixin, ZomeSpecific} from "./mixins";
|
|
3
|
-
//import {Cell} from "./cell";
|
|
1
|
+
import { decodeHashFromBase64, dhtLocationFrom32, encodeHashToBase64, HASH_TYPE_PREFIX, } from "@holochain/client";
|
|
4
2
|
/**
|
|
5
3
|
* Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64
|
|
6
4
|
*/
|
|
@@ -121,9 +119,19 @@ export function createHolochainId(hashType) {
|
|
|
121
119
|
super(input, hashType);
|
|
122
120
|
const type = getHashType(this.b64);
|
|
123
121
|
if (hashType != type) {
|
|
124
|
-
throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got' + type);
|
|
122
|
+
throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got: ' + type);
|
|
125
123
|
}
|
|
126
124
|
}
|
|
125
|
+
/** */
|
|
126
|
+
static from(start) {
|
|
127
|
+
const core = Uint8Array.from(start.hash.slice(3, 35));
|
|
128
|
+
const newHash = Uint8Array.from([
|
|
129
|
+
...HASH_TYPE_PREFIX[hashType],
|
|
130
|
+
...core,
|
|
131
|
+
...dhtLocationFrom32(core),
|
|
132
|
+
]);
|
|
133
|
+
return new AHoloId(newHash);
|
|
134
|
+
}
|
|
127
135
|
}
|
|
128
136
|
/** */
|
|
129
137
|
return AHoloId;
|
package/dist/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAE,kBAAkB,EAAwB,MAAM,mBAAmB,CAAC;AAClG,wGAAwG;AACxG,8BAA8B;AAG9B;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAQ;IAC/B,2BAA2B;IAC3B,IAAI,GAAG,YAAY,UAAU,EAAE;QAC7B,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAC;KAChC;SAAM;QACL,uCAAuC;QACvC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,MAAM,YAAY,GAChB,GAAG,CAAC,MAAM,GAAG,CAAC;gBACd,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBAClB,OAAO,KAAK,YAAY,UAAU,CAAC;gBACrC,CAAC,CAAC,CAAC;YACL,IAAI,YAAY,EAAE;gBAChB,IAAI,MAAM,GAAG,EAAE,CAAC;gBAChB,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE;oBACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACtC;gBACD,OAAO,MAAM,CAAC;aACf;SACF;KACF;IACD,OAAO,GAAG,CAAC;AACb,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AC;CAAG;AACjE,MAAM,OAAO,OAAQ,SAAQ,iBAAiB,CAAC,YAAY,CAAC,KAAK,CAAC;CAAG;AACrE,MAAM,OAAO,UAAW,SAAQ,iBAAiB,CAAC,YAAY,CAAC,QAAQ,CAAC;CAAG;AAM3E,MAAM;AACN,MAAM,UAAU,SAAS,CAAC,KAA6B;IACrD,IAAI;QACF,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;QACrC,OAAO,QAAQ,CAAC;KACjB;IAAC,OAAM,CAAC,EAAE;QACP,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO,OAAO,CAAC;KAClB;AACH,CAAC;AAGD,MAAM;AACN,MAAM,UAAU,cAAc,CAAC,KAA6B;IAC1D,IAAI;QACF,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAC/B,OAAO,KAAK,CAAC;KACd;IAAC,OAAM,CAAC,EAAE;QACT,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;QACzC,OAAO,UAAU,CAAC;KACnB;AACH,CAAC","sourcesContent":["import {decodeHashFromBase64, encodeHashToBase64, HoloHash, HoloHashB64} from \"@holochain/client\";\r\n//import {AbstractConstructor, CellMixin, Empty, GConstructor, ZomeMixin, ZomeSpecific} from \"./mixins\";\r\n//import {Cell} from \"./cell\";\r\n\r\n\r\n/**\r\n * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64\r\n */\r\nexport function anyToB64(obj: any): any {\r\n /** Check if it's a hash */\r\n if (obj instanceof Uint8Array) {\r\n return encodeHashToBase64(obj);\r\n } else {\r\n /** Check if it's an array of hashes */\r\n if (Array.isArray(obj)) {\r\n const isUint8Array =\r\n obj.length > 0 &&\r\n obj.every((value) => {\r\n return value instanceof Uint8Array;\r\n });\r\n if (isUint8Array) {\r\n let result = [];\r\n for (const cur of obj) {\r\n result.push(encodeHashToBase64(cur));\r\n }\r\n return result;\r\n }\r\n }\r\n }\r\n return obj;\r\n}\r\n\r\n\r\n/** ------------------------------------------------------------------------------------------------------------------*/\r\n\r\nexport enum HoloHashType {\r\n Action = \"Action\",\r\n Agent = \"Agent\",\r\n //DhtOp = \"DhtOp\",\r\n Dna = \"Dna\",\r\n Entry = \"Entry\",\r\n External = \"External\",\r\n Network = \"Network\",\r\n //Warrent = \"Warrent\",\r\n Wasm = \"Wasm\",\r\n}\r\n\r\nexport const HASH_TYPE_PREFIX_B64 = {\r\n Action: \"uhCkk\",\r\n Agent: \"uhCAk\",\r\n Dna: \"uhC0k\",\r\n //DhtOp: \"hCQk\",\r\n Entry: \"uhCEk\",\r\n External: \"uhC8k\",\r\n Network: \"uhCIk\",\r\n //Warrent: \"Warrent\",\r\n Wasm: \"uhCok\",\r\n};\r\n\r\nexport function getHashType(hash: HoloHashB64): HoloHashType {\r\n const hashExt = hash.slice(0, 5);\r\n const hashPrefixes = Object.values(HASH_TYPE_PREFIX_B64)\r\n for (let i = 0; i < hashPrefixes.length; i+= 1) {\r\n if (hashPrefixes[i] == hashExt) {\r\n return Object.keys(HASH_TYPE_PREFIX_B64)[i] as HoloHashType;\r\n }\r\n }\r\n throw Error(\"Unknown hash type\");\r\n}\r\n\r\n\r\n/** */\r\nexport function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType) {\r\n const slice = hash.slice(0, 5);\r\n const prefix = HASH_TYPE_PREFIX_B64[hashType];\r\n for (let i = 0; i < prefix.length; i++) {\r\n if (slice[i] !== prefix[i]) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n}\r\n\r\n\r\nexport function hasHoloHashType(hash: HoloHashB64): boolean {\r\n return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));\r\n}\r\n\r\n\r\n/** */\r\nexport function validateHashB64(hash: HoloHashB64) {\r\n if (!hash || typeof(hash) != 'string') {\r\n throw new Error(\"The hash must be a valid string\");\r\n }\r\n if (hash.length !== 53) {\r\n throw new Error(\"The hash must be exactly 53 characters long.\");\r\n }\r\n if (!hasHoloHashType(hash)) {\r\n throw new Error(\"The hash must have a valid HoloHash type.\");\r\n\r\n }\r\n}\r\n\r\n\r\nexport function dec64(hash: HoloHashB64): HoloHash {\r\n validateHashB64(hash);\r\n return decodeHashFromBase64(hash);\r\n}\r\nexport function enc64(hash: HoloHash): HoloHashB64 {\r\n let b64 = encodeHashToBase64(hash);\r\n validateHashB64(b64);\r\n return b64;\r\n}\r\n\r\n\r\n/** HoloHash starts with 'u' has a type and is 53 chars long */\r\nexport abstract class HolochainId {\r\n public readonly b64: HoloHashB64;\r\n //private readonly hash: HoloHash;\r\n\r\n /** Validate */\r\n constructor(input: HoloHashB64 | HoloHash, public readonly type: HoloHashType) {\r\n if (typeof(input) != 'string') {\r\n input = encodeHashToBase64(input);\r\n }\r\n validateHashB64(input);\r\n this.b64 = input;\r\n }\r\n\r\n get hash(): HoloHash { return dec64(this.b64) }\r\n /** First 8 chars of the Core */\r\n get short(): string { return this.b64.slice(5, 13); }\r\n\r\n toString(): string {return this.b64;}\r\n\r\n print(): string { return `${this.short} (${this.type})`}\r\n}\r\n\r\n\r\n/** Mixin */\r\nexport function createHolochainId(hashType: HoloHashType) {\r\n class AHoloId extends HolochainId {\r\n constructor(input: HoloHashB64 | HoloHash) {\r\n super(input, hashType);\r\n const type = getHashType(this.b64);\r\n if (hashType != type) {\r\n throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got' + type);\r\n }\r\n }\r\n }\r\n /** */\r\n return AHoloId;\r\n}\r\n\r\nexport class ActionId extends createHolochainId(HoloHashType.Action) {}\r\nexport class AgentId extends createHolochainId(HoloHashType.Agent) {}\r\nexport class DnaId extends createHolochainId(HoloHashType.Dna) {}\r\nexport class EntryId extends createHolochainId(HoloHashType.Entry) {}\r\nexport class ExternalId extends createHolochainId(HoloHashType.External) {}\r\n\r\nexport type AnyDhtId = ActionId | EntryId;\r\nexport type AnyLinkableId = AnyDhtId | ExternalId;\r\n\r\n\r\n/** */\r\nexport function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId {\r\n try {\r\n const actionId = new ActionId(input);\r\n return actionId;\r\n } catch(e) {\r\n const entryId = new EntryId(input);\r\n return entryId;\r\n }\r\n}\r\n\r\n\r\n/** */\r\nexport function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId {\r\n try {\r\n const dhtId = intoDhtId(input);\r\n return dhtId;\r\n } catch(e) {\r\n const externalId = new ExternalId(input);\r\n return externalId;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAAE,gBAAgB,GAGnC,MAAM,mBAAmB,CAAC;AAG7B;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAQ;IAC/B,2BAA2B;IAC3B,IAAI,GAAG,YAAY,UAAU,EAAE;QAC7B,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAC;KAChC;SAAM;QACL,uCAAuC;QACvC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,MAAM,YAAY,GAChB,GAAG,CAAC,MAAM,GAAG,CAAC;gBACd,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBAClB,OAAO,KAAK,YAAY,UAAU,CAAC;gBACrC,CAAC,CAAC,CAAC;YACL,IAAI,YAAY,EAAE;gBAChB,IAAI,MAAM,GAAG,EAAE,CAAC;gBAChB,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE;oBACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACtC;gBACD,OAAO,MAAM,CAAC;aACf;SACF;KACF;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAGD,wHAAwH;AAExH,MAAM,CAAN,IAAY,YAUX;AAVD,WAAY,YAAY;IACtB,iCAAiB,CAA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AAO,CAAC,OAAO,CAAC,CAAA;QAC7B,CAAC;KAEF;IACD,MAAM;IACN,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,OAAO,QAAS,SAAQ,iBAAiB,CAAC,YAAY,CAAC,MAAM,CAAC;CAAG;AACvE,MAAM,OAAO,OAAQ,SAAQ,iBAAiB,CAAC,YAAY,CAAC,KAAK,CAAC;CAAG;AACrE,MAAM,OAAO,KAAM,SAAQ,iBAAiB,CAAC,YAAY,CAAC,GAAG,CAAC;CAAG;AACjE,MAAM,OAAO,OAAQ,SAAQ,iBAAiB,CAAC,YAAY,CAAC,KAAK,CAAC;CAAG;AACrE,MAAM,OAAO,UAAW,SAAQ,iBAAiB,CAAC,YAAY,CAAC,QAAQ,CAAC;CAAG;AAM3E,MAAM;AACN,MAAM,UAAU,SAAS,CAAC,KAA6B;IACrD,IAAI;QACF,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;QACrC,OAAO,QAAQ,CAAC;KACjB;IAAC,OAAM,CAAC,EAAE;QACP,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO,OAAO,CAAC;KAClB;AACH,CAAC;AAGD,MAAM;AACN,MAAM,UAAU,cAAc,CAAC,KAA6B;IAC1D,IAAI;QACF,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAC/B,OAAO,KAAK,CAAC;KACd;IAAC,OAAM,CAAC,EAAE;QACT,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;QACzC,OAAO,UAAU,CAAC;KACnB;AACH,CAAC","sourcesContent":["import {\r\n decodeHashFromBase64,\r\n dhtLocationFrom32,\r\n encodeHashToBase64, HASH_TYPE_PREFIX,\r\n HoloHash,\r\n HoloHashB64,\r\n } from \"@holochain/client\";\r\n\r\n\r\n/**\r\n * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64\r\n */\r\nexport function anyToB64(obj: any): any {\r\n /** Check if it's a hash */\r\n if (obj instanceof Uint8Array) {\r\n return encodeHashToBase64(obj);\r\n } else {\r\n /** Check if it's an array of hashes */\r\n if (Array.isArray(obj)) {\r\n const isUint8Array =\r\n obj.length > 0 &&\r\n obj.every((value) => {\r\n return value instanceof Uint8Array;\r\n });\r\n if (isUint8Array) {\r\n let result = [];\r\n for (const cur of obj) {\r\n result.push(encodeHashToBase64(cur));\r\n }\r\n return result;\r\n }\r\n }\r\n }\r\n return obj;\r\n}\r\n\r\n\r\n/** ------------------------------------------------------------------------------------------------------------------*/\r\n\r\nexport enum HoloHashType {\r\n Action = \"Action\",\r\n Agent = \"Agent\",\r\n //DhtOp = \"DhtOp\",\r\n Dna = \"Dna\",\r\n Entry = \"Entry\",\r\n External = \"External\",\r\n Network = \"Network\",\r\n //Warrent = \"Warrent\",\r\n Wasm = \"Wasm\",\r\n}\r\n\r\nexport const HASH_TYPE_PREFIX_B64 = {\r\n Action: \"uhCkk\",\r\n Agent: \"uhCAk\",\r\n Dna: \"uhC0k\",\r\n //DhtOp: \"hCQk\",\r\n Entry: \"uhCEk\",\r\n External: \"uhC8k\",\r\n Network: \"uhCIk\",\r\n //Warrent: \"Warrent\",\r\n Wasm: \"uhCok\",\r\n};\r\n\r\nexport function getHashType(hash: HoloHashB64): HoloHashType {\r\n const hashExt = hash.slice(0, 5);\r\n const hashPrefixes = Object.values(HASH_TYPE_PREFIX_B64)\r\n for (let i = 0; i < hashPrefixes.length; i+= 1) {\r\n if (hashPrefixes[i] == hashExt) {\r\n return Object.keys(HASH_TYPE_PREFIX_B64)[i] as HoloHashType;\r\n }\r\n }\r\n throw Error(\"Unknown hash type\");\r\n}\r\n\r\n\r\n/** */\r\nexport function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType) {\r\n const slice = hash.slice(0, 5);\r\n const prefix = HASH_TYPE_PREFIX_B64[hashType];\r\n for (let i = 0; i < prefix.length; i++) {\r\n if (slice[i] !== prefix[i]) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n}\r\n\r\n\r\nexport function hasHoloHashType(hash: HoloHashB64): boolean {\r\n return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));\r\n}\r\n\r\n\r\n/** */\r\nexport function validateHashB64(hash: HoloHashB64) {\r\n if (!hash || typeof(hash) != 'string') {\r\n throw new Error(\"The hash must be a valid string\");\r\n }\r\n if (hash.length !== 53) {\r\n throw new Error(\"The hash must be exactly 53 characters long.\");\r\n }\r\n if (!hasHoloHashType(hash)) {\r\n throw new Error(\"The hash must have a valid HoloHash type.\");\r\n\r\n }\r\n}\r\n\r\n\r\nexport function dec64(hash: HoloHashB64): HoloHash {\r\n validateHashB64(hash);\r\n return decodeHashFromBase64(hash);\r\n}\r\nexport function enc64(hash: HoloHash): HoloHashB64 {\r\n let b64 = encodeHashToBase64(hash);\r\n validateHashB64(b64);\r\n return b64;\r\n}\r\n\r\n\r\n/** HoloHash starts with 'u' has a type and is 53 chars long */\r\nexport abstract class HolochainId {\r\n public readonly b64: HoloHashB64;\r\n //private readonly hash: HoloHash;\r\n\r\n /** Validate */\r\n constructor(input: HoloHashB64 | HoloHash, public readonly type: HoloHashType) {\r\n if (typeof(input) != 'string') {\r\n input = encodeHashToBase64(input);\r\n }\r\n validateHashB64(input);\r\n this.b64 = input;\r\n }\r\n\r\n get hash(): HoloHash { return dec64(this.b64) }\r\n /** First 8 chars of the Core */\r\n get short(): string { return this.b64.slice(5, 13); }\r\n\r\n toString(): string {return this.b64;}\r\n\r\n print(): string { return `${this.short} (${this.type})`}\r\n}\r\n\r\n\r\n/** Mixin */\r\nexport function createHolochainId(hashType: HoloHashType) {\r\n class AHoloId extends HolochainId {\r\n constructor(input: HoloHashB64 | HoloHash) {\r\n super(input, hashType);\r\n const type = getHashType(this.b64);\r\n if (hashType != type) {\r\n throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got: ' + type);\r\n }\r\n }\r\n /** */\r\n static from<T extends AHoloId>(start: T): AHoloId {\r\n const core = Uint8Array.from(start.hash.slice(3, 35));\r\n const newHash = Uint8Array.from([\r\n ...HASH_TYPE_PREFIX[hashType],\r\n ...core,\r\n ...dhtLocationFrom32(core),\r\n ]);\r\n return new AHoloId(newHash)\r\n }\r\n\r\n }\r\n /** */\r\n return AHoloId;\r\n}\r\n\r\nexport class ActionId extends createHolochainId(HoloHashType.Action) {}\r\nexport class AgentId extends createHolochainId(HoloHashType.Agent) {}\r\nexport class DnaId extends createHolochainId(HoloHashType.Dna) {}\r\nexport class EntryId extends createHolochainId(HoloHashType.Entry) {}\r\nexport class ExternalId extends createHolochainId(HoloHashType.External) {}\r\n\r\nexport type AnyDhtId = ActionId | EntryId;\r\nexport type AnyLinkableId = AnyDhtId | ExternalId;\r\n\r\n\r\n/** */\r\nexport function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId {\r\n try {\r\n const actionId = new ActionId(input);\r\n return actionId;\r\n } catch(e) {\r\n const entryId = new EntryId(input);\r\n return entryId;\r\n }\r\n}\r\n\r\n\r\n/** */\r\nexport function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId {\r\n try {\r\n const dhtId = intoDhtId(input);\r\n return dhtId;\r\n } catch(e) {\r\n const externalId = new ExternalId(input);\r\n return externalId;\r\n }\r\n}\r\n"]}
|