@enbox/agent 0.0.1
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.
- package/dist/browser.js +2215 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2215 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/index.js +8530 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/agent-did-resolver-cache.js +87 -0
- package/dist/esm/agent-did-resolver-cache.js.map +1 -0
- package/dist/esm/bearer-identity.js +41 -0
- package/dist/esm/bearer-identity.js.map +1 -0
- package/dist/esm/connect.js +191 -0
- package/dist/esm/connect.js.map +1 -0
- package/dist/esm/crypto-api.js +346 -0
- package/dist/esm/crypto-api.js.map +1 -0
- package/dist/esm/did-api.js +278 -0
- package/dist/esm/did-api.js.map +1 -0
- package/dist/esm/dwn-api.js +336 -0
- package/dist/esm/dwn-api.js.map +1 -0
- package/dist/esm/dwn-registrar.js +120 -0
- package/dist/esm/dwn-registrar.js.map +1 -0
- package/dist/esm/hd-identity-vault.js +729 -0
- package/dist/esm/hd-identity-vault.js.map +1 -0
- package/dist/esm/identity-api.js +262 -0
- package/dist/esm/identity-api.js.map +1 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/local-key-manager.js +498 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/oidc.js +507 -0
- package/dist/esm/oidc.js.map +1 -0
- package/dist/esm/permissions-api.js +322 -0
- package/dist/esm/permissions-api.js.map +1 -0
- package/dist/esm/prototyping/clients/dwn-rpc-types.js +2 -0
- package/dist/esm/prototyping/clients/dwn-rpc-types.js.map +1 -0
- package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js +74 -0
- package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js.map +1 -0
- package/dist/esm/prototyping/clients/http-dwn-rpc-client.js +105 -0
- package/dist/esm/prototyping/clients/http-dwn-rpc-client.js.map +1 -0
- package/dist/esm/prototyping/clients/json-rpc-socket.js +150 -0
- package/dist/esm/prototyping/clients/json-rpc-socket.js.map +1 -0
- package/dist/esm/prototyping/clients/json-rpc.js +58 -0
- package/dist/esm/prototyping/clients/json-rpc.js.map +1 -0
- package/dist/esm/prototyping/clients/server-info-types.js +2 -0
- package/dist/esm/prototyping/clients/server-info-types.js.map +1 -0
- package/dist/esm/prototyping/clients/web-socket-clients.js +90 -0
- package/dist/esm/prototyping/clients/web-socket-clients.js.map +1 -0
- package/dist/esm/prototyping/common/object.js +14 -0
- package/dist/esm/prototyping/common/object.js.map +1 -0
- package/dist/esm/prototyping/common/type-utils.js +2 -0
- package/dist/esm/prototyping/common/type-utils.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js +147 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-kw.js +137 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-kw.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/ecdsa.js +307 -0
- package/dist/esm/prototyping/crypto/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/eddsa.js +264 -0
- package/dist/esm/prototyping/crypto/algorithms/eddsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/hkdf.js +39 -0
- package/dist/esm/prototyping/crypto/algorithms/hkdf.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js +41 -0
- package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js.map +1 -0
- package/dist/esm/prototyping/crypto/crypto-error.js +41 -0
- package/dist/esm/prototyping/crypto/crypto-error.js.map +1 -0
- package/dist/esm/prototyping/crypto/dsa.js +236 -0
- package/dist/esm/prototyping/crypto/dsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe-compact.js +130 -0
- package/dist/esm/prototyping/crypto/jose/jwe-compact.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe-flattened.js +294 -0
- package/dist/esm/prototyping/crypto/jose/jwe-flattened.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe.js +308 -0
- package/dist/esm/prototyping/crypto/jose/jwe.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/aes-gcm.js +352 -0
- package/dist/esm/prototyping/crypto/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/aes-kw.js +247 -0
- package/dist/esm/prototyping/crypto/primitives/aes-kw.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/hkdf.js +80 -0
- package/dist/esm/prototyping/crypto/primitives/hkdf.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/pbkdf2.js +85 -0
- package/dist/esm/prototyping/crypto/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/cipher.js +2 -0
- package/dist/esm/prototyping/crypto/types/cipher.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/crypto-api.js +2 -0
- package/dist/esm/prototyping/crypto/types/crypto-api.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-converter.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-converter.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-deriver.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-deriver.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-io.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-io.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-manager.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-manager.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-wrapper.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-wrapper.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/params-direct.js +2 -0
- package/dist/esm/prototyping/crypto/types/params-direct.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/params-kms.js +2 -0
- package/dist/esm/prototyping/crypto/types/params-kms.js.map +1 -0
- package/dist/esm/prototyping/crypto/utils.js +19 -0
- package/dist/esm/prototyping/crypto/utils.js.map +1 -0
- package/dist/esm/prototyping/dids/resolver-cache-memory.js +77 -0
- package/dist/esm/prototyping/dids/resolver-cache-memory.js.map +1 -0
- package/dist/esm/prototyping/dids/utils.js +9 -0
- package/dist/esm/prototyping/dids/utils.js.map +1 -0
- package/dist/esm/rpc-client.js +123 -0
- package/dist/esm/rpc-client.js.map +1 -0
- package/dist/esm/store-data-protocols.js +38 -0
- package/dist/esm/store-data-protocols.js.map +1 -0
- package/dist/esm/store-data.js +320 -0
- package/dist/esm/store-data.js.map +1 -0
- package/dist/esm/store-did.js +136 -0
- package/dist/esm/store-did.js.map +1 -0
- package/dist/esm/store-identity.js +140 -0
- package/dist/esm/store-identity.js.map +1 -0
- package/dist/esm/store-key.js +136 -0
- package/dist/esm/store-key.js.map +1 -0
- package/dist/esm/sync-api.js +61 -0
- package/dist/esm/sync-api.js.map +1 -0
- package/dist/esm/sync-engine-level.js +618 -0
- package/dist/esm/sync-engine-level.js.map +1 -0
- package/dist/esm/test-harness.js +239 -0
- package/dist/esm/test-harness.js.map +1 -0
- package/dist/esm/types/agent.js +2 -0
- package/dist/esm/types/agent.js.map +1 -0
- package/dist/esm/types/dwn.js +31 -0
- package/dist/esm/types/dwn.js.map +1 -0
- package/dist/esm/types/identity-vault.js +2 -0
- package/dist/esm/types/identity-vault.js.map +1 -0
- package/dist/esm/types/identity.js +2 -0
- package/dist/esm/types/identity.js.map +1 -0
- package/dist/esm/types/key-manager.js +2 -0
- package/dist/esm/types/key-manager.js.map +1 -0
- package/dist/esm/types/permissions.js +2 -0
- package/dist/esm/types/permissions.js.map +1 -0
- package/dist/esm/types/sync.js +2 -0
- package/dist/esm/types/sync.js.map +1 -0
- package/dist/esm/types/vc.js +5 -0
- package/dist/esm/types/vc.js.map +1 -0
- package/dist/esm/utils-internal.js +147 -0
- package/dist/esm/utils-internal.js.map +1 -0
- package/dist/esm/utils.js +161 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/agent-did-resolver-cache.d.ts +30 -0
- package/dist/types/agent-did-resolver-cache.d.ts.map +1 -0
- package/dist/types/bearer-identity.d.ts +31 -0
- package/dist/types/bearer-identity.d.ts.map +1 -0
- package/dist/types/connect.d.ts +88 -0
- package/dist/types/connect.d.ts.map +1 -0
- package/dist/types/crypto-api.d.ts +286 -0
- package/dist/types/crypto-api.d.ts.map +1 -0
- package/dist/types/did-api.d.ts +119 -0
- package/dist/types/did-api.d.ts.map +1 -0
- package/dist/types/dwn-api.d.ts +66 -0
- package/dist/types/dwn-api.d.ts.map +1 -0
- package/dist/types/dwn-registrar.d.ts +29 -0
- package/dist/types/dwn-registrar.d.ts.map +1 -0
- package/dist/types/hd-identity-vault.d.ts +306 -0
- package/dist/types/hd-identity-vault.d.ts.map +1 -0
- package/dist/types/identity-api.d.ts +107 -0
- package/dist/types/identity-api.d.ts.map +1 -0
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +311 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/oidc.d.ts +247 -0
- package/dist/types/oidc.d.ts.map +1 -0
- package/dist/types/permissions-api.d.ts +35 -0
- package/dist/types/permissions-api.d.ts.map +1 -0
- package/dist/types/prototyping/clients/dwn-rpc-types.d.ts +45 -0
- package/dist/types/prototyping/clients/dwn-rpc-types.d.ts.map +1 -0
- package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts +57 -0
- package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts.map +1 -0
- package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts +13 -0
- package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts.map +1 -0
- package/dist/types/prototyping/clients/json-rpc-socket.d.ts +43 -0
- package/dist/types/prototyping/clients/json-rpc-socket.d.ts.map +1 -0
- package/dist/types/prototyping/clients/json-rpc.d.ts +49 -0
- package/dist/types/prototyping/clients/json-rpc.d.ts.map +1 -0
- package/dist/types/prototyping/clients/server-info-types.d.ts +20 -0
- package/dist/types/prototyping/clients/server-info-types.d.ts.map +1 -0
- package/dist/types/prototyping/clients/web-socket-clients.d.ts +10 -0
- package/dist/types/prototyping/clients/web-socket-clients.d.ts.map +1 -0
- package/dist/types/prototyping/common/object.d.ts +2 -0
- package/dist/types/prototyping/common/object.d.ts.map +1 -0
- package/dist/types/prototyping/common/type-utils.d.ts +7 -0
- package/dist/types/prototyping/common/type-utils.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts +151 -0
- package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts +109 -0
- package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts +160 -0
- package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts +157 -0
- package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts +21 -0
- package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts +21 -0
- package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/crypto-error.d.ts +29 -0
- package/dist/types/prototyping/crypto/crypto-error.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/dsa.d.ts +169 -0
- package/dist/types/prototyping/crypto/dsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts +135 -0
- package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts +134 -0
- package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe.d.ts +378 -0
- package/dist/types/prototyping/crypto/jose/jwe.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts +103 -0
- package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/hkdf.d.ts +90 -0
- package/dist/types/prototyping/crypto/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts +84 -0
- package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/cipher.d.ts +14 -0
- package/dist/types/prototyping/crypto/types/cipher.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/crypto-api.d.ts +35 -0
- package/dist/types/prototyping/crypto/types/crypto-api.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-converter.d.ts +49 -0
- package/dist/types/prototyping/crypto/types/key-converter.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-deriver.d.ts +50 -0
- package/dist/types/prototyping/crypto/types/key-deriver.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-io.d.ts +49 -0
- package/dist/types/prototyping/crypto/types/key-io.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-manager.d.ts +69 -0
- package/dist/types/prototyping/crypto/types/key-manager.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-wrapper.d.ts +14 -0
- package/dist/types/prototyping/crypto/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/params-direct.d.ts +75 -0
- package/dist/types/prototyping/crypto/types/params-direct.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/params-kms.d.ts +63 -0
- package/dist/types/prototyping/crypto/types/params-kms.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/utils.d.ts +7 -0
- package/dist/types/prototyping/crypto/utils.d.ts.map +1 -0
- package/dist/types/prototyping/dids/resolver-cache-memory.d.ts +57 -0
- package/dist/types/prototyping/dids/resolver-cache-memory.d.ts.map +1 -0
- package/dist/types/prototyping/dids/utils.d.ts +3 -0
- package/dist/types/prototyping/dids/utils.d.ts.map +1 -0
- package/dist/types/rpc-client.d.ts +51 -0
- package/dist/types/rpc-client.d.ts.map +1 -0
- package/dist/types/store-data-protocols.d.ts +4 -0
- package/dist/types/store-data-protocols.d.ts.map +1 -0
- package/dist/types/store-data.d.ts +95 -0
- package/dist/types/store-data.d.ts.map +1 -0
- package/dist/types/store-did.d.ts +33 -0
- package/dist/types/store-did.d.ts.map +1 -0
- package/dist/types/store-identity.d.ts +34 -0
- package/dist/types/store-identity.d.ts.map +1 -0
- package/dist/types/store-key.d.ts +32 -0
- package/dist/types/store-key.d.ts.map +1 -0
- package/dist/types/sync-api.d.ts +41 -0
- package/dist/types/sync-api.d.ts.map +1 -0
- package/dist/types/sync-engine-level.d.ts +85 -0
- package/dist/types/sync-engine-level.d.ts.map +1 -0
- package/dist/types/test-harness.d.ts +69 -0
- package/dist/types/test-harness.d.ts.map +1 -0
- package/dist/types/types/agent.d.ts +172 -0
- package/dist/types/types/agent.d.ts.map +1 -0
- package/dist/types/types/dwn.d.ts +178 -0
- package/dist/types/types/dwn.d.ts.map +1 -0
- package/dist/types/types/identity-vault.d.ts +129 -0
- package/dist/types/types/identity-vault.d.ts.map +1 -0
- package/dist/types/types/identity.d.ts +16 -0
- package/dist/types/types/identity.d.ts.map +1 -0
- package/dist/types/types/key-manager.d.ts +9 -0
- package/dist/types/types/key-manager.d.ts.map +1 -0
- package/dist/types/types/permissions.d.ts +98 -0
- package/dist/types/types/permissions.d.ts.map +1 -0
- package/dist/types/types/sync.d.ts +66 -0
- package/dist/types/types/sync.d.ts.map +1 -0
- package/dist/types/types/vc.d.ts +7 -0
- package/dist/types/types/vc.d.ts.map +1 -0
- package/dist/types/utils-internal.d.ts +50 -0
- package/dist/types/utils-internal.d.ts.map +1 -0
- package/dist/types/utils.d.ts +37 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +112 -0
- package/src/agent-did-resolver-cache.ts +95 -0
- package/src/bearer-identity.ts +42 -0
- package/src/connect.ts +296 -0
- package/src/crypto-api.ts +593 -0
- package/src/did-api.ts +429 -0
- package/src/dwn-api.ts +462 -0
- package/src/dwn-registrar.ts +127 -0
- package/src/hd-identity-vault.ts +853 -0
- package/src/identity-api.ts +324 -0
- package/src/index.ts +30 -0
- package/src/local-key-manager.ts +672 -0
- package/src/oidc.ts +857 -0
- package/src/permissions-api.ts +408 -0
- package/src/prototyping/clients/dwn-rpc-types.ts +55 -0
- package/src/prototyping/clients/dwn-server-info-cache-memory.ts +79 -0
- package/src/prototyping/clients/http-dwn-rpc-client.ts +110 -0
- package/src/prototyping/clients/json-rpc-socket.ts +169 -0
- package/src/prototyping/clients/json-rpc.ts +113 -0
- package/src/prototyping/clients/server-info-types.ts +21 -0
- package/src/prototyping/clients/web-socket-clients.ts +100 -0
- package/src/prototyping/common/object.ts +15 -0
- package/src/prototyping/common/type-utils.ts +6 -0
- package/src/prototyping/crypto/algorithms/aes-gcm.ts +211 -0
- package/src/prototyping/crypto/algorithms/aes-kw.ts +164 -0
- package/src/prototyping/crypto/algorithms/ecdsa.ts +365 -0
- package/src/prototyping/crypto/algorithms/eddsa.ts +310 -0
- package/src/prototyping/crypto/algorithms/hkdf.ts +40 -0
- package/src/prototyping/crypto/algorithms/pbkdf2.ts +44 -0
- package/src/prototyping/crypto/crypto-error.ts +45 -0
- package/src/prototyping/crypto/dsa.ts +367 -0
- package/src/prototyping/crypto/jose/jwe-compact.ts +225 -0
- package/src/prototyping/crypto/jose/jwe-flattened.ts +459 -0
- package/src/prototyping/crypto/jose/jwe.ts +653 -0
- package/src/prototyping/crypto/primitives/aes-gcm.ts +374 -0
- package/src/prototyping/crypto/primitives/aes-kw.ts +271 -0
- package/src/prototyping/crypto/primitives/hkdf.ts +121 -0
- package/src/prototyping/crypto/primitives/pbkdf2.ts +116 -0
- package/src/prototyping/crypto/types/cipher.ts +17 -0
- package/src/prototyping/crypto/types/crypto-api.ts +78 -0
- package/src/prototyping/crypto/types/key-converter.ts +53 -0
- package/src/prototyping/crypto/types/key-deriver.ts +56 -0
- package/src/prototyping/crypto/types/key-io.ts +51 -0
- package/src/prototyping/crypto/types/key-manager.ts +83 -0
- package/src/prototyping/crypto/types/key-wrapper.ts +17 -0
- package/src/prototyping/crypto/types/params-direct.ts +95 -0
- package/src/prototyping/crypto/types/params-kms.ts +76 -0
- package/src/prototyping/crypto/utils.ts +41 -0
- package/src/prototyping/dids/resolver-cache-memory.ts +83 -0
- package/src/prototyping/dids/utils.ts +10 -0
- package/src/rpc-client.ts +162 -0
- package/src/store-data-protocols.ts +40 -0
- package/src/store-data.ts +400 -0
- package/src/store-did.ts +105 -0
- package/src/store-identity.ts +109 -0
- package/src/store-key.ts +104 -0
- package/src/sync-api.ts +71 -0
- package/src/sync-engine-level.ts +714 -0
- package/src/test-harness.ts +330 -0
- package/src/types/agent.ts +195 -0
- package/src/types/dwn.ts +278 -0
- package/src/types/identity-vault.ts +137 -0
- package/src/types/identity.ts +18 -0
- package/src/types/key-manager.ts +15 -0
- package/src/types/permissions.ts +115 -0
- package/src/types/sync.ts +58 -0
- package/src/types/vc.ts +7 -0
- package/src/utils-internal.ts +157 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Jwk,
|
|
3
|
+
Cipher,
|
|
4
|
+
Hasher,
|
|
5
|
+
Signer,
|
|
6
|
+
KeyWrapper,
|
|
7
|
+
SignParams,
|
|
8
|
+
AesGcmParams,
|
|
9
|
+
DigestParams,
|
|
10
|
+
VerifyParams,
|
|
11
|
+
GenerateKeyParams,
|
|
12
|
+
GetPublicKeyParams,
|
|
13
|
+
KmsGetKeyUriParams,
|
|
14
|
+
AsymmetricKeyGenerator,
|
|
15
|
+
} from '@enbox/crypto';
|
|
16
|
+
|
|
17
|
+
import { CryptoAlgorithm, Sha2Algorithm, computeJwkThumbprint } from '@enbox/crypto';
|
|
18
|
+
|
|
19
|
+
import type { CryptoApi } from './prototyping/crypto/types/crypto-api.js';
|
|
20
|
+
import type { HkdfParams } from './prototyping/crypto/primitives/hkdf.js';
|
|
21
|
+
import type { Pbkdf2Params } from './prototyping/crypto/primitives/pbkdf2.js';
|
|
22
|
+
import type { KeyBytesDeriver } from './prototyping/crypto/types/key-deriver.js';
|
|
23
|
+
import type { AsymmetricKeyConverter, KeyConverter } from './prototyping/crypto/types/key-converter.js';
|
|
24
|
+
import type { BytesToPrivateKeyParams, BytesToPublicKeyParams, CipherParams, DeriveKeyBytesParams, DeriveKeyParams, PrivateKeyToBytesParams, PublicKeyToBytesParams, UnwrapKeyParams, WrapKeyParams } from './prototyping/crypto/types/params-direct.js';
|
|
25
|
+
|
|
26
|
+
import { HkdfAlgorithm } from './prototyping/crypto/algorithms/hkdf.js';
|
|
27
|
+
import { EcdsaAlgorithm } from './prototyping/crypto/algorithms/ecdsa.js';
|
|
28
|
+
import { EdDsaAlgorithm } from './prototyping/crypto/algorithms/eddsa.js';
|
|
29
|
+
import { AesKwAlgorithm } from './prototyping/crypto/algorithms/aes-kw.js';
|
|
30
|
+
import { Pbkdf2Algorithm } from './prototyping/crypto/algorithms/pbkdf2.js';
|
|
31
|
+
import { AesGcmAlgorithm } from './prototyping/crypto/algorithms/aes-gcm.js';
|
|
32
|
+
import { CryptoError, CryptoErrorCode } from './prototyping/crypto/crypto-error.js';
|
|
33
|
+
|
|
34
|
+
export interface CryptoApiBytesToPrivateKeyParams extends BytesToPrivateKeyParams {
|
|
35
|
+
algorithm: KeyConversionAlgorithm;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CryptoApiBytesToPublicKeyParams extends BytesToPublicKeyParams {
|
|
39
|
+
algorithm: AsymmetricKeyConversionAlgorithm;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The `CryptoApiCipherParams` interface defines the algorithm-specific parameters that should
|
|
44
|
+
* be passed into the {@link AgentCryptoApi.encrypt | `AgentCryptoApi.encrypt()`} or
|
|
45
|
+
* {@link AgentCryptoApi.decrypt | `AgentCryptoApi.decrypt()`} method.
|
|
46
|
+
*/
|
|
47
|
+
export interface CryptoApiCipherParams extends CipherParams, AesGcmParams {}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The `CryptoApiDigestParams` interface defines the algorithm-specific parameters that should
|
|
51
|
+
* be passed into the {@link AgentCryptoApi.digest | `AgentCryptoApi.digest()`} method.
|
|
52
|
+
*/
|
|
53
|
+
export interface CryptoApiDigestParams extends DigestParams {
|
|
54
|
+
/**
|
|
55
|
+
* A string defining the name of hash function to use. The value must be one of the following:
|
|
56
|
+
* - `"SHA-256"`: Generates a 256-bit digest.
|
|
57
|
+
*/
|
|
58
|
+
algorithm: DigestAlgorithm;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface CryptoApiDeriveKeyOptions {
|
|
62
|
+
'HKDF-256': Omit<HkdfParams, 'hash'> & { derivedKeyAlgorithm: CipherAlgorithm | KeyWrappingAlgorithm};
|
|
63
|
+
'HKDF-384': Omit<HkdfParams, 'hash'> & { derivedKeyAlgorithm: CipherAlgorithm | KeyWrappingAlgorithm};
|
|
64
|
+
'HKDF-512': Omit<HkdfParams, 'hash'> & { derivedKeyAlgorithm: CipherAlgorithm | KeyWrappingAlgorithm};
|
|
65
|
+
'PBES2-HS256+A128KW': Omit<Pbkdf2Params, 'hash'> & { derivedKeyAlgorithm?: never };
|
|
66
|
+
'PBES2-HS384+A192KW': Omit<Pbkdf2Params, 'hash'> & { derivedKeyAlgorithm?: never };
|
|
67
|
+
'PBES2-HS512+A256KW': Omit<Pbkdf2Params, 'hash'> & { derivedKeyAlgorithm?: never };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface CryptoApiDeriveKeyBytesOptions {
|
|
71
|
+
'HKDF-256': Omit<HkdfParams, 'hash'>;
|
|
72
|
+
'HKDF-384': Omit<HkdfParams, 'hash'>;
|
|
73
|
+
'HKDF-512': Omit<HkdfParams, 'hash'>;
|
|
74
|
+
'PBES2-HS256+A128KW': Omit<Pbkdf2Params, 'hash'>;
|
|
75
|
+
'PBES2-HS384+A192KW': Omit<Pbkdf2Params, 'hash'>;
|
|
76
|
+
'PBES2-HS512+A256KW': Omit<Pbkdf2Params, 'hash'>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The `CryptoApiDeriveKeyParams` interface defines the algorithm-specific parameters that
|
|
81
|
+
* should be passed into the {@link AgentCryptoApi.deriveKey | `AgentCryptoApi.deriveKey()`} method.
|
|
82
|
+
*/
|
|
83
|
+
export type CryptoApiDeriveKeyParams<T extends DeriveKeyAlgorithm> = DeriveKeyParams & {
|
|
84
|
+
/**
|
|
85
|
+
* A string defining the name of key derivation function to use. The value must be one of the
|
|
86
|
+
* following:
|
|
87
|
+
* - `"HKDF-256"`: HKDF with SHA-256.
|
|
88
|
+
* - `"HKDF-384"`: HKDF with SHA-384.
|
|
89
|
+
* - `"HKDF-512"`: HKDF with SHA-512.
|
|
90
|
+
* - `"PBKDF2-HS256+A128KW"`: PBKDF2 with HMAC SHA-256 and A128KW key wrapping.
|
|
91
|
+
* - `"PBKDF2-HS384+A192KW"`: PBKDF2 with HMAC SHA-384 and A192KW key wrapping.
|
|
92
|
+
* - `"PBKDF2-HS512+A256KW"`: PBKDF2 with HMAC SHA-512 and A256KW key wrapping.
|
|
93
|
+
*/
|
|
94
|
+
algorithm: T;
|
|
95
|
+
} & CryptoApiDeriveKeyOptions[T];
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The `CryptoApiDeriveKeyBytesParams` interface defines the algorithm-specific parameters that
|
|
99
|
+
* should be passed into the {@link AgentCryptoApi.deriveKeyBytes | `AgentCryptoApi.deriveKeyBytes()`} method.
|
|
100
|
+
*/
|
|
101
|
+
export type CryptoApiDeriveKeyBytesParams<T extends DeriveKeyByteAlgorithm> = DeriveKeyBytesParams & {
|
|
102
|
+
/**
|
|
103
|
+
* A string defining the name of key derivation function to use. The value must be one of the
|
|
104
|
+
* following:
|
|
105
|
+
* - `"HKDF-256"`: HKDF with SHA-256.
|
|
106
|
+
* - `"HKDF-384"`: HKDF with SHA-384.
|
|
107
|
+
* - `"HKDF-512"`: HKDF with SHA-512.
|
|
108
|
+
* - `"PBKDF2-HS256+A128KW"`: PBKDF2 with HMAC SHA-256 and A128KW key wrapping.
|
|
109
|
+
* - `"PBKDF2-HS384+A192KW"`: PBKDF2 with HMAC SHA-384 and A192KW key wrapping.
|
|
110
|
+
* - `"PBKDF2-HS512+A256KW"`: PBKDF2 with HMAC SHA-512 and A256KW key wrapping.
|
|
111
|
+
*/
|
|
112
|
+
algorithm: T;
|
|
113
|
+
} & CryptoApiDeriveKeyBytesOptions[T];
|
|
114
|
+
|
|
115
|
+
export interface CryptoApiGenerateKeyParams extends GenerateKeyParams {
|
|
116
|
+
algorithm: KeyGenerationAlgorithm;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* `supportedAlgorithms` is an object mapping algorithm names to their respective implementations
|
|
121
|
+
* Each entry in this map specifies the algorithm name and its associated properties, including the
|
|
122
|
+
* implementation class and any relevant names or identifiers for the algorithm. This structure
|
|
123
|
+
* allows for easy retrieval and instantiation of algorithm implementations based on the algorithm
|
|
124
|
+
* name or key specification. It facilitates the support of multiple algorithms within the
|
|
125
|
+
* `LocalKeyManager` class.
|
|
126
|
+
*/
|
|
127
|
+
const supportedAlgorithms = {
|
|
128
|
+
'AES-GCM': {
|
|
129
|
+
implementation : AesGcmAlgorithm,
|
|
130
|
+
names : ['A128GCM', 'A192GCM', 'A256GCM'],
|
|
131
|
+
operations : ['bytesToPrivateKey', 'decrypt', 'encrypt', 'generateKey'],
|
|
132
|
+
},
|
|
133
|
+
'AES-KW': {
|
|
134
|
+
implementation : AesKwAlgorithm,
|
|
135
|
+
names : ['A128KW', 'A192KW', 'A256KW'],
|
|
136
|
+
operations : ['bytesToPrivateKey', 'generateKey', 'privateKeyToBytes', 'wrapKey', 'unwrapKey'],
|
|
137
|
+
},
|
|
138
|
+
'Ed25519': {
|
|
139
|
+
implementation : EdDsaAlgorithm,
|
|
140
|
+
names : ['Ed25519'],
|
|
141
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
142
|
+
},
|
|
143
|
+
'HKDF': {
|
|
144
|
+
implementation : HkdfAlgorithm,
|
|
145
|
+
names : ['HKDF-256', 'HKDF-384', 'HKDF-512'],
|
|
146
|
+
operations : ['deriveKey', 'deriveKeyBytes'],
|
|
147
|
+
},
|
|
148
|
+
'PBKDF2': {
|
|
149
|
+
implementation : Pbkdf2Algorithm,
|
|
150
|
+
names : ['PBES2-HS256+A128KW', 'PBES2-HS384+A192KW', 'PBES2-HS512+A256KW'],
|
|
151
|
+
operations : ['deriveKey', 'deriveKeyBytes'],
|
|
152
|
+
},
|
|
153
|
+
'secp256k1': {
|
|
154
|
+
implementation : EcdsaAlgorithm,
|
|
155
|
+
names : ['ES256K', 'secp256k1'],
|
|
156
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
157
|
+
},
|
|
158
|
+
'secp256r1': {
|
|
159
|
+
implementation : EcdsaAlgorithm,
|
|
160
|
+
names : ['ES256', 'secp256r1'],
|
|
161
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
162
|
+
},
|
|
163
|
+
'SHA-256': {
|
|
164
|
+
implementation : Sha2Algorithm,
|
|
165
|
+
names : ['SHA-256'],
|
|
166
|
+
operations : ['digest'],
|
|
167
|
+
}
|
|
168
|
+
} as const;
|
|
169
|
+
|
|
170
|
+
/* Helper types for `supportedAlgorithms`. */
|
|
171
|
+
type SupportedAlgorithm = keyof typeof supportedAlgorithms;
|
|
172
|
+
type SupportedAlgorithms = typeof supportedAlgorithms;
|
|
173
|
+
|
|
174
|
+
/* Helper type for `supportedAlgorithms` implementations. */
|
|
175
|
+
type AlgorithmConstructor = typeof supportedAlgorithms[SupportedAlgorithm]['implementation'];
|
|
176
|
+
|
|
177
|
+
type CipherAlgorithms = {
|
|
178
|
+
[K in keyof SupportedAlgorithms]: 'encrypt' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
179
|
+
}[keyof SupportedAlgorithms];
|
|
180
|
+
|
|
181
|
+
type CipherAlgorithm = typeof supportedAlgorithms[CipherAlgorithms]['names'][number];
|
|
182
|
+
|
|
183
|
+
type DeriveKeyAlgorithms = {
|
|
184
|
+
[K in keyof SupportedAlgorithms]: 'deriveKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
185
|
+
}[keyof SupportedAlgorithms];
|
|
186
|
+
|
|
187
|
+
type DeriveKeyAlgorithm = typeof supportedAlgorithms[DeriveKeyAlgorithms]['names'][number];
|
|
188
|
+
|
|
189
|
+
type DeriveKeyBytesAlgorithms = {
|
|
190
|
+
[K in keyof SupportedAlgorithms]: 'deriveKeyBytes' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
191
|
+
}[keyof SupportedAlgorithms];
|
|
192
|
+
|
|
193
|
+
type DeriveKeyByteAlgorithm = typeof supportedAlgorithms[DeriveKeyBytesAlgorithms]['names'][number];
|
|
194
|
+
|
|
195
|
+
type DigestAlgorithms = {
|
|
196
|
+
[K in keyof SupportedAlgorithms]: 'digest' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
197
|
+
}[keyof SupportedAlgorithms];
|
|
198
|
+
|
|
199
|
+
type DigestAlgorithm = typeof supportedAlgorithms[DigestAlgorithms]['names'][number];
|
|
200
|
+
|
|
201
|
+
type KeyConversionAlgorithms = {
|
|
202
|
+
[K in keyof SupportedAlgorithms]: 'bytesToPrivateKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
203
|
+
}[keyof SupportedAlgorithms];
|
|
204
|
+
|
|
205
|
+
type KeyConversionAlgorithm = typeof supportedAlgorithms[KeyConversionAlgorithms]['names'][number];
|
|
206
|
+
|
|
207
|
+
type AsymmetricKeyConversionAlgorithms = {
|
|
208
|
+
[K in keyof SupportedAlgorithms]: 'bytesToPublicKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
209
|
+
}[keyof SupportedAlgorithms];
|
|
210
|
+
|
|
211
|
+
type AsymmetricKeyConversionAlgorithm = typeof supportedAlgorithms[AsymmetricKeyConversionAlgorithms]['names'][number];
|
|
212
|
+
|
|
213
|
+
type KeyWrappingAlgorithms = {
|
|
214
|
+
[K in keyof SupportedAlgorithms]: 'wrapKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
215
|
+
}[keyof SupportedAlgorithms];
|
|
216
|
+
|
|
217
|
+
type KeyWrappingAlgorithm = typeof supportedAlgorithms[KeyWrappingAlgorithms]['names'][number];
|
|
218
|
+
|
|
219
|
+
type KeyGenerationAlgorithms = {
|
|
220
|
+
[K in keyof SupportedAlgorithms]: 'generateKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
221
|
+
}[keyof SupportedAlgorithms];
|
|
222
|
+
|
|
223
|
+
type KeyGenerationAlgorithm = typeof supportedAlgorithms[KeyGenerationAlgorithms]['names'][number];
|
|
224
|
+
|
|
225
|
+
export class AgentCryptoApi implements CryptoApi<
|
|
226
|
+
CryptoApiGenerateKeyParams, Jwk, GetPublicKeyParams,
|
|
227
|
+
CryptoApiDigestParams,
|
|
228
|
+
SignParams, VerifyParams,
|
|
229
|
+
CryptoApiCipherParams, CryptoApiCipherParams,
|
|
230
|
+
CryptoApiBytesToPublicKeyParams, PublicKeyToBytesParams,
|
|
231
|
+
CryptoApiBytesToPrivateKeyParams, PrivateKeyToBytesParams,
|
|
232
|
+
CryptoApiDeriveKeyParams<DeriveKeyAlgorithm>, Jwk,
|
|
233
|
+
CryptoApiDeriveKeyBytesParams<DeriveKeyAlgorithm>, Uint8Array,
|
|
234
|
+
WrapKeyParams, UnwrapKeyParams
|
|
235
|
+
> {
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* A private map that stores instances of cryptographic algorithm implementations. Each key in
|
|
239
|
+
* this map is an `AlgorithmConstructor`, and its corresponding value is an instance of a class
|
|
240
|
+
* that implements a specific cryptographic algorithm. This map is used to cache and reuse
|
|
241
|
+
* instances for performance optimization, ensuring that each algorithm is instantiated only once.
|
|
242
|
+
*/
|
|
243
|
+
private _algorithmInstances: Map<AlgorithmConstructor, InstanceType<typeof CryptoAlgorithm>> = new Map();
|
|
244
|
+
|
|
245
|
+
public async bytesToPrivateKey({ algorithm: algorithmIdentifier, privateKeyBytes }:
|
|
246
|
+
CryptoApiBytesToPrivateKeyParams
|
|
247
|
+
): Promise<Jwk> {
|
|
248
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
249
|
+
const algorithm = this.getAlgorithmName({ algorithm: algorithmIdentifier });
|
|
250
|
+
|
|
251
|
+
// Get the key converter based on the algorithm name.
|
|
252
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as KeyConverter<CryptoApiBytesToPrivateKeyParams, PrivateKeyToBytesParams>;
|
|
253
|
+
|
|
254
|
+
// Convert the byte array to a JWK.
|
|
255
|
+
const privateKey = await keyConverter.bytesToPrivateKey({ algorithm: algorithmIdentifier, privateKeyBytes });
|
|
256
|
+
|
|
257
|
+
return privateKey;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public async bytesToPublicKey({ algorithm: algorithmIdentifier, publicKeyBytes }:
|
|
261
|
+
CryptoApiBytesToPublicKeyParams
|
|
262
|
+
): Promise<Jwk> {
|
|
263
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
264
|
+
const algorithm = this.getAlgorithmName({ algorithm: algorithmIdentifier });
|
|
265
|
+
|
|
266
|
+
// Get the key converter based on the algorithm name.
|
|
267
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as AsymmetricKeyConverter<CryptoApiBytesToPublicKeyParams, PublicKeyToBytesParams>;
|
|
268
|
+
|
|
269
|
+
// Convert the byte array to a JWK.
|
|
270
|
+
const publicKey = await keyConverter.bytesToPublicKey({ algorithm: algorithmIdentifier, publicKeyBytes });
|
|
271
|
+
|
|
272
|
+
return publicKey;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
public async decrypt(params: CryptoApiCipherParams): Promise<Uint8Array> {
|
|
276
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
277
|
+
const algorithm = this.getAlgorithmName({ key: params.key });
|
|
278
|
+
|
|
279
|
+
// Get the cipher algorithm based on the algorithm name.
|
|
280
|
+
const cipher = this.getAlgorithm({ algorithm }) as Cipher<CipherParams, CipherParams>;
|
|
281
|
+
|
|
282
|
+
// Decrypt the data.
|
|
283
|
+
return await cipher.decrypt(params);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
public async deriveKey<T extends DeriveKeyAlgorithm>(
|
|
287
|
+
params: CryptoApiDeriveKeyParams<T>
|
|
288
|
+
): Promise<Jwk> {
|
|
289
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
290
|
+
const algorithm = this.getAlgorithmName({ algorithm: params.algorithm });
|
|
291
|
+
|
|
292
|
+
// Get the key derivation function based on the algorithm name.
|
|
293
|
+
const kdf = this.getAlgorithm({ algorithm }) as KeyBytesDeriver<DeriveKeyBytesParams, Uint8Array>;
|
|
294
|
+
|
|
295
|
+
let derivedKeyAlgorithm: CipherAlgorithm | KeyWrappingAlgorithm;
|
|
296
|
+
|
|
297
|
+
switch (params.algorithm) {
|
|
298
|
+
case 'HKDF-256':
|
|
299
|
+
case 'HKDF-384':
|
|
300
|
+
case 'HKDF-512': {
|
|
301
|
+
derivedKeyAlgorithm = params.derivedKeyAlgorithm as CipherAlgorithm | KeyWrappingAlgorithm;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
case 'PBES2-HS256+A128KW':
|
|
306
|
+
case 'PBES2-HS384+A192KW':
|
|
307
|
+
case 'PBES2-HS512+A256KW': {
|
|
308
|
+
derivedKeyAlgorithm = params.algorithm.split(/[-+]/)[2] as 'A128KW' | 'A192KW' | 'A256KW';
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
default:
|
|
313
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The specified "algorithm" is not supported: ${params.algorithm}`);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Determine the bit length of the derived key based on the given algorithm.
|
|
317
|
+
const length = +(derivedKeyAlgorithm.match(/\d+/)?.[0] ?? -1);
|
|
318
|
+
|
|
319
|
+
if (length === -1) {
|
|
320
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The derived key algorithm" is not supported: ${derivedKeyAlgorithm}`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Derive the byte array.
|
|
324
|
+
const privateKeyBytes = await kdf.deriveKeyBytes({ ...params, length });
|
|
325
|
+
|
|
326
|
+
return await this.bytesToPrivateKey({ algorithm: derivedKeyAlgorithm, privateKeyBytes });
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
public async deriveKeyBytes<T extends DeriveKeyAlgorithm>(
|
|
330
|
+
params: CryptoApiDeriveKeyBytesParams<T>
|
|
331
|
+
): Promise<Uint8Array> {
|
|
332
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
333
|
+
const algorithm = this.getAlgorithmName({ algorithm: params.algorithm });
|
|
334
|
+
|
|
335
|
+
// Get the key derivation function based on the algorithm name.
|
|
336
|
+
const kdf = this.getAlgorithm({ algorithm }) as KeyBytesDeriver<DeriveKeyBytesParams, Uint8Array>;
|
|
337
|
+
|
|
338
|
+
// Derive the byte array.
|
|
339
|
+
const derivedKeyBytes = await kdf.deriveKeyBytes(params);
|
|
340
|
+
|
|
341
|
+
return derivedKeyBytes;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Generates a hash digest of the provided data.
|
|
346
|
+
*
|
|
347
|
+
* @remarks
|
|
348
|
+
* A digest is the output of the hash function. It's a fixed-size string of bytes that uniquely
|
|
349
|
+
* represents the data input into the hash function. The digest is often used for data integrity
|
|
350
|
+
* checks, as any alteration in the input data results in a significantly different digest.
|
|
351
|
+
*
|
|
352
|
+
* It takes the algorithm identifier of the hash function and data to digest as input and returns
|
|
353
|
+
* the digest of the data.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* const cryptoApi = new AgentCryptoApi();
|
|
358
|
+
* const data = new Uint8Array([...]);
|
|
359
|
+
* const digest = await cryptoApi.digest({ algorithm: 'SHA-256', data });
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @param params - The parameters for the digest operation.
|
|
363
|
+
* @param params.algorithm - The name of hash function to use.
|
|
364
|
+
* @param params.data - The data to digest.
|
|
365
|
+
*
|
|
366
|
+
* @returns A Promise which will be fulfilled with the hash digest.
|
|
367
|
+
*/
|
|
368
|
+
public async digest({ algorithm, data }:
|
|
369
|
+
CryptoApiDigestParams
|
|
370
|
+
): Promise<Uint8Array> {
|
|
371
|
+
// Get the hash function implementation based on the specified `algorithm` parameter.
|
|
372
|
+
const hasher = this.getAlgorithm({ algorithm }) as Hasher<CryptoApiDigestParams>;
|
|
373
|
+
|
|
374
|
+
// Compute the hash.
|
|
375
|
+
const hash = await hasher.digest({ algorithm, data });
|
|
376
|
+
|
|
377
|
+
return hash;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public async encrypt(params: CryptoApiCipherParams): Promise<Uint8Array> {
|
|
381
|
+
// If th
|
|
382
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
383
|
+
const algorithm = this.getAlgorithmName({ key: params.key });
|
|
384
|
+
|
|
385
|
+
// Get the cipher algorithm based on the algorithm name.
|
|
386
|
+
const cipher = this.getAlgorithm({ algorithm }) as Cipher<CipherParams, CipherParams>;
|
|
387
|
+
|
|
388
|
+
// Encrypt the data and return the ciphertext.
|
|
389
|
+
return await cipher.encrypt(params);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
public async generateKey(params: CryptoApiGenerateKeyParams): Promise<Jwk> {
|
|
393
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
394
|
+
const algorithm = this.getAlgorithmName({ algorithm: params.algorithm });
|
|
395
|
+
|
|
396
|
+
// Get the key generator implementation based on the algorithm.
|
|
397
|
+
const keyGenerator = this.getAlgorithm({ algorithm }) as AsymmetricKeyGenerator<CryptoApiGenerateKeyParams, Jwk, GetPublicKeyParams>;
|
|
398
|
+
|
|
399
|
+
// Generate the key.
|
|
400
|
+
const privateKey = await keyGenerator.generateKey({ algorithm: params.algorithm });
|
|
401
|
+
|
|
402
|
+
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
403
|
+
privateKey.kid ??= await computeJwkThumbprint({ jwk: privateKey });
|
|
404
|
+
|
|
405
|
+
return privateKey;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// ! TODO: Remove this once the `Dsa` interface is updated in @enbox/crypto to remove KMS-specific methods.
|
|
409
|
+
public async getKeyUri(_params: KmsGetKeyUriParams): Promise<string> {
|
|
410
|
+
throw new Error('Method not implemented.');
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
public async getPublicKey({ key }:
|
|
414
|
+
GetPublicKeyParams
|
|
415
|
+
): Promise<Jwk> {
|
|
416
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
417
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
418
|
+
|
|
419
|
+
// Get the key generator based on the algorithm name.
|
|
420
|
+
const keyGenerator = this.getAlgorithm({ algorithm }) as AsymmetricKeyGenerator<CryptoApiGenerateKeyParams, Jwk, GetPublicKeyParams>;
|
|
421
|
+
|
|
422
|
+
// Get the public key properties from the private JWK.
|
|
423
|
+
const publicKey = await keyGenerator.getPublicKey({ key });
|
|
424
|
+
|
|
425
|
+
return publicKey;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
public async privateKeyToBytes({ privateKey }: { privateKey: Jwk; }): Promise<Uint8Array> {
|
|
429
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
430
|
+
const algorithm = this.getAlgorithmName({ key: privateKey });
|
|
431
|
+
|
|
432
|
+
// Get the key converter based on the algorithm name.
|
|
433
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as KeyConverter<CryptoApiBytesToPrivateKeyParams, PrivateKeyToBytesParams>;
|
|
434
|
+
|
|
435
|
+
// Convert the JWK to a byte array.
|
|
436
|
+
const privateKeyBytes = await keyConverter.privateKeyToBytes({ privateKey });
|
|
437
|
+
|
|
438
|
+
return privateKeyBytes;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
public async publicKeyToBytes({ publicKey }: { publicKey: Jwk; }): Promise<Uint8Array> {
|
|
442
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
443
|
+
const algorithm = this.getAlgorithmName({ key: publicKey });
|
|
444
|
+
|
|
445
|
+
// Get the key converter based on the algorithm name.
|
|
446
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as AsymmetricKeyConverter<CryptoApiBytesToPublicKeyParams, PublicKeyToBytesParams>;
|
|
447
|
+
|
|
448
|
+
// Convert the JWK to a byte array.
|
|
449
|
+
const publicKeyBytes = await keyConverter.publicKeyToBytes({ publicKey });
|
|
450
|
+
|
|
451
|
+
return publicKeyBytes;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
public async sign({ key, data }:
|
|
455
|
+
SignParams
|
|
456
|
+
): Promise<Uint8Array> {
|
|
457
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
458
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
459
|
+
|
|
460
|
+
// Get the signature algorithm based on the algorithm name.
|
|
461
|
+
const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
|
|
462
|
+
|
|
463
|
+
// Sign the data.
|
|
464
|
+
const signature = signer.sign({ data, key });
|
|
465
|
+
|
|
466
|
+
return signature;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
public async unwrapKey(params: UnwrapKeyParams): Promise<Jwk> {
|
|
470
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
471
|
+
const algorithm = this.getAlgorithmName({ key: params.decryptionKey });
|
|
472
|
+
|
|
473
|
+
// Get the key wrapping algorithm based on the algorithm name.
|
|
474
|
+
const keyWrapper = this.getAlgorithm({ algorithm }) as KeyWrapper<WrapKeyParams, UnwrapKeyParams>;
|
|
475
|
+
|
|
476
|
+
// decrypt the key and return the ciphertext.
|
|
477
|
+
return await keyWrapper.unwrapKey(params);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
public async verify({ key, signature, data }:
|
|
481
|
+
VerifyParams
|
|
482
|
+
): Promise<boolean> {
|
|
483
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
484
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
485
|
+
|
|
486
|
+
// Get the signature algorithm based on the algorithm name.
|
|
487
|
+
const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
|
|
488
|
+
|
|
489
|
+
// Verify the signature.
|
|
490
|
+
const isSignatureValid = signer.verify({ key, signature, data });
|
|
491
|
+
|
|
492
|
+
return isSignatureValid;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
public async wrapKey(params: WrapKeyParams): Promise<Uint8Array> {
|
|
496
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
497
|
+
const algorithm = this.getAlgorithmName({ key: params.encryptionKey });
|
|
498
|
+
|
|
499
|
+
// Get the key wrapping algorithm based on the algorithm name.
|
|
500
|
+
const keyWrapper = this.getAlgorithm({ algorithm }) as KeyWrapper<WrapKeyParams, UnwrapKeyParams>;
|
|
501
|
+
|
|
502
|
+
// Encrypt the key and return the ciphertext.
|
|
503
|
+
return await keyWrapper.wrapKey(params);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Retrieves an algorithm implementation instance based on the provided algorithm name.
|
|
508
|
+
*
|
|
509
|
+
* @remarks
|
|
510
|
+
* This method checks if the requested algorithm is supported and returns a cached instance
|
|
511
|
+
* if available. If an instance does not exist, it creates and caches a new one. This approach
|
|
512
|
+
* optimizes performance by reusing algorithm instances across cryptographic operations.
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```ts
|
|
516
|
+
* const signer = this.getAlgorithm({ algorithm: 'Ed25519' });
|
|
517
|
+
* ```
|
|
518
|
+
*
|
|
519
|
+
* @param params - The parameters for retrieving the algorithm implementation.
|
|
520
|
+
* @param params.algorithm - The name of the algorithm to retrieve.
|
|
521
|
+
*
|
|
522
|
+
* @returns An instance of the requested algorithm implementation.
|
|
523
|
+
*
|
|
524
|
+
* @throws Error if the requested algorithm is not supported.
|
|
525
|
+
*/
|
|
526
|
+
private getAlgorithm({ algorithm }: {
|
|
527
|
+
algorithm: SupportedAlgorithm;
|
|
528
|
+
}): InstanceType<typeof CryptoAlgorithm> {
|
|
529
|
+
// Check if algorithm is supported.
|
|
530
|
+
const AlgorithmImplementation = supportedAlgorithms[algorithm]?.['implementation'];
|
|
531
|
+
if (!AlgorithmImplementation) {
|
|
532
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Check if instance already exists for the `AlgorithmImplementation`.
|
|
536
|
+
if (!this._algorithmInstances.has(AlgorithmImplementation)) {
|
|
537
|
+
// If not, create a new instance and store it in the cache
|
|
538
|
+
this._algorithmInstances.set(AlgorithmImplementation, new AlgorithmImplementation());
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Return the cached instance
|
|
542
|
+
return this._algorithmInstances.get(AlgorithmImplementation)!;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Determines the algorithm name based on the key's properties.
|
|
547
|
+
*
|
|
548
|
+
* @remarks
|
|
549
|
+
* This method facilitates the identification of the correct algorithm for cryptographic
|
|
550
|
+
* operations based on the `alg` or `crv` properties of a {@link Jwk | JWK}.
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* ```ts
|
|
554
|
+
* const key = { ... }; // Public key in JWK format
|
|
555
|
+
* const algorithm = this.getAlgorithmName({ key });
|
|
556
|
+
* ```
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* ```ts
|
|
560
|
+
* const algorithm = this.getAlgorithmName({ algorithm: 'ES256' });
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @param params - The parameters for determining the algorithm name.
|
|
564
|
+
* @param params.key - A JWK containing the `alg` or `crv` properties.
|
|
565
|
+
*
|
|
566
|
+
* @returns The algorithm name associated with the key.
|
|
567
|
+
*
|
|
568
|
+
* @throws Error if the algorithm name cannot be determined from the provided input.
|
|
569
|
+
*/
|
|
570
|
+
private getAlgorithmName({ key }: { key: Jwk }): SupportedAlgorithm;
|
|
571
|
+
private getAlgorithmName({ algorithm }: { algorithm: string }): SupportedAlgorithm;
|
|
572
|
+
private getAlgorithmName({ algorithm, key }: {
|
|
573
|
+
algorithm?: string;
|
|
574
|
+
key?: { alg?: string, crv?: string };
|
|
575
|
+
}): SupportedAlgorithm {
|
|
576
|
+
const algProperty = key?.alg ?? algorithm;
|
|
577
|
+
const crvProperty = key?.crv;
|
|
578
|
+
|
|
579
|
+
for (const algorithmIdentifier of Object.keys(supportedAlgorithms) as SupportedAlgorithm[]) {
|
|
580
|
+
const algorithmNames = supportedAlgorithms[algorithmIdentifier].names as readonly string[];
|
|
581
|
+
if (algProperty && algorithmNames.includes(algProperty)) {
|
|
582
|
+
return algorithmIdentifier;
|
|
583
|
+
} else if (crvProperty && algorithmNames.includes(crvProperty)) {
|
|
584
|
+
return algorithmIdentifier;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported,
|
|
589
|
+
`Algorithm not supported based on provided input: alg=${algProperty}, crv=${crvProperty}. ` +
|
|
590
|
+
'Please check the documentation for the list of supported algorithms.'
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
}
|