|
1 | 1 | /*! |
2 | | - * Copyright (c) 2020-2022 Digital Bazaar, Inc. All rights reserved. |
| 2 | + * Copyright (c) 2020-2025 Digital Bazaar, Inc. All rights reserved. |
3 | 3 | */ |
4 | 4 | import * as bedrock from '@bedrock/core'; |
5 | | -import {createRequire} from 'node:module'; |
| 5 | +import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey'; |
6 | 6 | import {decodeSecretKeySeed} from 'bnid'; |
| 7 | +import {driver} from '@digitalbazaar/did-method-key'; |
7 | 8 | import {logger} from './logger.js'; |
8 | | -const require = createRequire(import.meta.url); |
9 | | -const {driver} = require('@digitalbazaar/did-method-key'); |
| 9 | +import { |
| 10 | + X25519KeyAgreementKey2020 |
| 11 | +} from '@digitalbazaar/x25519-key-agreement-key-2020'; |
10 | 12 |
|
11 | 13 | import './config.js'; |
12 | 14 |
|
13 | 15 | const {config, util: {BedrockError}} = bedrock; |
| 16 | + |
14 | 17 | const didKeyDriver = driver(); |
| 18 | +didKeyDriver.use({ |
| 19 | + multibaseMultikeyHeader: 'z6Mk', |
| 20 | + fromMultibase: Ed25519Multikey.from |
| 21 | +}); |
| 22 | +didKeyDriver.use({ |
| 23 | + multibaseMultikeyHeader: 'z6LS', |
| 24 | + fromMultibase: X25519KeyAgreementKey2020.from |
| 25 | +}); |
15 | 26 |
|
16 | 27 | let APPLICATION_IDENTITY; |
17 | 28 | const SERVICE_IDENTITIES = new Map(); |
@@ -122,27 +133,37 @@ async function _generateKeys({seedMultibase}) { |
122 | 133 | // convert multibase seed to Uint8Array |
123 | 134 | const seed = decodeSecretKeySeed({secretKeySeed: seedMultibase}); |
124 | 135 |
|
125 | | - // initialize key after bedrock configuration stage is complete |
126 | | - const didKey = await didKeyDriver.generate({seed}); |
127 | | - |
128 | | - const {didDocument: { |
129 | | - authentication, assertionMethod, capabilityDelegation, capabilityInvocation, |
130 | | - keyAgreement, id |
131 | | - }} = didKey; |
132 | | - |
133 | | - const authenticationKey = didKey.keyPairs.get(authentication[0]); |
134 | | - const assertionMethodKey = didKey.keyPairs.get(assertionMethod[0]); |
135 | | - const capabilityDelegationKey = didKey.keyPairs.get(capabilityDelegation[0]); |
136 | | - const capabilityInvocationKey = didKey.keyPairs.get(capabilityInvocation[0]); |
137 | | - const keyAgreementKey = didKey.keyPairs.get(keyAgreement[0].id); |
| 136 | + // initialize `did:key` DID after bedrock configuration stage is complete |
| 137 | + const verificationKeyPair = await Ed25519Multikey.generate({seed}); |
| 138 | + const { |
| 139 | + didDocument: {id}, methodFor |
| 140 | + } = await didKeyDriver.fromKeyPair({verificationKeyPair}); |
| 141 | + |
| 142 | + // update verification key pair with `id` and `controller` |
| 143 | + verificationKeyPair.id = methodFor({purpose: 'authentication'}).id; |
| 144 | + verificationKeyPair.controller = id; |
| 145 | + // for backwards compatibility, use legacy type `Ed25519VerificationKey2020`; |
| 146 | + // to be replaced with `Multikey` in next major release |
| 147 | + verificationKeyPair.type = 'Ed25519VerificationKey2020'; |
| 148 | + |
| 149 | + // for backwards compatibility, import key agreement key; to be removed |
| 150 | + // in next major release |
| 151 | + const keyAgreementKey = X25519KeyAgreementKey2020 |
| 152 | + .fromEd25519VerificationKey2020({ |
| 153 | + keyPair: { |
| 154 | + ...verificationKeyPair, |
| 155 | + privateKeyMultibase: verificationKeyPair.secretKeyMultibase |
| 156 | + } |
| 157 | + }); |
| 158 | + keyAgreementKey.id = `${id}#${keyAgreementKey.publicKeyMultibase}`; |
138 | 159 |
|
139 | 160 | return { |
140 | 161 | id, |
141 | 162 | keys: { |
142 | | - authenticationKey, |
143 | | - assertionMethodKey, |
144 | | - capabilityDelegationKey, |
145 | | - capabilityInvocationKey, |
| 163 | + authenticationKey: verificationKeyPair, |
| 164 | + assertionMethodKey: verificationKeyPair, |
| 165 | + capabilityDelegationKey: verificationKeyPair, |
| 166 | + capabilityInvocationKey: verificationKeyPair, |
146 | 167 | keyAgreementKey |
147 | 168 | } |
148 | 169 | }; |
|
0 commit comments