forked from WebOfTrustInfo/btcr-did-tools-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddoResolver.js
220 lines (190 loc) · 6.49 KB
/
ddoResolver.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
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
'use strict';
const txRefConversion = require("txref-conversion-js");
const util = require("./util");
var keyCounter = 1;
/**
* For each authentication credential
* - if missing an owner then add it
* - if missing id (special case if hex matches or not)
*
* @param authenticationCredential
* @param btcrDid
* @param publicKeyHex
* @returns fixed auth credential
*/
let fixPublicKeys = function (publicKeys, btcrDid, publicKeyHex) {
if (!publicKeys) {
return [];
}
let fixed = publicKeys.reduce(
(r, e, i, a) => {
if (!e.id) {
throw "Error parsing supplemental DID; public key didn't contain an id"
}
if (!e.owner) {
e.owner = btcrDid;
}
if (e.id.endsWith("#keys-1")) {
throw "Error parsing supplemental DID; this tries to override key-1"
} else if (e.publicKeyHex == publicKeyHex) {
// do nothing; we've already added it
} else if (e.id.startsWith("did:")) {
// add it as is
r.push(e);
} else if (!e.id.startsWith("#")) {
// this is defined by the transaction; add the DID id
throw "Error parsing supplemental DID; doesn't correspond to an expected format";
} else {
e.id = btcrDid + e.id;
r.push(e);
}
return r;
}, []);
return fixed;
};
let fixPermittedProofTypes = function (permittedProofTypes, btcrDid, publicKeyHex) {
let fixed = permittedProofTypes.reduce(
(r1, e1) => {
var fixed = fixAuthenticationCredentials(e1.authenticationCredential, btcrDid, publicKeyHex);
r1.push(fixed);
return r1;
}, []);
return fixed;
};
/**
* TODO:
* - if mutable store, check for signature
* - determine how to find "did document" section from link. I.e. which type?
* @param implicitDdo
* @param txDetails
* @param txref
* @returns {Promise.<*>}
*/
async function addSupplementalDidDocuments(implicitDdo, txDetails, txref) {
let ddo = implicitDdo;
if (implicitDdo.service && implicitDdo.service.length == 1 && implicitDdo.service[0].serviceEndpoint) {
let ddoUrl = implicitDdo.service[0].serviceEndpoint;
let ddo1 = await txRefConversion.promisifiedRequest({"url": ddoUrl});
let ddoJson = JSON.parse(ddo1).didDocument;
// append public keys
let fixedPublicKeys = fixPublicKeys(ddoJson.publicKey,
implicitDdo.id,
implicitDdo.publicKey[0].publicKeyHex);
fixedPublicKeys.forEach(e => ddo.publicKey.push(e));
// append all authentications
if (ddoJson && ddoJson.authentication) {
ddoJson.authentication.forEach(e => ddo.authentication.push(e));
}
// append all services
if (ddoJson && ddoJson.service) {
ddoJson.service.forEach(e => ddo.service.push(e));
}
return ddo;
}
}
async function retrieveEndpointFragments(ddoUrl) {
let ddo1 = await txRefConversion.promisifiedRequest({"url": ddoUrl});
return [ddo1];
}
/**r
* TODO
* - Update remaining satoshi proof elements
* @param txDetails
* @param txref
* @returns {{@context: string, id: string, publicKey: [null], authentication: [null], SatoshiAuditTrail: [null]}}
*/
async function toImplicitDidDocument(txDetails, txref) {
if (!txDetails) {
throw "Missing txDetails argument";
}
if (!txref) {
throw "Missing txref argument";
}
/*const ddoResult = {
"@context": ["https://schema.org/", "https://w3id.org/security/v1"]
};*/
let ddoResult = {};
let btcrDid = txref;
let fundingScript = txDetails.inputs[0].script;
let publicKeyHex = util.extractPublicKeyHexFromScript(fundingScript).toString();
let ddoUrl = txDetails.outputs.filter((o) => o.dataString).map(e => e.dataString).find(f => f);
if (ddoUrl) {
let explicitDdoRaw = await retrieveEndpointFragments(ddoUrl);
try {
let explicitDdo = JSON.parse(explicitDdoRaw);
ddoResult['explicitDdo'] = explicitDdo;
} catch (e) {
ddoResult['raw'] = explicitDdoRaw;
ddoResult['error'] = e.toString();
}
} else {
ddoResult['implicitDdo'] = {
"@context": "https://w3id.org/btcr/v1",
"id": btcrDid,
"publicKey": [{
"id": btcrDid + "#keys-1",
"owner": btcrDid,
"type": "EcdsaSecp256k1VerificationKey2019",
"publicKeyHex": publicKeyHex.toString()
}],
"authentication": [{
"type": "EcdsaSecp256k1VerificationKey2019",
"publicKey": "#keys-1"
}],
"SatoshiAuditTrail": [{
"chain": txDetails.chain,
"blockhash": txDetails.blockHash,
"blockindex": txDetails.blockIndex,
"outputindex": txDetails.utxoIndex,
"blocktime": txDetails.txReceived,
"time": 1499501000,
"timereceived": txDetails.txReceived,
"burn-fee": -0.05
}]
};
}
return ddoResult;
}
async function toDidDocument(txDetails, txref) {
let ddoResult = await toImplicitDidDocument(txDetails, txref);
let result = {
"txDetails": txDetails,
"ddoResult": ddoResult
};
return result;
}
async function resolveFromTxref(txref) {
if (!txref) {
throw "Missing txref argument";
}
let txDetails = await util.txDetailsFromTxref(txref);
let deterministicDid = await toDidDocument(txDetails, txref);
return deterministicDid;
}
async function resolveFromTxid(txid, chain, utxoIndex=0) {
if (!txid) {
throw "Missing txid argument";
}
if (!chain) {
throw "Missing chain argument";
}
let txDetails = await util.txDetailsFromTxid(txid, chain, utxoIndex);
let deterministicDid = await toDidDocument(txDetails, txDetails.txref);
return deterministicDid;
}
/*
resolveFromTxref("did:btcr:xyv2-xzpq-q9wa-p7t").then(dddo => {
console.log(JSON.stringify(dddo, null, 4));
}, error => {
console.error(error)
});*/
/*
resolveFromTxid("11d8023bd6ef3afc621a019d939345d31a5afa65c93dd4aab6af5feb6a55f4f2", "mainnet").then(dddo => {
console.log(JSON.stringify(dddo, null, 4));
}, error => {
console.error(error)
});*/
module.exports = {
resolveFromTxref: resolveFromTxref,
resolveFromTxid: resolveFromTxid
};