-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-matchDIDKeyWRemoteT2-getJWT.ts
242 lines (211 loc) · 8.44 KB
/
remote-matchDIDKeyWRemoteT2-getJWT.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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'
import {hash} from '@stablelib/sha256'
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() {
let did = 'did:key:zDnaerx9CtbPJ1q36T5Ln5wYt3MQYeGRG5ehnPAmxcf5mDZpv';
const newDID = await matchDIDKeyWithRemote(did,stream);
/*
did = newDID;
console.log('public Key is:');
console.log(newDID); /// this is great, but I just need to stuff after the comma
console.log(did)
*/
// if await is non blocking, just let newDID be DID
/*
const newDID = 'did:key:zDnaezUFn4zmNoNeZvBEdVyCv6MVL69X8NRD8YavTCJWGuXM7'
*/
const signer = await remoteP256Signer(stream);
//const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'})
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: newDID, signer },{alg: 'ES256'})
console.log(jwt)
})();
},250);
})
server.listen(3000);
// if this does not work, try converting the ascii to a byte array in the getSignature function
function remoteP256Signer(stream): Signer {
return async (payload: string | Uint8Array): Promise<string> => {
// return async (payload: 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;
}
*/
/*
async function getSignature(stream,data: Uint8Array) {
// getSignature should take a sha256hash as a hex string....or convert a uint8array to a hexstring
// I think that the string needs to be sha256ed before it gets signed....?
// data may be a string, if so ascii to Uint8Array .... have a function that checks for Unit8Array or ascii string
const input = hash(data);
const inputHex = u8a.toString(input,'hex');
console.log(inputHex);
stream.write('2'+'1200'+inputHex);
let result = (await waitForEvent(stream,'data')).toString();
console.log(result);
console.log(resultToUint8Array(result));
let resultExit = bytesToBase64url(resultToUint8Array(result))
//console.log(resultExit);
return resultExit;
}
*/
/*
async function getSignature(stream,data: string | Uint8Array) {
// getSignature should take a sha256hash as a hex string....or convert a uint8array to a hexstring
// I think that the string needs to be sha256ed before it gets signed....?
if(data.constructor === Uint8Array) {
const signature = await signatureLogic(stream,data);
return signature;
} else if (data.constructor === String) {
const u8toSign = u8a.fromString(data,'ascii')
const signature = await signatureLogic(stream,u8toSign);
return signature;
}
}
async function signatureLogic(stream,data: Uint8Array) {
const input = hash(data);
const inputHex = u8a.toString(input,'hex');
console.log(inputHex);
stream.write('2'+'1200'+inputHex);
let result = (await waitForEvent(stream,'data')).toString();
console.log(result);
console.log(resultToUint8Array(result));
let resultExit = bytesToBase64url(resultToUint8Array(result))
return resultExit;
}
*/
async function getSignature(stream,data: string | Uint8Array) {
// getSignature should take a sha256hash as a hex string....or convert a uint8array to a hexstring
// I think that the string needs to be sha256ed before it gets signed....?
if(data.constructor === Uint8Array) {
console.log(data);
console.log('fed and uint8array');
const signature = await signatureLogic(stream,data);
console.log('the signature is: '+signature)
return signature;
} else if (data.constructor === String) {
console.log('here is the signature')
console.log(data);
const u8toSign = u8a.fromString(data,'ascii')
console.log(u8toSign);
console.log('fed a string');
const signature = await signatureLogic(stream,u8toSign);
console.log('the signature is: '+signature)
return signature;
}
}
async function signatureLogic(stream,data: Uint8Array) {
const input = hash(data);
console.log(input);
const inputHex = u8a.toString(input,'hex');
console.log(inputHex);
stream.write('2'+'1200'+inputHex);
let result = (await waitForEvent(stream,'data')).toString();
console.log('signatureresult');
console.log(result);
console.log(signatureResultToUint8Array(result));
let resultExit = bytesToBase64url(signatureResultToUint8Array(result))
return resultExit;
}
export function signatureResultToUint8Array(a: string): Uint8Array {
// splot a string, and get the second half
const myArray = a.split(",");
if(myArray[0] == 'signature') {
const hex = myArray[1];
const result = u8a.fromString(hex,'hex')
return result;
} else {
return undefined;
}
}
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]))
}