-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.js
84 lines (73 loc) · 2.68 KB
/
helpers.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
/*!
* Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved.
*/
import {decode} from 'base58-universal';
import {driver} from '@digitalbazaar/did-method-key';
import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020';
import {ZcapClient} from '@digitalbazaar/ezcap';
// found in bedrock-app-identity
const DEFAULT_APPLICATION_ID_SEED =
'z1AmMXgweztXscpTpxx19jsCLkPXUacTTBme2oxWGvuto9S';
// multibase base58-btc header
const MULTIBASE_BASE58BTC_HEADER = 'z';
// multihash identity function cdoe
const MULTIHASH_IDENTITY_FUNCTION_CODE = 0x00;
// seed byte size
const SEED_BYTE_SIZE = 32;
export async function createMeter({capabilityAgent} = {}) {
const invocationSigner = await getInvocationSigner();
const zcapClient = new ZcapClient({
invocationSigner,
SuiteClass: Ed25519Signature2020
});
// create a meter
const meterService = 'https://localhost:18443/meters';
let meter = {
controller: capabilityAgent.id,
product: {
// mock ID for webkms service product
id: 'urn:uuid:80a82316-e8c2-11eb-9570-10bf48838a41'
}
};
({data: {meter}} = await zcapClient.write({url: meterService, json: meter}));
// return full meter ID
const {id} = meter;
return {id: `${meterService}/${id}`};
}
export async function getInvocationSigner() {
// convert multibase seed to Uint8Array
const seed = _decodeMultibaseSeed({
seedMultibase: DEFAULT_APPLICATION_ID_SEED
});
const didKeyDriver = driver();
const didKey = await didKeyDriver.generate({seed});
const {didDocument: {capabilityInvocation}} = didKey;
const capabilityInvocationKey = didKey.keyPairs.get(capabilityInvocation[0]);
return capabilityInvocationKey.signer();
}
function _decodeMultibaseSeed({seedMultibase}) {
const prefix = seedMultibase[0];
if(prefix !== MULTIBASE_BASE58BTC_HEADER) {
throw new Error('Unsupported multibase encoding.');
}
const data = seedMultibase.substring(1);
// <varint hash fn code> <varint digest size in bytes> <hash fn output>
// <identity function> <32> <seed bytes>
const seedMultihash = decode(data);
// <varint hash fn code>: identity function
const [hashFnCode] = seedMultihash.slice(0, 1);
if(hashFnCode !== MULTIHASH_IDENTITY_FUNCTION_CODE) {
throw new Error('Invalid multihash function code.');
}
// <varint digest size in bytes>: 32
const [digestSize] = seedMultihash.slice(1, 2);
if(digestSize !== SEED_BYTE_SIZE) {
throw new Error('Invalid digest size.');
}
// <hash fn output>: seed bytes
const seedBytes = seedMultihash.slice(2, seedMultihash.length);
if(seedBytes.byteLength !== SEED_BYTE_SIZE) {
throw new Error('Invalid digest.');
}
return seedBytes;
}