@ddd-qc/cell-proxy 0.26.2 → 0.26.4
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/hash.d.ts +26 -186
- package/dist/hash.js +61 -39
- package/dist/hash.js.map +1 -1
- package/package.json +3 -2
package/dist/hash.d.ts
CHANGED
|
@@ -27,205 +27,44 @@ export declare function getHashType(hash: HoloHashB64): HoloHashType;
|
|
|
27
27
|
export declare function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType): boolean;
|
|
28
28
|
export declare function hasHoloHashType(hash: HoloHashB64): boolean;
|
|
29
29
|
/** */
|
|
30
|
-
export declare function validateHashB64(hash: HoloHashB64): void;
|
|
30
|
+
export declare function validateHashB64(hash: HoloHashB64, hashType?: HoloHashType): void;
|
|
31
31
|
export declare function dec64(hash: HoloHashB64): HoloHash;
|
|
32
32
|
export declare function enc64(hash: HoloHash): HoloHashB64;
|
|
33
33
|
/** HoloHash starts with 'u' has a type and is 53 chars long */
|
|
34
34
|
export declare abstract class HolochainId {
|
|
35
|
-
readonly type: HoloHashType;
|
|
36
35
|
readonly b64: HoloHashB64;
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
static readonly HASH_TYPE: HoloHashType;
|
|
37
|
+
get hashType(): HoloHashType;
|
|
38
|
+
/** ctor: validate */
|
|
39
|
+
constructor(input: HoloHashB64 | HoloHash);
|
|
40
|
+
/** */
|
|
41
|
+
static empty<T extends HolochainId>(this: any): T;
|
|
42
|
+
/** */
|
|
43
|
+
static from<T extends HolochainId, Z extends HolochainId>(this: new (input: HoloHashB64 | HoloHash) => Z, start: T): Z;
|
|
39
44
|
get hash(): HoloHash;
|
|
40
45
|
/** First 8 chars of the Core */
|
|
41
46
|
get short(): string;
|
|
42
|
-
toString(): string;
|
|
43
47
|
print(): string;
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
readonly b64: string;
|
|
49
|
-
readonly type: HoloHashType;
|
|
50
|
-
readonly hash: Uint8Array;
|
|
51
|
-
/** First 8 chars of the Core */
|
|
52
|
-
readonly short: string;
|
|
53
|
-
toString(): string;
|
|
54
|
-
print(): string;
|
|
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
|
-
};
|
|
74
|
-
};
|
|
75
|
-
declare const ActionId_base: {
|
|
76
|
-
new (input: string | Uint8Array): {
|
|
77
|
-
readonly b64: string;
|
|
78
|
-
readonly type: HoloHashType;
|
|
79
|
-
readonly hash: Uint8Array;
|
|
80
|
-
/** First 8 chars of the Core */
|
|
81
|
-
readonly short: string;
|
|
82
|
-
toString(): string;
|
|
83
|
-
print(): string;
|
|
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
|
-
};
|
|
103
|
-
};
|
|
104
|
-
export declare class ActionId extends ActionId_base {
|
|
49
|
+
export declare class ActionId extends HolochainId {
|
|
50
|
+
static readonly HASH_TYPE = HoloHashType.Action;
|
|
51
|
+
action(): void;
|
|
105
52
|
}
|
|
106
|
-
declare
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
readonly type: HoloHashType;
|
|
110
|
-
readonly hash: Uint8Array;
|
|
111
|
-
/** First 8 chars of the Core */
|
|
112
|
-
readonly short: string;
|
|
113
|
-
toString(): string;
|
|
114
|
-
print(): string;
|
|
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
|
-
};
|
|
134
|
-
};
|
|
135
|
-
export declare class AgentId extends AgentId_base {
|
|
53
|
+
export declare class AgentId extends HolochainId {
|
|
54
|
+
static readonly HASH_TYPE = HoloHashType.Agent;
|
|
55
|
+
agent(): void;
|
|
136
56
|
}
|
|
137
|
-
declare
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
readonly type: HoloHashType;
|
|
141
|
-
readonly hash: Uint8Array;
|
|
142
|
-
/** First 8 chars of the Core */
|
|
143
|
-
readonly short: string;
|
|
144
|
-
toString(): string;
|
|
145
|
-
print(): string;
|
|
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
|
-
};
|
|
165
|
-
};
|
|
166
|
-
export declare class DnaId extends DnaId_base {
|
|
57
|
+
export declare class EntryId extends HolochainId {
|
|
58
|
+
static readonly HASH_TYPE = HoloHashType.Entry;
|
|
59
|
+
entry(): void;
|
|
167
60
|
}
|
|
168
|
-
declare
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
readonly type: HoloHashType;
|
|
172
|
-
readonly hash: Uint8Array;
|
|
173
|
-
/** First 8 chars of the Core */
|
|
174
|
-
readonly short: string;
|
|
175
|
-
toString(): string;
|
|
176
|
-
print(): string;
|
|
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
|
-
};
|
|
196
|
-
};
|
|
197
|
-
export declare class EntryId extends EntryId_base {
|
|
61
|
+
export declare class DnaId extends HolochainId {
|
|
62
|
+
static readonly HASH_TYPE = HoloHashType.Dna;
|
|
63
|
+
dna(): void;
|
|
198
64
|
}
|
|
199
|
-
declare
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
readonly type: HoloHashType;
|
|
203
|
-
readonly hash: Uint8Array;
|
|
204
|
-
/** First 8 chars of the Core */
|
|
205
|
-
readonly short: string;
|
|
206
|
-
toString(): string;
|
|
207
|
-
print(): string;
|
|
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
|
-
};
|
|
227
|
-
};
|
|
228
|
-
export declare class ExternalId extends ExternalId_base {
|
|
65
|
+
export declare class ExternalId extends HolochainId {
|
|
66
|
+
static readonly HASH_TYPE = HoloHashType.External;
|
|
67
|
+
external(): void;
|
|
229
68
|
}
|
|
230
69
|
export type AnyDhtId = ActionId | EntryId;
|
|
231
70
|
export type AnyLinkableId = AnyDhtId | ExternalId;
|
|
@@ -233,4 +72,5 @@ export type AnyLinkableId = AnyDhtId | ExternalId;
|
|
|
233
72
|
export declare function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId;
|
|
234
73
|
/** */
|
|
235
74
|
export declare function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId;
|
|
236
|
-
|
|
75
|
+
/** */
|
|
76
|
+
export declare function testHoloId(): void;
|
package/dist/hash.js
CHANGED
|
@@ -74,14 +74,14 @@ export function hasHoloHashType(hash) {
|
|
|
74
74
|
return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));
|
|
75
75
|
}
|
|
76
76
|
/** */
|
|
77
|
-
export function validateHashB64(hash) {
|
|
77
|
+
export function validateHashB64(hash, hashType) {
|
|
78
78
|
if (!hash || typeof (hash) != 'string') {
|
|
79
79
|
throw new Error("The hash must be a valid string");
|
|
80
80
|
}
|
|
81
81
|
if (hash.length !== 53) {
|
|
82
|
-
throw new Error("The hash must be exactly 53 characters long.");
|
|
82
|
+
throw new Error("The hash must be exactly 53 characters long. Got: " + hash.length);
|
|
83
83
|
}
|
|
84
|
-
if (!hasHoloHashType(hash)) {
|
|
84
|
+
if ((hashType && !isHashTypeB64(hash, hashType)) || !hasHoloHashType(hash)) {
|
|
85
85
|
throw new Error("The hash must have a valid HoloHash type.");
|
|
86
86
|
}
|
|
87
87
|
}
|
|
@@ -96,56 +96,65 @@ export function enc64(hash) {
|
|
|
96
96
|
}
|
|
97
97
|
/** HoloHash starts with 'u' has a type and is 53 chars long */
|
|
98
98
|
export class HolochainId {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
get hashType() {
|
|
100
|
+
return this.constructor.HASH_TYPE;
|
|
101
|
+
}
|
|
102
|
+
/** ctor: validate */
|
|
103
|
+
constructor(input) {
|
|
103
104
|
if (typeof (input) != 'string') {
|
|
104
105
|
input = encodeHashToBase64(input);
|
|
105
106
|
}
|
|
106
|
-
validateHashB64(input);
|
|
107
|
+
validateHashB64(input, this.hashType);
|
|
107
108
|
this.b64 = input;
|
|
108
109
|
}
|
|
110
|
+
/** */
|
|
111
|
+
static empty() {
|
|
112
|
+
const empty = new Uint8Array(32);
|
|
113
|
+
const newHash = Uint8Array.from([
|
|
114
|
+
...HASH_TYPE_PREFIX[this.HASH_TYPE],
|
|
115
|
+
...empty,
|
|
116
|
+
...dhtLocationFrom32(empty),
|
|
117
|
+
]);
|
|
118
|
+
return new this(newHash);
|
|
119
|
+
}
|
|
120
|
+
/** */
|
|
121
|
+
static from(start) {
|
|
122
|
+
const core = Uint8Array.from(start.hash.slice(3, 35));
|
|
123
|
+
const hashType = this.HASH_TYPE;
|
|
124
|
+
const newHash = Uint8Array.from([
|
|
125
|
+
...HASH_TYPE_PREFIX[hashType],
|
|
126
|
+
...core,
|
|
127
|
+
...dhtLocationFrom32(core),
|
|
128
|
+
]);
|
|
129
|
+
return new this(newHash);
|
|
130
|
+
}
|
|
131
|
+
// Don't autoconvert to string as this can lead to confusions. Have convert to string be explicit
|
|
132
|
+
//toString(): string {return this.b64;}
|
|
109
133
|
get hash() { return dec64(this.b64); }
|
|
110
134
|
/** First 8 chars of the Core */
|
|
111
135
|
get short() { return this.b64.slice(5, 13); }
|
|
112
|
-
|
|
113
|
-
print() { return `${this.short} (${this.type})`; }
|
|
114
|
-
}
|
|
115
|
-
/** Mixin */
|
|
116
|
-
export function createHolochainId(hashType) {
|
|
117
|
-
class AHoloId extends HolochainId {
|
|
118
|
-
constructor(input) {
|
|
119
|
-
super(input, hashType);
|
|
120
|
-
const type = getHashType(this.b64);
|
|
121
|
-
if (hashType != type) {
|
|
122
|
-
throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got: ' + type);
|
|
123
|
-
}
|
|
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
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/** */
|
|
137
|
-
return AHoloId;
|
|
136
|
+
print() { return `${this.short} (${this.hashType})`; }
|
|
138
137
|
}
|
|
139
|
-
export class ActionId extends
|
|
138
|
+
export class ActionId extends HolochainId {
|
|
139
|
+
action() { }
|
|
140
140
|
}
|
|
141
|
-
|
|
141
|
+
ActionId.HASH_TYPE = HoloHashType.Action;
|
|
142
|
+
export class AgentId extends HolochainId {
|
|
143
|
+
agent() { }
|
|
142
144
|
}
|
|
143
|
-
|
|
145
|
+
AgentId.HASH_TYPE = HoloHashType.Agent;
|
|
146
|
+
export class EntryId extends HolochainId {
|
|
147
|
+
entry() { }
|
|
144
148
|
}
|
|
145
|
-
|
|
149
|
+
EntryId.HASH_TYPE = HoloHashType.Entry;
|
|
150
|
+
export class DnaId extends HolochainId {
|
|
151
|
+
dna() { }
|
|
146
152
|
}
|
|
147
|
-
|
|
153
|
+
DnaId.HASH_TYPE = HoloHashType.Dna;
|
|
154
|
+
export class ExternalId extends HolochainId {
|
|
155
|
+
external() { }
|
|
148
156
|
}
|
|
157
|
+
ExternalId.HASH_TYPE = HoloHashType.External;
|
|
149
158
|
/** */
|
|
150
159
|
export function intoDhtId(input) {
|
|
151
160
|
try {
|
|
@@ -168,4 +177,17 @@ export function intoLinkableId(input) {
|
|
|
168
177
|
return externalId;
|
|
169
178
|
}
|
|
170
179
|
}
|
|
180
|
+
/** */
|
|
181
|
+
export function testHoloId() {
|
|
182
|
+
console.log("testHoloId()");
|
|
183
|
+
const emptyAgent = AgentId.empty();
|
|
184
|
+
const emptyAction = ActionId.empty();
|
|
185
|
+
const emptyEntry = EntryId.from(emptyAgent);
|
|
186
|
+
console.log("testHoloId()", emptyAction);
|
|
187
|
+
/** */
|
|
188
|
+
function printEh(eh) {
|
|
189
|
+
console.log("printEh", eh);
|
|
190
|
+
}
|
|
191
|
+
printEh(emptyEntry);
|
|
192
|
+
}
|
|
171
193
|
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;IA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,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"]}
|
|
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;AAE7B;;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,CAAA;IACjB,+BAAe,CAAA;IACf,kBAAkB;IAClB,2BAAW,CAAA;IACX,+BAAe,CAAA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UAAU;IACxB,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAC5B,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IACnC,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;IACrC,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAE5C,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IACzC,MAAM;IACN,SAAS,OAAO,CAAC,EAAW;QAC1B,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,UAAU,CAAC,CAAC;AACtB,CAAC","sourcesContent":["import {\n decodeHashFromBase64,\n dhtLocationFrom32,\n encodeHashToBase64, HASH_TYPE_PREFIX,\n HoloHash,\n HoloHashB64,\n } from \"@holochain/client\";\n\n/**\n * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64\n */\nexport function anyToB64(obj: any): any {\n /** Check if it's a hash */\n if (obj instanceof Uint8Array) {\n return encodeHashToBase64(obj);\n } else {\n /** Check if it's an array of hashes */\n if (Array.isArray(obj)) {\n const isUint8Array =\n obj.length > 0 &&\n obj.every((value) => {\n return value instanceof Uint8Array;\n });\n if (isUint8Array) {\n let result = [];\n for (const cur of obj) {\n result.push(encodeHashToBase64(cur));\n }\n return result;\n }\n }\n }\n return obj;\n}\n\n\n/** ------------------------------------------------------------------------------------------------------------------*/\n\nexport enum HoloHashType {\n Action = \"Action\",\n Agent = \"Agent\",\n //DhtOp = \"DhtOp\",\n Dna = \"Dna\",\n Entry = \"Entry\",\n External = \"External\",\n Network = \"Network\",\n //Warrent = \"Warrent\",\n Wasm = \"Wasm\",\n}\n\nexport const HASH_TYPE_PREFIX_B64 = {\n Action: \"uhCkk\",\n Agent: \"uhCAk\",\n Dna: \"uhC0k\",\n //DhtOp: \"hCQk\",\n Entry: \"uhCEk\",\n External: \"uhC8k\",\n Network: \"uhCIk\",\n //Warrent: \"Warrent\",\n Wasm: \"uhCok\",\n};\n\nexport function getHashType(hash: HoloHashB64): HoloHashType {\n const hashExt = hash.slice(0, 5);\n const hashPrefixes = Object.values(HASH_TYPE_PREFIX_B64)\n for (let i = 0; i < hashPrefixes.length; i+= 1) {\n if (hashPrefixes[i] == hashExt) {\n return Object.keys(HASH_TYPE_PREFIX_B64)[i] as HoloHashType;\n }\n }\n throw Error(\"Unknown hash type\");\n}\n\n\n/** */\nexport function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType) {\n const slice = hash.slice(0, 5);\n const prefix = HASH_TYPE_PREFIX_B64[hashType];\n for (let i = 0; i < prefix.length; i++) {\n if (slice[i] !== prefix[i]) {\n return false;\n }\n }\n return true;\n}\n\n\nexport function hasHoloHashType(hash: HoloHashB64): boolean {\n return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));\n}\n\n\n/** */\nexport function validateHashB64(hash: HoloHashB64, hashType?: HoloHashType) {\n if (!hash || typeof(hash) != 'string') {\n throw new Error(\"The hash must be a valid string\");\n }\n if (hash.length !== 53) {\n throw new Error(\"The hash must be exactly 53 characters long. Got: \" + hash.length);\n }\n if((hashType && !isHashTypeB64(hash, hashType)) || !hasHoloHashType(hash)) {\n throw new Error(\"The hash must have a valid HoloHash type.\");\n }\n}\n\n\nexport function dec64(hash: HoloHashB64): HoloHash {\n validateHashB64(hash);\n return decodeHashFromBase64(hash);\n}\nexport function enc64(hash: HoloHash): HoloHashB64 {\n let b64 = encodeHashToBase64(hash);\n validateHashB64(b64);\n return b64;\n}\n\n\n\n/** HoloHash starts with 'u' has a type and is 53 chars long */\nexport abstract class HolochainId {\n public readonly b64: HoloHashB64;\n\n static readonly HASH_TYPE: HoloHashType;\n\n get hashType(): HoloHashType {\n return (this.constructor as typeof HolochainId).HASH_TYPE;\n }\n\n /** ctor: validate */\n constructor(input: HoloHashB64 | HoloHash) {\n if (typeof(input) != 'string') {\n input = encodeHashToBase64(input);\n }\n validateHashB64(input, this.hashType);\n this.b64 = input;\n }\n\n /** */\n static empty<T extends HolochainId>(this: any): T {\n const empty = new Uint8Array(32);\n const newHash = Uint8Array.from([\n ...HASH_TYPE_PREFIX[this.HASH_TYPE],\n ...empty,\n ...dhtLocationFrom32(empty),\n ]);\n return new this(newHash);\n }\n\n /** */\n static from<T extends HolochainId, Z extends HolochainId>(this: new (input: HoloHashB64 | HoloHash) => Z, start: T): Z {\n const core = Uint8Array.from(start.hash.slice(3, 35));\n const hashType = (this as unknown as typeof HolochainId).HASH_TYPE;\n const newHash = Uint8Array.from([\n ...HASH_TYPE_PREFIX[hashType],\n ...core,\n ...dhtLocationFrom32(core),\n ]);\n return new this(newHash);\n }\n\n // Don't autoconvert to string as this can lead to confusions. Have convert to string be explicit\n //toString(): string {return this.b64;}\n\n get hash(): HoloHash { return dec64(this.b64) }\n /** First 8 chars of the Core */\n get short(): string { return this.b64.slice(5, 13); }\n\n print(): string { return `${this.short} (${this.hashType})`}\n}\n\n\nexport class ActionId extends HolochainId { static readonly HASH_TYPE = HoloHashType.Action; action() {} }\nexport class AgentId extends HolochainId { static readonly HASH_TYPE = HoloHashType.Agent; agent() {} }\nexport class EntryId extends HolochainId { static readonly HASH_TYPE = HoloHashType.Entry; entry() {} }\nexport class DnaId extends HolochainId { static readonly HASH_TYPE = HoloHashType.Dna; dna() {} }\nexport class ExternalId extends HolochainId { static readonly HASH_TYPE = HoloHashType.External; external() {} }\n\n\nexport type AnyDhtId = ActionId | EntryId;\nexport type AnyLinkableId = AnyDhtId | ExternalId;\n\n\n/** */\nexport function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId {\n try {\n const actionId = new ActionId(input);\n return actionId;\n } catch(e) {\n const entryId = new EntryId(input);\n return entryId;\n }\n}\n\n\n/** */\nexport function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId {\n try {\n const dhtId = intoDhtId(input);\n return dhtId;\n } catch(e) {\n const externalId = new ExternalId(input);\n return externalId;\n }\n}\n\n\n/** */\nexport function testHoloId() {\n console.log(\"testHoloId()\");\n const emptyAgent = AgentId.empty();\n const emptyAction = ActionId.empty();\n const emptyEntry = EntryId.from(emptyAgent);\n\n console.log(\"testHoloId()\", emptyAction);\n /** */\n function printEh(eh: EntryId) {\n console.log(\"printEh\", eh);\n }\n\n printEh(emptyEntry);\n}\n\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ddd-qc/cell-proxy",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.4",
|
|
4
4
|
"description": "Proxy classes and helpers for managing a Holochain AppWebsocket",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"lint": "eslint --ext .ts .",
|
|
13
|
-
"build": "rm -rf dist && tsc"
|
|
13
|
+
"build": "rm -rf dist && tsc",
|
|
14
|
+
"build:watch": "tsc -w --preserveWatchOutput"
|
|
14
15
|
},
|
|
15
16
|
"repository": {
|
|
16
17
|
"type": "git",
|