-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-getJWT-matchremoteDID_multi.ts
167 lines (139 loc) · 5.45 KB
/
remote-getJWT-matchremoteDID_multi.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Signer } from 'did-jwt'
import { createJWS } from 'did-jwt'
import { createJWT } 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'
import * as u8a from 'uint8arrays'
const server = http.createServer();
//const websocketServer = new WebSocketStream({ server: server })
const websocketServer = WebSocket.createServer({server: server})
DNS.lookup(OS.hostname(), function (err, add, fam) {
console.log('addr: '+add);
})
websocketServer.on('stream',function(stream,request) {
setInterval(function(){
(async function() {
// you need to show the function from remote.ts that gets the proper did
let did = 'did:key:zDnaerx9CtbPJ1q36T5Ln5wYt3MQYeGRG5ehnPAmxcf5mDZpv';
// this code cannot be uncommented w/o breaking
const newDID = await matchDIDKeyWithRemote(did,stream);
did = newDID;
console.log(did);
did = 'did:key:zDnaeqYWNxcFqy5DcJm91BMTeWv5hjs1VL5medk9n8dDUC67T'
const newDID2 = await matchDIDKeyWithRemote(did,stream);
did = newDID2;
console.log(did);
/*
const signer = await remoteP256Signer(stream);
const toSign = 'hello';
// getSignature(stream,toSign)
const signature = await getSignature(stream,toSign);
console.log('ha');
console.log(signature);
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'})
console.log(jwt)
*/
// what are cryptographic signers supposed to return??
/*
console.log(signature.toString());
const sig = "signature,eb9b40e7635cd930f06ccca634fde3da3c230b315788aae0f2c42cf9f02ae7649f72d960812a82efc0d0102069258ad2859ad371b5f41e2b151a0983d34083e6"
const myArray = sig.split(",");
console.log(myArray[1]);
const hex = myArray[1];
console.log(hex)
*/
})();
},250);
})
server.listen(3000);
function remoteP256Signer(stream): Signer {
return async (payload: string | Uint8Array): Promise<string> => {
return await getSignature(stream,payload);
}
}
async function getSignature(stream,string) {
stream.write('2'+'1200'+string);
let result = (await waitForEvent(stream,'data')).toString();
//console.log(result);
let resultExit = bytesToBase64url(resultToUint8Array(result))
//console.log(resultExit);
return resultExit;
}
export function resultToUint8Array(a: string): Uint8Array {
// splot a string, and get the second half
const myArray = a.split(",");
const hex = myArray[1];
const result = u8a.fromString(hex,'hex')
return result;
}
export function bytesToBase64url(b: Uint8Array): string {
return u8a.toString(b, 'base64url')
}
// 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 matchDIDKeyWithRemote(didkeyURL: string,stream: any) : Promise<string> {
const compressedPublicKey = didKeyURLtoPubKeyHex(didkeyURL);
// console.log(compressedPublicKey);
//nist_weierstrauss.secp256r1.ECPointDecompress
const publicKey = nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToUint8ArrayPointPair(nist_weierstrauss.secp256r1.ECPointDecompress(fromString(compressedPublicKey,'hex')))
//const publicKey = publicKeyIntToUint8ArrayPointPair(ECPointDecompress(fromString(compressedPublicKey,'hex'))); // actually I need to create a function called compressed to raw
// console.log(publicKey);
const publicRawKey = octetToRaw(publicKey)
// console.log(publicRawKey);
let result = (await matchPublicKeyWithRemote(publicRawKey,stream)).toString();
/*
console.log('start of result')
console.log(result);
console.log('end of result')
*/
// return didkeyURL;
if(result.length > 1) {
return rpcToDID(result);
} else {
return didkeyURL;
}
}
async function matchPublicKeyWithRemote(publicKey: string,stream: any) : Promise<string> {
let rpcPayload = '0'+'1200'+publicKey;
stream.write(rpcPayload);
// console.log('rpcpaylod'+rpcPayload);
let result = await waitForEvent(stream,'data');
// console.log('result is:'+result);
return result;
}
function octetToRaw(publicKey: octetPoint) {
return toString(publicKey.xOctet,'hex')+toString(publicKey.yOctet,'hex')
}
function rpcToDID(response) : string {
let result = response.split(',');
//compressedKeyInHexfromRaw(result[1])
// return result[1];
const multicodecName = 'p256-pub';
return encodeDIDfromHexString(multicodecName,compressedKeyInHexfromRaw(result[1]))
}