@bedrock/kms-http 14.5.0 → 16.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +0,0 @@
1
- {
2
- "env": {
3
- "mocha": true,
4
- "browser": true
5
- },
6
- "globals": {
7
- "should": true
8
- }
9
- }
@@ -1,108 +0,0 @@
1
- /*!
2
- * Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved.
3
- */
4
- import pMap from 'p-map';
5
- import uuid from 'uuid-random';
6
- import {CapabilityAgent, KeystoreAgent, KmsClient}
7
- from '@digitalbazaar/webkms-client';
8
- import * as helpers from './helpers.js';
9
-
10
- describe('bedrock-kms-http HMAC operations', () => {
11
- describe('Sha256HmacKey2019', () => {
12
- let hmac;
13
- before(async () => {
14
- const secret = 'b07e6b31-d910-438e-9a5f-08d945a5f676';
15
- const handle = 'testKey1';
16
- const capabilityAgent = await CapabilityAgent.fromSecret(
17
- {secret, handle});
18
-
19
- let err;
20
- let keystore;
21
- try {
22
- keystore = await _createKeystore({capabilityAgent});
23
- } catch(e) {
24
- err = e;
25
- }
26
- should.not.exist(err);
27
-
28
- // create kmsClient only required because we need to use httpsAgent
29
- // that accepts self-signed certs used in test suite
30
- const kmsClient = new KmsClient();
31
- const keystoreAgent = new KeystoreAgent({
32
- capabilityAgent,
33
- keystoreId: keystore.id,
34
- kmsClient
35
- });
36
- hmac = await keystoreAgent.generateKey({type: 'hmac'});
37
- });
38
- it('successfully signs', async () => {
39
- const data = new TextEncoder('utf-8').encode('hello');
40
- let err;
41
- let result;
42
- try {
43
- result = await hmac.sign({data});
44
- } catch(e) {
45
- err = e;
46
- }
47
- should.not.exist(err);
48
- should.exist(result);
49
- result.should.be.a('Uint8Array');
50
- });
51
-
52
- describe('bulk operations', () => {
53
- const operationCount = 1000;
54
- const vData = [];
55
- before(async () => {
56
- for(let i = 0; i < operationCount; ++i) {
57
- // uuids are 37 chars long, * 30 is ~1KiB
58
- let v = '';
59
- for(let n = 0; n < 30; ++n) {
60
- v += uuid();
61
- }
62
- vData.push(new TextEncoder('utf-8').encode(v));
63
- }
64
- });
65
- it(`performs ${operationCount} signatures`, async function() {
66
- this.timeout(0);
67
- const startTime = Date.now();
68
- let result;
69
- let err;
70
- try {
71
- result = await pMap(
72
- vData, data => hmac.sign({data}), {concurrency: 30});
73
- } catch(e) {
74
- err = e;
75
- }
76
- should.not.exist(err);
77
- should.exist(result);
78
- result.should.be.an('array');
79
- result.should.have.length(operationCount);
80
- const elapsedTime = Date.now() - startTime;
81
- // NOTE: reporter in karma does not report elapsed time for individual
82
- // tests, this logging is intentional
83
- console.log('ELAPSED TIME', elapsedTime);
84
- });
85
- }); // end bulk operations
86
- });
87
- });
88
-
89
- async function _createKeystore({capabilityAgent, referenceId}) {
90
- const {id: meterId} = await helpers.createMeter({capabilityAgent});
91
-
92
- // create keystore
93
- const config = {
94
- sequence: 0,
95
- controller: capabilityAgent.id,
96
- kmsModule: 'ssm-v1',
97
- meterId
98
- };
99
-
100
- if(referenceId) {
101
- config.referenceId = referenceId;
102
- }
103
- return KmsClient.createKeystore({
104
- url: 'https://localhost:18443/kms/keystores',
105
- config,
106
- invocationSigner: capabilityAgent.getSigner()
107
- });
108
- }
@@ -1,84 +0,0 @@
1
- /*!
2
- * Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved.
3
- */
4
- import {decode} from 'base58-universal';
5
- import {driver} from '@digitalbazaar/did-method-key';
6
- import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020';
7
- import {ZcapClient} from '@digitalbazaar/ezcap';
8
-
9
- // found in bedrock-app-identity
10
- const DEFAULT_APPLICATION_ID_SEED =
11
- 'z1AmMXgweztXscpTpxx19jsCLkPXUacTTBme2oxWGvuto9S';
12
-
13
- // multibase base58-btc header
14
- const MULTIBASE_BASE58BTC_HEADER = 'z';
15
- // multihash identity function cdoe
16
- const MULTIHASH_IDENTITY_FUNCTION_CODE = 0x00;
17
- // seed byte size
18
- const SEED_BYTE_SIZE = 32;
19
-
20
- export async function createMeter({capabilityAgent} = {}) {
21
- const invocationSigner = await getInvocationSigner();
22
- const zcapClient = new ZcapClient({
23
- invocationSigner,
24
- SuiteClass: Ed25519Signature2020
25
- });
26
-
27
- // create a meter
28
- const meterService = 'https://localhost:18443/meters';
29
- let meter = {
30
- controller: capabilityAgent.id,
31
- product: {
32
- // mock ID for webkms service product
33
- id: 'urn:uuid:80a82316-e8c2-11eb-9570-10bf48838a41'
34
- }
35
- };
36
- ({data: {meter}} = await zcapClient.write({url: meterService, json: meter}));
37
-
38
- // return full meter ID
39
- const {id} = meter;
40
- return {id: `${meterService}/${id}`};
41
- }
42
-
43
- export async function getInvocationSigner() {
44
- // convert multibase seed to Uint8Array
45
- const seed = _decodeMultibaseSeed({
46
- seedMultibase: DEFAULT_APPLICATION_ID_SEED
47
- });
48
-
49
- const didKeyDriver = driver();
50
- const didKey = await didKeyDriver.generate({seed});
51
-
52
- const {didDocument: {capabilityInvocation}} = didKey;
53
-
54
- const capabilityInvocationKey = didKey.keyPairs.get(capabilityInvocation[0]);
55
- return capabilityInvocationKey.signer();
56
- }
57
-
58
- function _decodeMultibaseSeed({seedMultibase}) {
59
- const prefix = seedMultibase[0];
60
- if(prefix !== MULTIBASE_BASE58BTC_HEADER) {
61
- throw new Error('Unsupported multibase encoding.');
62
- }
63
- const data = seedMultibase.substring(1);
64
- // <varint hash fn code> <varint digest size in bytes> <hash fn output>
65
- // <identity function> <32> <seed bytes>
66
- const seedMultihash = decode(data);
67
- // <varint hash fn code>: identity function
68
- const [hashFnCode] = seedMultihash.slice(0, 1);
69
- if(hashFnCode !== MULTIHASH_IDENTITY_FUNCTION_CODE) {
70
- throw new Error('Invalid multihash function code.');
71
- }
72
- // <varint digest size in bytes>: 32
73
- const [digestSize] = seedMultihash.slice(1, 2);
74
- if(digestSize !== SEED_BYTE_SIZE) {
75
- throw new Error('Invalid digest size.');
76
- }
77
- // <hash fn output>: seed bytes
78
- const seedBytes = seedMultihash.slice(2, seedMultihash.length);
79
- if(seedBytes.byteLength !== SEED_BYTE_SIZE) {
80
- throw new Error('Invalid digest.');
81
- }
82
-
83
- return seedBytes;
84
- }