forked from UCLA-IRL/ndn-workspace-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
129 lines (116 loc) · 4.42 KB
/
main.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { DenoKvStorage } from '@ucla-irl/ndnts-aux/storage';
import { Workspace } from '@ucla-irl/ndnts-aux/workspace';
import { AsyncDisposableStack, base64ToBytes } from '@ucla-irl/ndnts-aux/utils';
import { CertStorage } from '@ucla-irl/ndnts-aux/security';
import { Decoder } from '@ndn/tlv';
import { Data, digestSigning, Name } from '@ndn/packet';
import { Certificate } from '@ndn/keychain';
import { SafeBag } from '@ndn/ndnsec';
import { UnixTransport } from '@ndn/node-transport';
import * as nfdmgmt from '@ndn/nfdmgmt';
import { Forwarder, FwTracer } from '@ndn/fw';
import { sleep } from 'https://deno.land/x/[email protected]/mod.ts';
import * as Y from 'yjs';
const TRUST_ANCHOR = `
Bv0BOQcxCANuZG4IC3dlZWtseS1jYWxsCANLRVkICKLXjAGJ/kLcCARzZWxmNggA
AAGSfkVHqhQJGAECGQQANu6AFVswWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARw
QGjuS2M5L9CwTZ6xevhiMZ9fZnN64yoK3fOHfbR0FzMmbqz/wrh0gdRT14Ho7dz7
6vJz0Ux5u/B7KjEZWV3dFlIbAQMcIwchCANuZG4IC3dlZWtseS1jYWxsCANLRVkI
CKLXjAGJ/kLc/QD9Jv0A/g8yMDI0MTAxMlQwMTA4NDL9AP8PMjA0NDEwMDdUMDEw
ODQyF0gwRgIhAOzxzMcY58CYXmWVZbrDcoo1u0xoW7Mya3wlK09OxYymAiEA/xQq
u1UI7NZmxsCCHUhr8xn2gfMFi+bHHBTzjUy9ACU=
`;
const SAFEBAG = `
gP0CXAb9AV8HSAgDbmRuCAt3ZWVrbHktY2FsbAgJYnJhZC1sb3dlCANLRVkICChC
PlZ7sD/uCBAvbmRuL3dlZWtseS1jYWxsNggAAAGSfkeriRQJGAECGQQANu6AFVsw
WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASKmqCshGLfdrVwilvnPcwnmxekSNYJ
p1uZTliPKhU/4wCZ8sR+XEbysEpLdFnwCYp3/gVFsOJgZGFDphoOXP43FmIbAQMc
MwcxCANuZG4IC3dlZWtseS1jYWxsCANLRVkICKLXjAGJ/kLcCARzZWxmNggAAAGS
fkVHqv0A/Sb9AP4PMjAyNDEwMTJUMDExMTIw/QD/DzIwMjUxMDEyVDAxMTExORdH
MEUCIQD+KFNlhOqbCOd46sQHJh7agn+pSxYtBBRDYiTkB6VnKwIgYvaYRDS6lZnh
pvFPp1dTnhpjAMeYydeDHvv/qZG7E+OB9zCB9DBfBgkqhkiG9w0BBQ0wUjAxBgkq
hkiG9w0BBQwwJAQQvZzQaOVfcBg1Z++PGaBs3wICCAAwDAYIKoZIhvcNAgkFADAd
BglghkgBZQMEASoEEA4oHm/oh5767zQIsAYR8P4EgZD6iNpv9BPU07iW7ZDaksDD
9kpZJEH5lGEZrrAW2mRYmP+pss+dsc5hprT/Z8gTBCxOkSH3MjxYJ9P8JcqWa1N8
T3fakEgiW/vvvprpf5CqJ+jq8RxKQ48NtxqccCPdHGwdaUK0WWpijTPOOfjg1Rjt
4qZxZ0i3Qs6Lx5prtS0+VNa2SZRJhLKYV9lNuuKSt94=
`;
const decodeCert = (b64Value: string) => {
const wire = base64ToBytes(b64Value);
const data = Decoder.decode(wire, Data);
const cert = Certificate.fromData(data);
return cert;
};
const decodeSafebag = async (b64Value: string, passcode: string) => {
const wire = base64ToBytes(b64Value);
const safebag = Decoder.decode(wire, SafeBag);
const cert = safebag.certificate;
const prvKey = await safebag.decryptKey(passcode);
return { cert, prvKey };
};
const DEBUG = false;
const main = async () => {
if (DEBUG) FwTracer.enable();
await using closers = new AsyncDisposableStack();
const trustAnchor = decodeCert(TRUST_ANCHOR);
const { cert, prvKey } = await decodeSafebag(SAFEBAG, '123456');
const fw = Forwarder.getDefault();
const storage = await DenoKvStorage.create('./data/kv-store');
closers.use(storage);
const certStore = new CertStorage(trustAnchor, cert, storage, fw, prvKey);
const face = await UnixTransport.createFace({ l3: { local: true } }, '/run/nfd/nfd.sock');
closers.defer(() => face.close());
// Not working. Registered wrong profixes (.../test/sync/alo)
// enableNfdPrefixReg(face, {
// signer: digestSigning,
// });
// Register prefixes
const cr = await nfdmgmt.invoke('rib/register', {
name: new Name('/ndn-workspace/test'),
origin: 65, // client
cost: 0,
flags: 0x02, // CAPTURE
}, {
cOpts: { fw },
prefix: nfdmgmt.localhostPrefix,
signer: digestSigning,
});
if (cr.statusCode !== 200) {
console.error(`Unable to register route: ${cr.statusCode} ${cr.statusText}`);
Deno.exit();
}
const cr2 = await nfdmgmt.invoke('rib/register', {
name: new Name('/ndn-workspace/test/node-2'),
origin: 65, // client
cost: 0,
flags: 0x02, // CAPTURE
}, {
cOpts: { fw },
prefix: nfdmgmt.localhostPrefix,
signer: digestSigning,
});
if (cr2.statusCode !== 200) {
console.error(`Unable to register route: ${cr2.statusCode} ${cr2.statusText}`);
Deno.exit();
}
// TODO: Run without a signer
const workspace = await Workspace.create({
nodeId: new Name('/ndn-workspace/test/node-2'),
persistStore: storage,
fw,
rootDoc: new Y.Doc(),
signer: certStore.signer,
verifier: certStore.verifier,
});
closers.defer(() => workspace.destroy());
const exitSignal = new Promise<void>((resolve) => {
Deno.addSignalListener('SIGINT', () => {
console.log('Stopped by Ctrl+C');
resolve();
});
});
await exitSignal;
};
if (import.meta.main) {
await main();
}