-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-getPublicKey.ts
74 lines (58 loc) · 2.22 KB
/
remote-getPublicKey.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
import { Signer } from 'did-jwt'
import { createJWS } from 'did-jwt'
import stringify from 'fast-json-stable-stringify'
import type {
AuthParams,
CreateJWSParams,
DIDMethodName,
DIDProviderMethods,
DIDProvider,
GeneralJWS,
} from 'dids'
import { RPCError, createHandler } from 'rpc-utils'
import type { HandlerMethods, RPCRequest, RPCResponse, SendRequestFunc } from 'rpc-utils'
import { encodeDIDfromHexString, compressedKeyInHexfromRaw, didKeyURLtoPubKeyHex } from 'did-key-creator'
import { toString } from 'uint8arrays/to-string'
import * as http from 'http'
import * as WebSocket from 'websocket-stream'
import * as nist_weierstrauss from 'nist-weierstrauss'
import {octetPoint} from 'nist-weierstrauss'
import KeyResolver from 'key-did-resolver'
import { DID } from 'dids'
import {fromString} from 'uint8arrays'
import * as DNS from 'dns'
import * as OS from 'os'
const server = http.createServer();
const websocketServer = WebSocket.createServer({server: server})
server.listen(3000);
DNS.lookup(OS.hostname(), function (err, add, fam) {
console.log('addr: '+add);
})
websocketServer.on('stream',function(stream,request) {
stream.setEncoding('utf8');
setInterval(function(){
(async function() {
const publicKey = await getPublicKey(stream);
console.log('public Key is:');
console.log(publicKey); /// this is great, but I just need to stuff after the comma
})();
},250);
})
//server.listen(3000);
// I think that I have to close some listeners here....because I get to the maxListner limit
async function waitForEvent(emitter, event): Promise<string> {
return new Promise((resolve, reject) => {
emitter.once(event, resolve);
emitter.once("error", reject);
emitter.removeAllListeners("error"); /// I hope this is correct, it seems to stop the code from complaining about the maxListenerLimit being exceeded
});
}
async function getPublicKey(stream) : Promise<string> {
/// look at the RPC call to get the public key
let rpcPayload = '1'+'1200';
stream.write(rpcPayload);
let preResult = await waitForEvent(stream,'data');
console.log(preResult);
let result = preResult.split(',');
return result[1];
}