-
Notifications
You must be signed in to change notification settings - Fork 0
/
Address.js
60 lines (47 loc) · 1.36 KB
/
Address.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// @flow
import Sha from 'jssha/src/sha256';
import type { Term } from './types';
export type Address = string;
const store: { [key: string]: any } = {};
export function hashObj(obj: Object) {
const json = JSON.stringify(obj);
const sha = new Sha('SHA-256', 'TEXT');
sha.update(json);
const hash = sha.getHash('HEX');
return { hash, json };
}
export function get(addr: Address): any {
return store[addr];
}
export function set(val: Term<Address>): Address {
const hash = val.hash;
store[hash] = val;
return hash;
}
export type TermR = Term<TermR | Address>;
// Helper: stop recursively instantiating if we hit a string (Address)
function setRecursive_(val: TermR | Address): Address {
return typeof val === 'string'
? val
: set(val.map(setRecursive_));
}
// Expand and set a tree of terms. See Rec-test for examples.
export function setRecursive(val: TermR): [Term<Address>, Address] {
const val_ = val.mapSubterms(setRecursive_);
return [val_, set(val_)];
}
export function expand(address: Address): Term<Address> {
return get(address);
}
export function debugExpand(address: Address) {
const me: Term<Address> = expand(address);
const ret = {};
for (const [k, v] of me.entries()) {
if (typeof v === 'string' && v.length === 64 && get(v) != null) {
ret[k] = debugExpand(v);
} else {
ret[k] = v;
}
}
return ret;
}