@open-e2ee/signal-protocol-sdk 7.1.0 → 8.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.
- package/ADAPTERS.md +55 -6
- package/ARCHITECTURE.md +1 -0
- package/CHANGELOG.md +565 -0
- package/README.md +19 -5
- package/SECURITY.md +2 -2
- package/THIRD_PARTY_NOTICES.md +429 -0
- package/TROUBLESHOOTING.md +10 -0
- package/client/README.md +1 -2
- package/device/README.md +50 -19
- package/dist/client/client.d.ts +31 -5
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +149 -90
- package/dist/client/client.js.map +1 -1
- package/dist/client/event-hooks.d.ts.map +1 -1
- package/dist/client/event-hooks.js +2 -0
- package/dist/client/event-hooks.js.map +1 -1
- package/dist/client/files.d.ts.map +1 -1
- package/dist/client/files.js +19 -2
- package/dist/client/files.js.map +1 -1
- package/dist/client/headless.d.ts +1 -1
- package/dist/client/headless.js +1 -1
- package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
- package/dist/client/hosted-anonymous-delivery.js +2 -1
- package/dist/client/hosted-anonymous-delivery.js.map +1 -1
- package/dist/client/hosted-connection.d.ts +1 -1
- package/dist/client/hosted-connection.d.ts.map +1 -1
- package/dist/client/hosted-connection.js +8 -12
- package/dist/client/hosted-connection.js.map +1 -1
- package/dist/client/hosted-group-authority.d.ts.map +1 -1
- package/dist/client/hosted-group-authority.js +2 -1
- package/dist/client/hosted-group-authority.js.map +1 -1
- package/dist/client/hosted-groups.d.ts.map +1 -1
- package/dist/client/hosted-groups.js +2 -1
- package/dist/client/hosted-groups.js.map +1 -1
- package/dist/client/hosted-push.d.ts.map +1 -1
- package/dist/client/hosted-push.js +2 -1
- package/dist/client/hosted-push.js.map +1 -1
- package/dist/client/hosted-transport.d.ts.map +1 -1
- package/dist/client/hosted-transport.js +10 -11
- package/dist/client/hosted-transport.js.map +1 -1
- package/dist/client/hosted-trust.d.ts +11 -11
- package/dist/client/hosted-trust.d.ts.map +1 -1
- package/dist/client/hosted-trust.js +12 -12
- package/dist/client/hosted-trust.js.map +1 -1
- package/dist/client/messages.d.ts.map +1 -1
- package/dist/client/messages.js +6 -1
- package/dist/client/messages.js.map +1 -1
- package/dist/client/multi-device.d.ts.map +1 -1
- package/dist/client/multi-device.js +2 -1
- package/dist/client/multi-device.js.map +1 -1
- package/dist/client/relay-lifecycle.d.ts +6 -2
- package/dist/client/relay-lifecycle.d.ts.map +1 -1
- package/dist/client/relay-lifecycle.js.map +1 -1
- package/dist/client/relay-work.d.ts +30 -0
- package/dist/client/relay-work.d.ts.map +1 -0
- package/dist/client/relay-work.js +189 -0
- package/dist/client/relay-work.js.map +1 -0
- package/dist/client/retry.d.ts.map +1 -1
- package/dist/client/retry.js +2 -0
- package/dist/client/retry.js.map +1 -1
- package/dist/client/sessions.d.ts.map +1 -1
- package/dist/client/sessions.js +3 -0
- package/dist/client/sessions.js.map +1 -1
- package/dist/client/signal-service-cipher.d.ts +39 -16
- package/dist/client/signal-service-cipher.d.ts.map +1 -1
- package/dist/client/signal-service-cipher.js +100 -60
- package/dist/client/signal-service-cipher.js.map +1 -1
- package/dist/client/state.d.ts +8 -0
- package/dist/client/state.d.ts.map +1 -1
- package/dist/client/state.js +11 -0
- package/dist/client/state.js.map +1 -1
- package/dist/client/types.d.ts +5 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js.map +1 -1
- package/dist/device/connection.d.ts +1 -1
- package/dist/device/connection.d.ts.map +1 -1
- package/dist/device/connection.js +4 -11
- package/dist/device/connection.js.map +1 -1
- package/dist/device/device-id.d.ts +10 -4
- package/dist/device/device-id.d.ts.map +1 -1
- package/dist/device/device-id.js +15 -9
- package/dist/device/device-id.js.map +1 -1
- package/dist/device/device-name-crypto.d.ts.map +1 -1
- package/dist/device/device-name-crypto.js +2 -2
- package/dist/device/device-name-crypto.js.map +1 -1
- package/dist/device/expo/index.d.ts +25 -8
- package/dist/device/expo/index.d.ts.map +1 -1
- package/dist/device/expo/index.js +58 -8
- package/dist/device/expo/index.js.map +1 -1
- package/dist/device/index.d.ts +2 -2
- package/dist/device/index.d.ts.map +1 -1
- package/dist/device/index.js +3 -2
- package/dist/device/index.js.map +1 -1
- package/dist/device/lifecycle/index.d.ts +4 -2
- package/dist/device/lifecycle/index.d.ts.map +1 -1
- package/dist/device/lifecycle/index.js.map +1 -1
- package/dist/device/lifecycle/manager.d.ts +12 -14
- package/dist/device/lifecycle/manager.d.ts.map +1 -1
- package/dist/device/lifecycle/manager.js +52 -97
- package/dist/device/lifecycle/manager.js.map +1 -1
- package/dist/device/lifecycle/types.d.ts +26 -16
- package/dist/device/lifecycle/types.d.ts.map +1 -1
- package/dist/device/lifecycle/utils.d.ts +8 -4
- package/dist/device/lifecycle/utils.d.ts.map +1 -1
- package/dist/device/lifecycle/utils.js +12 -16
- package/dist/device/lifecycle/utils.js.map +1 -1
- package/dist/device/provisioning.d.ts.map +1 -1
- package/dist/device/provisioning.js +6 -8
- package/dist/device/provisioning.js.map +1 -1
- package/dist/device/react-native/index.d.ts +40 -0
- package/dist/device/react-native/index.d.ts.map +1 -0
- package/dist/device/react-native/index.js +74 -0
- package/dist/device/react-native/index.js.map +1 -0
- package/dist/device/transfer.d.ts +15 -7
- package/dist/device/transfer.d.ts.map +1 -1
- package/dist/device/transfer.js +30 -22
- package/dist/device/transfer.js.map +1 -1
- package/dist/device/types.d.ts +15 -7
- package/dist/device/types.d.ts.map +1 -1
- package/dist/device/types.js.map +1 -1
- package/dist/device/vault-text.d.ts +13 -0
- package/dist/device/vault-text.d.ts.map +1 -0
- package/dist/device/vault-text.js +17 -0
- package/dist/device/vault-text.js.map +1 -0
- package/dist/hooks/use-key-rotation.d.ts.map +1 -1
- package/dist/hooks/use-key-rotation.js +2 -1
- package/dist/hooks/use-key-rotation.js.map +1 -1
- package/dist/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/internal/crypto/index.d.ts +3 -3
- package/dist/internal/crypto/index.d.ts.map +1 -1
- package/dist/internal/crypto/index.js +3 -3
- package/dist/internal/crypto/index.js.map +1 -1
- package/dist/internal/crypto/random-host.d.ts +1 -1
- package/dist/internal/crypto/random-host.d.ts.map +1 -1
- package/dist/internal/crypto/random-host.js +3 -2
- package/dist/internal/crypto/random-host.js.map +1 -1
- package/dist/internal/crypto/random-host.native.d.ts +6 -1
- package/dist/internal/crypto/random-host.native.d.ts.map +1 -1
- package/dist/internal/crypto/random-host.native.js +14 -10
- package/dist/internal/crypto/random-host.native.js.map +1 -1
- package/dist/internal/crypto/random-host.node.d.ts +1 -1
- package/dist/internal/crypto/random-host.node.d.ts.map +1 -1
- package/dist/internal/crypto/random-host.node.js +1 -1
- package/dist/internal/crypto/random-host.node.js.map +1 -1
- package/dist/internal/crypto/random.d.ts +12 -3
- package/dist/internal/crypto/random.d.ts.map +1 -1
- package/dist/internal/crypto/random.js +14 -3
- package/dist/internal/crypto/random.js.map +1 -1
- package/dist/internal/crypto/utils.d.ts.map +1 -1
- package/dist/internal/crypto/utils.js +10 -6
- package/dist/internal/crypto/utils.js.map +1 -1
- package/dist/internal/encoding/proto/primitives.d.ts +2 -0
- package/dist/internal/encoding/proto/primitives.d.ts.map +1 -1
- package/dist/internal/encoding/proto/primitives.js +2 -1
- package/dist/internal/encoding/proto/primitives.js.map +1 -1
- package/dist/internal/encoding/proto/signal-message.d.ts +1 -0
- package/dist/internal/encoding/proto/signal-message.d.ts.map +1 -1
- package/dist/internal/encoding/proto/signal-message.js +2 -1
- package/dist/internal/encoding/proto/signal-message.js.map +1 -1
- package/dist/internal/groups/authority-certificate.d.ts.map +1 -1
- package/dist/internal/groups/authority-certificate.js +2 -1
- package/dist/internal/groups/authority-certificate.js.map +1 -1
- package/dist/internal/groups/encrypted-state.d.ts.map +1 -1
- package/dist/internal/groups/encrypted-state.js +9 -7
- package/dist/internal/groups/encrypted-state.js.map +1 -1
- package/dist/internal/groups/invite-link.d.ts.map +1 -1
- package/dist/internal/groups/invite-link.js +2 -1
- package/dist/internal/groups/invite-link.js.map +1 -1
- package/dist/internal/groups/manager.d.ts.map +1 -1
- package/dist/internal/groups/manager.js +5 -5
- package/dist/internal/groups/manager.js.map +1 -1
- package/dist/internal/groups/sdk-store.d.ts.map +1 -1
- package/dist/internal/groups/sdk-store.js +4 -3
- package/dist/internal/groups/sdk-store.js.map +1 -1
- package/dist/internal/groups/server-engine.d.ts +6 -1
- package/dist/internal/groups/server-engine.d.ts.map +1 -1
- package/dist/internal/groups/server-engine.js +11 -15
- package/dist/internal/groups/server-engine.js.map +1 -1
- package/dist/internal/groups/wire.d.ts.map +1 -1
- package/dist/internal/groups/wire.js +2 -1
- package/dist/internal/groups/wire.js.map +1 -1
- package/dist/internal/platform/blob.d.ts +10 -0
- package/dist/internal/platform/blob.d.ts.map +1 -0
- package/dist/internal/platform/blob.js +30 -0
- package/dist/internal/platform/blob.js.map +1 -0
- package/dist/internal/platform/env.d.ts +13 -0
- package/dist/internal/platform/env.d.ts.map +1 -0
- package/dist/internal/platform/env.js +17 -0
- package/dist/internal/platform/env.js.map +1 -0
- package/dist/internal/platform/index.d.ts +5 -0
- package/dist/internal/platform/index.d.ts.map +1 -0
- package/dist/internal/platform/index.js +5 -0
- package/dist/internal/platform/index.js.map +1 -0
- package/dist/internal/platform/url.d.ts +72 -0
- package/dist/internal/platform/url.d.ts.map +1 -0
- package/dist/internal/platform/url.js +275 -0
- package/dist/internal/platform/url.js.map +1 -0
- package/dist/internal/platform/utf8.d.ts +28 -0
- package/dist/internal/platform/utf8.d.ts.map +1 -0
- package/dist/internal/platform/utf8.js +167 -0
- package/dist/internal/platform/utf8.js.map +1 -0
- package/dist/internal/protocol/sealed-sender/delivery-token.d.ts +1 -1
- package/dist/internal/protocol/sealed-sender/delivery-token.d.ts.map +1 -1
- package/dist/internal/protocol/sealed-sender/delivery-token.js +5 -10
- package/dist/internal/protocol/sealed-sender/delivery-token.js.map +1 -1
- package/dist/internal/protocol/sealed-sender/proto/index.d.ts.map +1 -1
- package/dist/internal/protocol/sealed-sender/proto/index.js +2 -4
- package/dist/internal/protocol/sealed-sender/proto/index.js.map +1 -1
- package/dist/internal/protocol/sender-keys/manager.d.ts.map +1 -1
- package/dist/internal/protocol/sender-keys/manager.js +4 -3
- package/dist/internal/protocol/sender-keys/manager.js.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/noble-pq/index.d.ts +4 -4
- package/dist/internal/protocol/spqr/ml-kem-braid/noble-pq/index.d.ts.map +1 -1
- package/dist/internal/protocol/spqr/ml-kem-braid/noble-pq/index.js +8 -10
- package/dist/internal/protocol/spqr/ml-kem-braid/noble-pq/index.js.map +1 -1
- package/dist/internal/protocol/username/hash.d.ts.map +1 -1
- package/dist/internal/protocol/username/hash.js +18 -3
- package/dist/internal/protocol/username/hash.js.map +1 -1
- package/dist/internal/protocol/username/link.d.ts.map +1 -1
- package/dist/internal/protocol/username/link.js +2 -1
- package/dist/internal/protocol/username/link.js.map +1 -1
- package/dist/internal/protocol/username/proof.d.ts.map +1 -1
- package/dist/internal/protocol/username/proof.js +2 -1
- package/dist/internal/protocol/username/proof.js.map +1 -1
- package/dist/internal/sesame/device-registry.d.ts +22 -6
- package/dist/internal/sesame/device-registry.d.ts.map +1 -1
- package/dist/internal/sesame/device-registry.js +18 -12
- package/dist/internal/sesame/device-registry.js.map +1 -1
- package/dist/internal/sesame/index.d.ts +1 -1
- package/dist/internal/sesame/index.d.ts.map +1 -1
- package/dist/internal/sesame/manager.d.ts.map +1 -1
- package/dist/internal/sesame/manager.js +3 -2
- package/dist/internal/sesame/manager.js.map +1 -1
- package/dist/internal/session/index.d.ts +1 -2
- package/dist/internal/session/index.d.ts.map +1 -1
- package/dist/internal/session/index.js +1 -1
- package/dist/internal/session/index.js.map +1 -1
- package/dist/internal/session/validation.d.ts +0 -80
- package/dist/internal/session/validation.d.ts.map +1 -1
- package/dist/internal/session/validation.js +0 -134
- package/dist/internal/session/validation.js.map +1 -1
- package/dist/local/store/device-record.d.ts +18 -0
- package/dist/local/store/device-record.d.ts.map +1 -0
- package/dist/local/store/device-record.js +34 -0
- package/dist/local/store/device-record.js.map +1 -0
- package/dist/local/store/expo/adapter.d.ts +59 -238
- package/dist/local/store/expo/adapter.d.ts.map +1 -1
- package/dist/local/store/expo/adapter.js +83 -640
- package/dist/local/store/expo/adapter.js.map +1 -1
- package/dist/local/store/expo/driver.d.ts +16 -0
- package/dist/local/store/expo/driver.d.ts.map +1 -0
- package/dist/local/store/expo/driver.js +141 -0
- package/dist/local/store/expo/driver.js.map +1 -0
- package/dist/local/store/expo/index.d.ts +3 -12
- package/dist/local/store/expo/index.d.ts.map +1 -1
- package/dist/local/store/expo/index.js +1 -8
- package/dist/local/store/expo/index.js.map +1 -1
- package/dist/local/store/expo/maintenance.d.ts +3 -2
- package/dist/local/store/expo/maintenance.d.ts.map +1 -1
- package/dist/local/store/expo/maintenance.js +5 -34
- package/dist/local/store/expo/maintenance.js.map +1 -1
- package/dist/local/store/index.d.ts +10 -5
- package/dist/local/store/index.d.ts.map +1 -1
- package/dist/local/store/index.js +10 -5
- package/dist/local/store/index.js.map +1 -1
- package/dist/local/store/key-value/adapter.d.ts +326 -0
- package/dist/local/store/key-value/adapter.d.ts.map +1 -0
- package/dist/local/store/key-value/adapter.js +1472 -0
- package/dist/local/store/key-value/adapter.js.map +1 -0
- package/dist/local/store/{react-native → key-value}/backend-conformance.d.ts +7 -8
- package/dist/local/store/key-value/backend-conformance.d.ts.map +1 -0
- package/dist/local/store/{react-native → key-value}/backend-conformance.js +4 -5
- package/dist/local/store/key-value/backend-conformance.js.map +1 -0
- package/dist/local/store/key-value/batch.d.ts +41 -0
- package/dist/local/store/key-value/batch.d.ts.map +1 -0
- package/dist/local/store/key-value/batch.js +77 -0
- package/dist/local/store/key-value/batch.js.map +1 -0
- package/dist/local/store/key-value/device-record.d.ts +23 -0
- package/dist/local/store/key-value/device-record.d.ts.map +1 -0
- package/dist/local/store/key-value/device-record.js +33 -0
- package/dist/local/store/key-value/device-record.js.map +1 -0
- package/dist/local/store/key-value/index.d.ts +24 -0
- package/dist/local/store/key-value/index.d.ts.map +1 -0
- package/dist/local/store/key-value/index.js +8 -0
- package/dist/local/store/key-value/index.js.map +1 -0
- package/dist/local/store/key-value/realm/index.d.ts +83 -0
- package/dist/local/store/key-value/realm/index.d.ts.map +1 -0
- package/dist/local/store/key-value/realm/index.js +102 -0
- package/dist/local/store/key-value/realm/index.js.map +1 -0
- package/dist/local/store/{react-native → key-value}/storage.d.ts +6 -6
- package/dist/local/store/key-value/storage.d.ts.map +1 -0
- package/dist/local/store/key-value/storage.js.map +1 -0
- package/dist/local/store/key-value/transactional-backend.d.ts +50 -0
- package/dist/local/store/key-value/transactional-backend.d.ts.map +1 -0
- package/dist/local/store/key-value/transactional-backend.js +90 -0
- package/dist/local/store/key-value/transactional-backend.js.map +1 -0
- package/dist/local/store/key-value/value-key.d.ts +34 -0
- package/dist/local/store/key-value/value-key.d.ts.map +1 -0
- package/dist/local/store/key-value/value-key.js +83 -0
- package/dist/local/store/key-value/value-key.js.map +1 -0
- package/dist/local/store/memory/adapter.d.ts +8 -0
- package/dist/local/store/memory/adapter.d.ts.map +1 -1
- package/dist/local/store/memory/adapter.js +60 -75
- package/dist/local/store/memory/adapter.js.map +1 -1
- package/dist/local/store/node/adapter.d.ts +74 -130
- package/dist/local/store/node/adapter.d.ts.map +1 -1
- package/dist/local/store/node/adapter.js +83 -727
- package/dist/local/store/node/adapter.js.map +1 -1
- package/dist/local/store/node/index.d.ts +8 -6
- package/dist/local/store/node/index.d.ts.map +1 -1
- package/dist/local/store/node/index.js +3 -7
- package/dist/local/store/node/index.js.map +1 -1
- package/dist/local/store/node/maintenance.d.ts +5 -0
- package/dist/local/store/node/maintenance.d.ts.map +1 -0
- package/dist/local/store/node/maintenance.js +7 -0
- package/dist/local/store/node/maintenance.js.map +1 -0
- package/dist/local/store/node/owner-lock.d.ts +26 -0
- package/dist/local/store/node/owner-lock.d.ts.map +1 -0
- package/dist/local/store/node/owner-lock.js +88 -0
- package/dist/local/store/node/owner-lock.js.map +1 -0
- package/dist/local/store/react-native/adapter.d.ts +63 -273
- package/dist/local/store/react-native/adapter.d.ts.map +1 -1
- package/dist/local/store/react-native/adapter.js +80 -1405
- package/dist/local/store/react-native/adapter.js.map +1 -1
- package/dist/local/store/react-native/index.d.ts +7 -19
- package/dist/local/store/react-native/index.d.ts.map +1 -1
- package/dist/local/store/react-native/index.js +2 -7
- package/dist/local/store/react-native/index.js.map +1 -1
- package/dist/local/store/react-native/maintenance.d.ts +5 -0
- package/dist/local/store/react-native/maintenance.d.ts.map +1 -0
- package/dist/local/store/react-native/maintenance.js +7 -0
- package/dist/local/store/react-native/maintenance.js.map +1 -0
- package/dist/local/store/sqlite/better-sqlite3-multiple-ciphers-driver.d.ts +38 -0
- package/dist/local/store/sqlite/better-sqlite3-multiple-ciphers-driver.d.ts.map +1 -0
- package/dist/local/store/sqlite/better-sqlite3-multiple-ciphers-driver.js +196 -0
- package/dist/local/store/sqlite/better-sqlite3-multiple-ciphers-driver.js.map +1 -0
- package/dist/local/store/sqlite/database.d.ts +20 -0
- package/dist/local/store/sqlite/database.d.ts.map +1 -0
- package/dist/local/store/sqlite/database.js +41 -0
- package/dist/local/store/sqlite/database.js.map +1 -0
- package/dist/local/store/sqlite/driver.d.ts +138 -0
- package/dist/local/store/sqlite/driver.d.ts.map +1 -0
- package/dist/local/store/sqlite/driver.js +10 -0
- package/dist/local/store/sqlite/driver.js.map +1 -0
- package/dist/local/store/sqlite/encryption.d.ts +61 -0
- package/dist/local/store/sqlite/encryption.d.ts.map +1 -0
- package/dist/local/store/sqlite/encryption.js +98 -0
- package/dist/local/store/sqlite/encryption.js.map +1 -0
- package/dist/local/store/sqlite/errors.d.ts +70 -0
- package/dist/local/store/sqlite/errors.d.ts.map +1 -0
- package/dist/local/store/sqlite/errors.js +101 -0
- package/dist/local/store/sqlite/errors.js.map +1 -0
- package/dist/local/store/sqlite/executor.d.ts +22 -0
- package/dist/local/store/sqlite/executor.d.ts.map +1 -0
- package/dist/local/store/sqlite/executor.js +129 -0
- package/dist/local/store/sqlite/executor.js.map +1 -0
- package/dist/local/store/sqlite/index.d.ts +20 -0
- package/dist/local/store/sqlite/index.d.ts.map +1 -0
- package/dist/local/store/sqlite/index.js +18 -0
- package/dist/local/store/sqlite/index.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/account.d.ts +45 -0
- package/dist/local/store/sqlite/key-storage/account.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/account.js +125 -0
- package/dist/local/store/sqlite/key-storage/account.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/contact-identities.d.ts +19 -0
- package/dist/local/store/sqlite/key-storage/contact-identities.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/contact-identities.js +85 -0
- package/dist/local/store/sqlite/key-storage/contact-identities.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/context.d.ts +19 -0
- package/dist/local/store/sqlite/key-storage/context.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/context.js +18 -0
- package/dist/local/store/sqlite/key-storage/context.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/identity-keys.d.ts +18 -0
- package/dist/local/store/sqlite/key-storage/identity-keys.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/identity-keys.js +88 -0
- package/dist/local/store/sqlite/key-storage/identity-keys.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/index.d.ts +108 -0
- package/dist/local/store/sqlite/key-storage/index.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/index.js +241 -0
- package/dist/local/store/sqlite/key-storage/index.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/message-records.d.ts +41 -0
- package/dist/local/store/sqlite/key-storage/message-records.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/message-records.js +133 -0
- package/dist/local/store/sqlite/key-storage/message-records.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/metadata.d.ts +16 -0
- package/dist/local/store/sqlite/key-storage/metadata.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/metadata.js +53 -0
- package/dist/local/store/sqlite/key-storage/metadata.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/one-time-prekeys.d.ts +36 -0
- package/dist/local/store/sqlite/key-storage/one-time-prekeys.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/one-time-prekeys.js +129 -0
- package/dist/local/store/sqlite/key-storage/one-time-prekeys.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/session-trust.d.ts +10 -0
- package/dist/local/store/sqlite/key-storage/session-trust.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/session-trust.js +88 -0
- package/dist/local/store/sqlite/key-storage/session-trust.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/sessions.d.ts +50 -0
- package/dist/local/store/sqlite/key-storage/sessions.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/sessions.js +209 -0
- package/dist/local/store/sqlite/key-storage/sessions.js.map +1 -0
- package/dist/local/store/sqlite/key-storage/signed-prekeys.d.ts +69 -0
- package/dist/local/store/sqlite/key-storage/signed-prekeys.d.ts.map +1 -0
- package/dist/local/store/sqlite/key-storage/signed-prekeys.js +219 -0
- package/dist/local/store/sqlite/key-storage/signed-prekeys.js.map +1 -0
- package/dist/local/store/sqlite/maintenance.d.ts +7 -0
- package/dist/local/store/sqlite/maintenance.d.ts.map +1 -0
- package/dist/local/store/sqlite/maintenance.js +32 -0
- package/dist/local/store/sqlite/maintenance.js.map +1 -0
- package/dist/local/store/sqlite/migrations.d.ts +31 -0
- package/dist/local/store/sqlite/migrations.d.ts.map +1 -0
- package/dist/local/store/sqlite/migrations.js +232 -0
- package/dist/local/store/sqlite/migrations.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/ec-one-time-prekey.d.ts +17 -22
- package/dist/local/store/sqlite/models/ec-one-time-prekey.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/ec-one-time-prekey.js +61 -99
- package/dist/local/store/sqlite/models/ec-one-time-prekey.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/ec-signed-prekey.d.ts +22 -25
- package/dist/local/store/sqlite/models/ec-signed-prekey.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/ec-signed-prekey.js +82 -128
- package/dist/local/store/sqlite/models/ec-signed-prekey.js.map +1 -0
- package/dist/local/store/sqlite/models/identity-key-id.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/identity-key-id.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/index.d.ts +5 -5
- package/dist/local/store/sqlite/models/index.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/index.js +12 -5
- package/dist/local/store/sqlite/models/index.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-one-time-prekey.d.ts +17 -20
- package/dist/local/store/sqlite/models/kyber-one-time-prekey.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-one-time-prekey.js +65 -105
- package/dist/local/store/sqlite/models/kyber-one-time-prekey.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-prekey-used.d.ts +3 -3
- package/dist/local/store/sqlite/models/kyber-prekey-used.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-prekey-used.js +13 -25
- package/dist/local/store/sqlite/models/kyber-prekey-used.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-prekey.d.ts +16 -19
- package/dist/local/store/sqlite/models/kyber-prekey.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/kyber-prekey.js +67 -111
- package/dist/local/store/sqlite/models/kyber-prekey.js.map +1 -0
- package/dist/local/store/sqlite/models/local-identity.d.ts +51 -0
- package/dist/local/store/sqlite/models/local-identity.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/local-identity.js +150 -0
- package/dist/local/store/sqlite/models/local-identity.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/message-record.d.ts +15 -44
- package/dist/local/store/sqlite/models/message-record.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/message-record.js +43 -109
- package/dist/local/store/sqlite/models/message-record.js.map +1 -0
- package/dist/local/store/sqlite/models/recipient-identity.d.ts +21 -0
- package/dist/local/store/sqlite/models/recipient-identity.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/recipient-identity.js +59 -0
- package/dist/local/store/sqlite/models/recipient-identity.js.map +1 -0
- package/dist/local/store/sqlite/models/replaced-prekeys.d.ts +23 -0
- package/dist/local/store/sqlite/models/replaced-prekeys.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/replaced-prekeys.js +42 -0
- package/dist/local/store/sqlite/models/replaced-prekeys.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/sender-key.d.ts +22 -23
- package/dist/local/store/sqlite/models/sender-key.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/sender-key.js +66 -94
- package/dist/local/store/sqlite/models/sender-key.js.map +1 -0
- package/dist/local/store/sqlite/models/sesame-user-record.d.ts +32 -0
- package/dist/local/store/sqlite/models/sesame-user-record.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/sesame-user-record.js +78 -0
- package/dist/local/store/sqlite/models/sesame-user-record.js.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/session.d.ts +24 -20
- package/dist/local/store/sqlite/models/session.d.ts.map +1 -0
- package/dist/local/store/{expo → sqlite}/models/session.js +54 -72
- package/dist/local/store/sqlite/models/session.js.map +1 -0
- package/dist/local/store/sqlite/models/skipped-sender-key.d.ts +24 -0
- package/dist/local/store/sqlite/models/skipped-sender-key.d.ts.map +1 -0
- package/dist/local/store/sqlite/models/skipped-sender-key.js +62 -0
- package/dist/local/store/sqlite/models/skipped-sender-key.js.map +1 -0
- package/dist/local/store/sqlite/op-sqlite-driver.d.ts +14 -0
- package/dist/local/store/sqlite/op-sqlite-driver.d.ts.map +1 -0
- package/dist/local/store/sqlite/op-sqlite-driver.js +180 -0
- package/dist/local/store/sqlite/op-sqlite-driver.js.map +1 -0
- package/dist/local/store/sqlite/open-store.d.ts +83 -0
- package/dist/local/store/sqlite/open-store.d.ts.map +1 -0
- package/dist/local/store/sqlite/open-store.js +177 -0
- package/dist/local/store/sqlite/open-store.js.map +1 -0
- package/dist/local/store/sqlite/schema.d.ts +102 -0
- package/dist/local/store/sqlite/schema.d.ts.map +1 -0
- package/dist/local/store/sqlite/schema.js +61 -0
- package/dist/local/store/sqlite/schema.js.map +1 -0
- package/dist/local/store/sqlite/store.d.ts +199 -0
- package/dist/local/store/sqlite/store.d.ts.map +1 -0
- package/dist/local/store/sqlite/store.js +494 -0
- package/dist/local/store/sqlite/store.js.map +1 -0
- package/dist/local/store/sqlite/web/driver.d.ts +50 -0
- package/dist/local/store/sqlite/web/driver.d.ts.map +1 -0
- package/dist/local/store/sqlite/web/driver.js +169 -0
- package/dist/local/store/sqlite/web/driver.js.map +1 -0
- package/dist/local/store/sqlite/web/protocol.d.ts +80 -0
- package/dist/local/store/sqlite/web/protocol.d.ts.map +1 -0
- package/dist/local/store/sqlite/web/protocol.js +13 -0
- package/dist/local/store/sqlite/web/protocol.js.map +1 -0
- package/dist/local/store/sqlite/web/sqlite3mc/sqlite3.mjs +14526 -0
- package/dist/local/store/sqlite/web/sqlite3mc/sqlite3.wasm +0 -0
- package/dist/local/store/sqlite/web/worker.d.ts +36 -0
- package/dist/local/store/sqlite/web/worker.d.ts.map +1 -0
- package/dist/local/store/sqlite/web/worker.js +616 -0
- package/dist/local/store/sqlite/web/worker.js.map +1 -0
- package/dist/local/store/web/adapter.d.ts +5 -14
- package/dist/local/store/web/adapter.d.ts.map +1 -1
- package/dist/local/store/web/adapter.js +119 -106
- package/dist/local/store/web/adapter.js.map +1 -1
- package/dist/local/store/web/device-record.d.ts +23 -0
- package/dist/local/store/web/device-record.d.ts.map +1 -0
- package/dist/local/store/web/device-record.js +31 -0
- package/dist/local/store/web/device-record.js.map +1 -0
- package/dist/local/store/web-sqlite/adapter.d.ts +98 -0
- package/dist/local/store/web-sqlite/adapter.d.ts.map +1 -0
- package/dist/local/store/web-sqlite/adapter.js +151 -0
- package/dist/local/store/web-sqlite/adapter.js.map +1 -0
- package/dist/local/store/web-sqlite/index.d.ts +14 -0
- package/dist/local/store/web-sqlite/index.d.ts.map +1 -0
- package/dist/local/store/web-sqlite/index.js +3 -0
- package/dist/local/store/web-sqlite/index.js.map +1 -0
- package/dist/local/store/web-sqlite/maintenance.d.ts +5 -0
- package/dist/local/store/web-sqlite/maintenance.d.ts.map +1 -0
- package/dist/local/store/web-sqlite/maintenance.js +7 -0
- package/dist/local/store/web-sqlite/maintenance.js.map +1 -0
- package/dist/local/store/web-sqlite/store-locks.d.ts +33 -0
- package/dist/local/store/web-sqlite/store-locks.d.ts.map +1 -0
- package/dist/local/store/web-sqlite/store-locks.js +60 -0
- package/dist/local/store/web-sqlite/store-locks.js.map +1 -0
- package/dist/local/vault/electron-safe-storage.d.ts +45 -0
- package/dist/local/vault/electron-safe-storage.d.ts.map +1 -0
- package/dist/local/vault/electron-safe-storage.js +212 -0
- package/dist/local/vault/electron-safe-storage.js.map +1 -0
- package/dist/local/vault/index.d.ts +4 -2
- package/dist/local/vault/index.d.ts.map +1 -1
- package/dist/local/vault/indexed-db.d.ts +37 -0
- package/dist/local/vault/indexed-db.d.ts.map +1 -0
- package/dist/local/vault/indexed-db.js +102 -0
- package/dist/local/vault/indexed-db.js.map +1 -0
- package/dist/local/vault/react-native-keychain.d.ts +12 -0
- package/dist/local/vault/react-native-keychain.d.ts.map +1 -0
- package/dist/local/vault/react-native-keychain.js +71 -0
- package/dist/local/vault/react-native-keychain.js.map +1 -0
- package/dist/local/vault/require.d.ts +10 -0
- package/dist/local/vault/require.d.ts.map +1 -0
- package/dist/local/vault/require.js +16 -0
- package/dist/local/vault/require.js.map +1 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +2 -1
- package/dist/logger.js.map +1 -1
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +2 -1
- package/dist/media/index.js.map +1 -1
- package/dist/profile/contact-state.d.ts.map +1 -1
- package/dist/profile/contact-state.js +2 -4
- package/dist/profile/contact-state.js.map +1 -1
- package/dist/profile/index.d.ts +0 -1
- package/dist/profile/index.d.ts.map +1 -1
- package/dist/profile/index.js +0 -2
- package/dist/profile/index.js.map +1 -1
- package/dist/profile/profile-key.d.ts +16 -11
- package/dist/profile/profile-key.d.ts.map +1 -1
- package/dist/profile/profile-key.js +29 -29
- package/dist/profile/profile-key.js.map +1 -1
- package/dist/profile/update-service.d.ts +6 -0
- package/dist/profile/update-service.d.ts.map +1 -1
- package/dist/profile/update-service.js +3 -1
- package/dist/profile/update-service.js.map +1 -1
- package/dist/remote/object-store/validation.d.ts.map +1 -1
- package/dist/remote/object-store/validation.js +2 -1
- package/dist/remote/object-store/validation.js.map +1 -1
- package/dist/remote/relay/memory/adapter.d.ts +2 -3
- package/dist/remote/relay/memory/adapter.d.ts.map +1 -1
- package/dist/remote/relay/memory/adapter.js +22 -30
- package/dist/remote/relay/memory/adapter.js.map +1 -1
- package/dist/remote/relay/memory/failures.d.ts.map +1 -1
- package/dist/remote/relay/memory/failures.js +2 -1
- package/dist/remote/relay/memory/failures.js.map +1 -1
- package/dist/remote/relay/memory/group-server.d.ts +5 -1
- package/dist/remote/relay/memory/group-server.d.ts.map +1 -1
- package/dist/remote/relay/memory/group-server.js +11 -1
- package/dist/remote/relay/memory/group-server.js.map +1 -1
- package/dist/safety/url.d.ts.map +1 -1
- package/dist/safety/url.js +21 -26
- package/dist/safety/url.js.map +1 -1
- package/dist/types/api.d.ts +15 -2
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/errors.d.ts +97 -1
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +105 -1
- package/dist/types/errors.js.map +1 -1
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/session.js +2 -1
- package/dist/types/session.js.map +1 -1
- package/dist/utils/retry.d.ts +2 -0
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +4 -2
- package/dist/utils/retry.js.map +1 -1
- package/docs/ASSURANCE.md +81 -8
- package/docs/CLIENT_COMPOSITION.md +1 -2
- package/docs/DEVIATIONS.md +27 -3
- package/docs/ERROR_HANDLING.md +6 -0
- package/docs/GETTING_STARTED.md +1 -2
- package/docs/PACKAGE_SURFACE.md +29 -11
- package/docs/RECIPES.md +1 -2
- package/examples/browser/index.html +1 -1
- package/local/store/README.md +117 -34
- package/local/store/expo/README.md +67 -47
- package/local/store/key-value/README.md +278 -0
- package/local/store/node/README.md +206 -45
- package/local/store/react-native/README.md +121 -52
- package/local/store/web/README.md +9 -0
- package/local/store/web-sqlite/README.md +202 -0
- package/local/vault/README.md +115 -2
- package/package.json +61 -42
- package/profile/README.md +9 -11
- package/remote/README.md +1 -2
- package/dist/local/store/expo/database-key.d.ts +0 -142
- package/dist/local/store/expo/database-key.d.ts.map +0 -1
- package/dist/local/store/expo/database-key.js +0 -312
- package/dist/local/store/expo/database-key.js.map +0 -1
- package/dist/local/store/expo/db.d.ts +0 -95
- package/dist/local/store/expo/db.d.ts.map +0 -1
- package/dist/local/store/expo/db.js +0 -147
- package/dist/local/store/expo/db.js.map +0 -1
- package/dist/local/store/expo/key-storage.d.ts +0 -429
- package/dist/local/store/expo/key-storage.d.ts.map +0 -1
- package/dist/local/store/expo/key-storage.js +0 -1361
- package/dist/local/store/expo/key-storage.js.map +0 -1
- package/dist/local/store/expo/models/ec-one-time-prekey.d.ts.map +0 -1
- package/dist/local/store/expo/models/ec-one-time-prekey.js.map +0 -1
- package/dist/local/store/expo/models/ec-signed-prekey.d.ts.map +0 -1
- package/dist/local/store/expo/models/ec-signed-prekey.js.map +0 -1
- package/dist/local/store/expo/models/identity-key-id.d.ts.map +0 -1
- package/dist/local/store/expo/models/identity-key-id.js.map +0 -1
- package/dist/local/store/expo/models/index.d.ts.map +0 -1
- package/dist/local/store/expo/models/index.js.map +0 -1
- package/dist/local/store/expo/models/kyber-one-time-prekey.d.ts.map +0 -1
- package/dist/local/store/expo/models/kyber-one-time-prekey.js.map +0 -1
- package/dist/local/store/expo/models/kyber-prekey-used.d.ts.map +0 -1
- package/dist/local/store/expo/models/kyber-prekey-used.js.map +0 -1
- package/dist/local/store/expo/models/kyber-prekey.d.ts.map +0 -1
- package/dist/local/store/expo/models/kyber-prekey.js.map +0 -1
- package/dist/local/store/expo/models/local-identity.d.ts +0 -52
- package/dist/local/store/expo/models/local-identity.d.ts.map +0 -1
- package/dist/local/store/expo/models/local-identity.js +0 -166
- package/dist/local/store/expo/models/local-identity.js.map +0 -1
- package/dist/local/store/expo/models/message-record.d.ts.map +0 -1
- package/dist/local/store/expo/models/message-record.js.map +0 -1
- package/dist/local/store/expo/models/recipient-identity.d.ts +0 -21
- package/dist/local/store/expo/models/recipient-identity.d.ts.map +0 -1
- package/dist/local/store/expo/models/recipient-identity.js +0 -66
- package/dist/local/store/expo/models/recipient-identity.js.map +0 -1
- package/dist/local/store/expo/models/replaced-prekey-utils.d.ts +0 -33
- package/dist/local/store/expo/models/replaced-prekey-utils.d.ts.map +0 -1
- package/dist/local/store/expo/models/replaced-prekey-utils.js +0 -54
- package/dist/local/store/expo/models/replaced-prekey-utils.js.map +0 -1
- package/dist/local/store/expo/models/sender-key.d.ts.map +0 -1
- package/dist/local/store/expo/models/sender-key.js.map +0 -1
- package/dist/local/store/expo/models/session.d.ts.map +0 -1
- package/dist/local/store/expo/models/session.js.map +0 -1
- package/dist/local/store/expo/schema.d.ts +0 -1917
- package/dist/local/store/expo/schema.d.ts.map +0 -1
- package/dist/local/store/expo/schema.js +0 -197
- package/dist/local/store/expo/schema.js.map +0 -1
- package/dist/local/store/node/database-encryption.d.ts +0 -109
- package/dist/local/store/node/database-encryption.d.ts.map +0 -1
- package/dist/local/store/node/database-encryption.js +0 -202
- package/dist/local/store/node/database-encryption.js.map +0 -1
- package/dist/local/store/node/database-file-commit.d.ts +0 -8
- package/dist/local/store/node/database-file-commit.d.ts.map +0 -1
- package/dist/local/store/node/database-file-commit.js +0 -62
- package/dist/local/store/node/database-file-commit.js.map +0 -1
- package/dist/local/store/node/database-key.d.ts +0 -129
- package/dist/local/store/node/database-key.d.ts.map +0 -1
- package/dist/local/store/node/database-key.js +0 -293
- package/dist/local/store/node/database-key.js.map +0 -1
- package/dist/local/store/node/database-lock.d.ts +0 -8
- package/dist/local/store/node/database-lock.d.ts.map +0 -1
- package/dist/local/store/node/database-lock.js +0 -44
- package/dist/local/store/node/database-lock.js.map +0 -1
- package/dist/local/store/node/database.d.ts +0 -226
- package/dist/local/store/node/database.d.ts.map +0 -1
- package/dist/local/store/node/database.js +0 -813
- package/dist/local/store/node/database.js.map +0 -1
- package/dist/local/store/react-native/backend-conformance.d.ts.map +0 -1
- package/dist/local/store/react-native/backend-conformance.js.map +0 -1
- package/dist/local/store/react-native/reference-backend.d.ts +0 -43
- package/dist/local/store/react-native/reference-backend.d.ts.map +0 -1
- package/dist/local/store/react-native/reference-backend.js +0 -178
- package/dist/local/store/react-native/reference-backend.js.map +0 -1
- package/dist/local/store/react-native/storage.d.ts.map +0 -1
- package/dist/local/store/react-native/storage.js.map +0 -1
- package/dist/profile/storage.d.ts +0 -61
- package/dist/profile/storage.d.ts.map +0 -1
- package/dist/profile/storage.js +0 -145
- package/dist/profile/storage.js.map +0 -1
- /package/dist/local/store/{react-native → key-value}/storage.js +0 -0
- /package/dist/local/store/{expo → sqlite}/models/identity-key-id.d.ts +0 -0
- /package/dist/local/store/{expo → sqlite}/models/identity-key-id.js +0 -0
|
@@ -0,0 +1,1472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key-Value Storage Adapter (Bare Workflow)
|
|
3
|
+
*
|
|
4
|
+
* Storage adapter for React Native applications without Expo.
|
|
5
|
+
* Uses:
|
|
6
|
+
* - a caller-provided persistent key-value backend
|
|
7
|
+
* - a caller-provided secret vault that holds the 32-byte value key
|
|
8
|
+
* - the SDK secure random seam, which reads the global crypto.getRandomValues.
|
|
9
|
+
* The app installs react-native-get-random-values 2.x or
|
|
10
|
+
* react-native-quick-crypto and loads it before the first SDK call.
|
|
11
|
+
* - Signal Protocol crypto module for AES-256-GCM encryption. It uses Web Crypto
|
|
12
|
+
* when `crypto.subtle` exists and the bundled Noble AES otherwise.
|
|
13
|
+
*
|
|
14
|
+
* Security Considerations:
|
|
15
|
+
* - All sensitive data encrypted with AES-256-GCM (128-bit auth tag)
|
|
16
|
+
* - The value key lives only in the secret vault, never in the key-value backend
|
|
17
|
+
* - Data without its key is an error (`LOCAL_STORE_KEY_LOST`), never a silent reset
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const storage = await KeyValueSignalProtocolStore.create({
|
|
22
|
+
* storage: yourPersistentKeyValueStorage,
|
|
23
|
+
* vault: yourPlatformSecretVault,
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Store identity key
|
|
27
|
+
* await storage.storeIdentityKey(keyPair);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import { utf8Decode } from '../../../internal/platform/index.js';
|
|
31
|
+
import * as SignalProtocolCrypto from '../../../internal/crypto/index.js';
|
|
32
|
+
import { MAX_UNACKNOWLEDGED_SESSION_AGE_MS } from '../../../types/protocol-config.js';
|
|
33
|
+
import { assertCurrentSessionRecord } from '../../../types/session.js';
|
|
34
|
+
import { acceptContactIdentityRotation as acceptRotation, createUnverifiedContactIdentityRecord, evaluateContactIdentityCandidate, validateContactIdentityRecord, verifyContactIdentityRecord, } from '../../../keys/identity.js';
|
|
35
|
+
import { ProtocolAddress } from '../../../types/address.js';
|
|
36
|
+
import { IdentityKeyChange, StorageQuotaExceededError } from '../../../types/index.js';
|
|
37
|
+
import { requireSecretVault } from '../../vault/require.js';
|
|
38
|
+
import { asBase64 } from '../../../types/utils.js';
|
|
39
|
+
import { withSession } from '../device-record.js';
|
|
40
|
+
import { decodeUserRecord, encodeUserRecord } from './device-record.js';
|
|
41
|
+
import { deleteDataThenReplaceValueKey, openValueKey } from './value-key.js';
|
|
42
|
+
import { deserializeSessionRecord, serializeSessionRecord } from '../session-codec.js';
|
|
43
|
+
import { parseReceivedContent } from '../received-content.js';
|
|
44
|
+
import { createKyberPreKeyState, createKyberPreKeyInstanceId, getCurrentKyberPreKey, getRetainedKyberPreKey, recordKyberPreKeyUse, recordKyberPreKeyUseById, retainKyberPreKey, } from '../kyber-prekey-lifecycle.js';
|
|
45
|
+
const STORAGE_KEYS = {
|
|
46
|
+
IDENTITY_KEY_PREFIX: '@signal:identity:keyPair:',
|
|
47
|
+
REGISTRATION_ID_PREFIX: '@signal:metadata:registrationId:',
|
|
48
|
+
METADATA_PREFIX: '@signal:metadata:value:',
|
|
49
|
+
CONTACT_PREFIX: '@signal:contacts:',
|
|
50
|
+
CONTACT_V1_PREFIX: '@signal:contacts:v1:',
|
|
51
|
+
PREKEY_PREFIX: '@signal:prekeys:',
|
|
52
|
+
SESSION_PREFIX: '@signal:sessions:',
|
|
53
|
+
SESAME_USER_PREFIX: '@signal:sesame:user:',
|
|
54
|
+
SENDER_KEY_RECORD_PREFIX: '@signal:sender-key-record:',
|
|
55
|
+
// Maps an opaque `senderKeyId` to the record that holds it. A received group
|
|
56
|
+
// message names its sender key by that id and nothing else. This backend is
|
|
57
|
+
// a flat key-value store with no secondary indexes, so the reverse lookup
|
|
58
|
+
// has to be a stored pointer. The pointer value is a record key, not key
|
|
59
|
+
// material. The ids in it already travel unencrypted on the wire.
|
|
60
|
+
//
|
|
61
|
+
// The pointer is scoped by sender, not by id alone. A sender chooses its own
|
|
62
|
+
// id and announces it in a distribution message. Two senders can therefore
|
|
63
|
+
// name the same one, by accident or deliberately, after reading a shared
|
|
64
|
+
// group's distribution. A single global slot per id would let the second
|
|
65
|
+
// writer silently take over the first's pointer. Every subsequent message
|
|
66
|
+
// from the first sender would then fail to resolve.
|
|
67
|
+
// See `getSenderKeyIdPointerKey`.
|
|
68
|
+
SENDER_KEY_ID_INDEX_PREFIX: '@signal:sender-key-id:',
|
|
69
|
+
SKIPPED_SENDER_KEY_PREFIX: '@signal:skipped-sender-key:',
|
|
70
|
+
MESSAGE_RECORD_PREFIX: '@signal:message-record:',
|
|
71
|
+
SECURITY_EVENTS: '@signal:security-events',
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Escape a value so it cannot contain the `:` that joins composite keys.
|
|
75
|
+
*
|
|
76
|
+
* Composite keys in this backend are `:`-joined strings. The components are
|
|
77
|
+
* identifiers the adapter does not get to choose. They are group IDs, sender
|
|
78
|
+
* key IDs read straight off the wire, and user IDs handed over by the host
|
|
79
|
+
* application's identify hook.
|
|
80
|
+
*
|
|
81
|
+
* None of them are validated, and a tenant-scoped `tenant:alice` is an
|
|
82
|
+
* ordinary thing for a host to produce. So "components contain no `:`" is an
|
|
83
|
+
* assumption rather than a fact, and the encoding has to make it one.
|
|
84
|
+
*
|
|
85
|
+
* `%` is escaped first so the escape character itself round-trips: without it
|
|
86
|
+
* a literal `%3A` in an identifier would unescape into a separator. Values
|
|
87
|
+
* containing neither character escape to themselves, so an existing
|
|
88
|
+
* deployment whose IDs are already well behaved keeps the keys it has.
|
|
89
|
+
*/
|
|
90
|
+
function escapeKeyComponent(value) {
|
|
91
|
+
return value.replace(/%/g, '%25').replace(/:/g, '%3A');
|
|
92
|
+
}
|
|
93
|
+
/** Inverse of {@link escapeKeyComponent}. It unescapes in the opposite order. */
|
|
94
|
+
function unescapeKeyComponent(value) {
|
|
95
|
+
return value.replace(/%3A/g, ':').replace(/%25/g, '%');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A write the injected backend rejects for want of space fails with an error
|
|
99
|
+
* named `QuotaExceededError`. Checked by name rather than instanceof, as in
|
|
100
|
+
* the web adapter. The backend is application-supplied, so no error class is
|
|
101
|
+
* shared with it, and the name is the documented signal.
|
|
102
|
+
*/
|
|
103
|
+
function isQuotaExceededError(error) {
|
|
104
|
+
return (typeof error === 'object' &&
|
|
105
|
+
error !== null &&
|
|
106
|
+
error.name === 'QuotaExceededError');
|
|
107
|
+
}
|
|
108
|
+
function mapQuotaFailure(operation, error) {
|
|
109
|
+
if (!isQuotaExceededError(error))
|
|
110
|
+
return error;
|
|
111
|
+
// The failure is Error-shaped by the name check above, but not always
|
|
112
|
+
// instanceof Error: a backend may surface a platform exception object.
|
|
113
|
+
return new StorageQuotaExceededError(operation, error);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Rebinds every method of the store so a QuotaExceededError from the
|
|
117
|
+
* injected backend crosses the adapter boundary as the typed
|
|
118
|
+
* StorageQuotaExceededError. Quota exhaustion can surface from any setItem
|
|
119
|
+
* or atomicWrite the backend runs. The mapping therefore lives at this one
|
|
120
|
+
* seam, instead of inside each of the adapter's write paths. A method added
|
|
121
|
+
* later is covered without a wrapper of its own. The backend contract
|
|
122
|
+
* commits nothing from a rejected write, so the typed error always means
|
|
123
|
+
* the write did not persist.
|
|
124
|
+
*
|
|
125
|
+
* Wrapping runs inside the private constructor, and `create` and `reset` are
|
|
126
|
+
* the only construction paths, so every caller-visible instance is wrapped
|
|
127
|
+
* before any method can run.
|
|
128
|
+
*/
|
|
129
|
+
function mapQuotaFailuresAtBoundary(store) {
|
|
130
|
+
const methods = new Map();
|
|
131
|
+
for (let prototype = Object.getPrototypeOf(store); prototype !== null && prototype !== Object.prototype; prototype = Object.getPrototypeOf(prototype)) {
|
|
132
|
+
for (const name of Object.getOwnPropertyNames(prototype)) {
|
|
133
|
+
// First found wins: the walk goes most-derived first.
|
|
134
|
+
if (name === 'constructor' || methods.has(name))
|
|
135
|
+
continue;
|
|
136
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
|
137
|
+
if (typeof descriptor?.value !== 'function')
|
|
138
|
+
continue;
|
|
139
|
+
methods.set(name, descriptor.value);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const [name, method] of methods) {
|
|
143
|
+
store[name] = function mapped(...args) {
|
|
144
|
+
let result;
|
|
145
|
+
try {
|
|
146
|
+
result = method.apply(this, args);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw mapQuotaFailure(name, error);
|
|
150
|
+
}
|
|
151
|
+
// Duck-typed rather than instanceof Promise so a cross-realm
|
|
152
|
+
// promise or plain thenable from an override maps too.
|
|
153
|
+
if (result !== null &&
|
|
154
|
+
typeof result === 'object' &&
|
|
155
|
+
typeof result.then === 'function') {
|
|
156
|
+
return Promise.resolve(result).catch((error) => {
|
|
157
|
+
throw mapQuotaFailure(name, error);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Key-value storage adapter implementation
|
|
166
|
+
*
|
|
167
|
+
* Provides secure storage for Signal Protocol keys using an injected
|
|
168
|
+
* application-supplied key-value backend, an injected secret vault, and the SDK
|
|
169
|
+
* secure random seam.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const storage = await KeyValueSignalProtocolStore.create({
|
|
174
|
+
* storage: yourPersistentKeyValueStorage,
|
|
175
|
+
* vault: yourPlatformSecretVault,
|
|
176
|
+
* });
|
|
177
|
+
*
|
|
178
|
+
* // Store identity key
|
|
179
|
+
* await storage.storeIdentityKey(keyPair);
|
|
180
|
+
*
|
|
181
|
+
* // Get identity key
|
|
182
|
+
* const keyPair = await storage.getIdentityKey();
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export class KeyValueSignalProtocolStore {
|
|
186
|
+
databaseKey = null;
|
|
187
|
+
initialized = false;
|
|
188
|
+
storageBackend;
|
|
189
|
+
vault;
|
|
190
|
+
constructor(options, operation) {
|
|
191
|
+
this.storageBackend = options.storage;
|
|
192
|
+
this.vault = requireSecretVault(options.vault, operation);
|
|
193
|
+
mapQuotaFailuresAtBoundary(this);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Open the store. On first use it creates the value key in the vault.
|
|
197
|
+
*
|
|
198
|
+
* @throws EncryptionError with `LOCAL_STORE_KEY_LOST` when the backend
|
|
199
|
+
* holds store data and the vault holds no key. Restore the vault entry,
|
|
200
|
+
* or call {@link KeyValueSignalProtocolStore.reset}.
|
|
201
|
+
* @throws EncryptionError with `KEY_STORAGE_ERROR` when the vault fails or
|
|
202
|
+
* holds a key of the wrong size.
|
|
203
|
+
* @throws EncryptionError with `SECRET_VAULT_REQUIRED` when `vault` is absent.
|
|
204
|
+
*/
|
|
205
|
+
static async create(options) {
|
|
206
|
+
const store = new KeyValueSignalProtocolStore(options, 'KeyValueSignalProtocolStore.create');
|
|
207
|
+
await store.initialize();
|
|
208
|
+
return store;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Delete every store record, then create a new value key, and return the
|
|
212
|
+
* open, empty store. This is the only way to open a store whose key is lost.
|
|
213
|
+
*
|
|
214
|
+
* The deletion is one `atomicWrite`, so it commits all or nothing. When the
|
|
215
|
+
* process stops after the deletion and before the vault write, the backend
|
|
216
|
+
* holds no store data and the vault holds the old key or none. The next
|
|
217
|
+
* {@link KeyValueSignalProtocolStore.create} then opens an empty store
|
|
218
|
+
* and never reports a lost key.
|
|
219
|
+
*/
|
|
220
|
+
static async reset(options) {
|
|
221
|
+
const store = new KeyValueSignalProtocolStore(options, 'KeyValueSignalProtocolStore.reset');
|
|
222
|
+
await store.resetData();
|
|
223
|
+
return store;
|
|
224
|
+
}
|
|
225
|
+
// ============================================================================
|
|
226
|
+
// Initialization
|
|
227
|
+
// ============================================================================
|
|
228
|
+
/** Load the value key, or create it for an empty backend. See `openValueKey`. */
|
|
229
|
+
async initialize() {
|
|
230
|
+
if (this.initialized) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.databaseKey = await openValueKey(this.storageBackend, this.vault);
|
|
234
|
+
this.initialized = true;
|
|
235
|
+
}
|
|
236
|
+
/** Delete the store data, then replace the value key. See `deleteDataThenReplaceValueKey`. */
|
|
237
|
+
async resetData() {
|
|
238
|
+
this.databaseKey = await deleteDataThenReplaceValueKey(this.storageBackend, this.vault);
|
|
239
|
+
this.initialized = true;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Get the database encryption key
|
|
243
|
+
*
|
|
244
|
+
* @returns 32-byte AES-256 key
|
|
245
|
+
* @throws Error if not initialized
|
|
246
|
+
*/
|
|
247
|
+
async getDatabaseKey() {
|
|
248
|
+
this.ensureInitialized();
|
|
249
|
+
return this.databaseKey;
|
|
250
|
+
}
|
|
251
|
+
// ============================================================================
|
|
252
|
+
// Identity Key Management - Own Keys
|
|
253
|
+
// ============================================================================
|
|
254
|
+
async storeIdentityKey(keyPair, identityType) {
|
|
255
|
+
this.ensureInitialized();
|
|
256
|
+
const encrypted = await this.encrypt(JSON.stringify(keyPair));
|
|
257
|
+
await this.storageBackend.setItem(this.getIdentityKeyStorageKey(identityType), encrypted);
|
|
258
|
+
}
|
|
259
|
+
async getIdentityKey(identityType) {
|
|
260
|
+
this.ensureInitialized();
|
|
261
|
+
const encrypted = await this.storageBackend.getItem(this.getIdentityKeyStorageKey(identityType));
|
|
262
|
+
if (!encrypted)
|
|
263
|
+
return null;
|
|
264
|
+
const decrypted = await this.decrypt(encrypted);
|
|
265
|
+
return JSON.parse(decrypted);
|
|
266
|
+
}
|
|
267
|
+
async hasIdentityKey(identityType) {
|
|
268
|
+
this.ensureInitialized();
|
|
269
|
+
const key = await this.storageBackend.getItem(this.getIdentityKeyStorageKey(identityType));
|
|
270
|
+
return key !== null;
|
|
271
|
+
}
|
|
272
|
+
async getLocalRegistrationId(identityType) {
|
|
273
|
+
this.ensureInitialized();
|
|
274
|
+
const id = await this.storageBackend.getItem(this.getRegistrationIdStorageKey(identityType));
|
|
275
|
+
if (!id)
|
|
276
|
+
return 0;
|
|
277
|
+
return parseInt(id, 10);
|
|
278
|
+
}
|
|
279
|
+
async setLocalRegistrationId(id, identityType) {
|
|
280
|
+
this.ensureInitialized();
|
|
281
|
+
await this.storageBackend.setItem(this.getRegistrationIdStorageKey(identityType), id.toString());
|
|
282
|
+
}
|
|
283
|
+
// ============================================================================
|
|
284
|
+
// Identity Verification - Contact Keys (TOFU)
|
|
285
|
+
// ============================================================================
|
|
286
|
+
async saveContactIdentity(address, identity, identityType, suppliedCommitment) {
|
|
287
|
+
this.ensureInitialized();
|
|
288
|
+
const storageKey = this.getContactStorageKey(address, identityType);
|
|
289
|
+
const existing = await this.readContactRecord(address, identityType);
|
|
290
|
+
const status = evaluateContactIdentityCandidate(existing, identity, suppliedCommitment);
|
|
291
|
+
if (status === 'NEW') {
|
|
292
|
+
await this.writeContactRecord(storageKey, createUnverifiedContactIdentityRecord(identity, Date.now()));
|
|
293
|
+
return IdentityKeyChange.NEW_IDENTITY;
|
|
294
|
+
}
|
|
295
|
+
if (status === 'MATCH')
|
|
296
|
+
return IdentityKeyChange.UNCHANGED;
|
|
297
|
+
if (status === 'ROLLBACK')
|
|
298
|
+
return IdentityKeyChange.ROLLBACK;
|
|
299
|
+
return IdentityKeyChange.CHANGED;
|
|
300
|
+
}
|
|
301
|
+
async getContactIdentity(address, identityType) {
|
|
302
|
+
this.ensureInitialized();
|
|
303
|
+
return await this.readContactRecord(address, identityType);
|
|
304
|
+
}
|
|
305
|
+
async acceptContactIdentityRotation(address, identity, identityType, suppliedCommitment) {
|
|
306
|
+
return await this.acceptContactIdentityRotationAndDeleteSessions(address, identity, identityType, suppliedCommitment);
|
|
307
|
+
}
|
|
308
|
+
async acceptContactIdentityRotationAndDeleteSessions(address, identity, identityType, suppliedCommitment) {
|
|
309
|
+
this.ensureInitialized();
|
|
310
|
+
const contactKey = this.getContactStorageKey(address, identityType);
|
|
311
|
+
const existingValue = await this.storageBackend.getItem(contactKey);
|
|
312
|
+
if (!existingValue)
|
|
313
|
+
throw new Error('Cannot rotate an unseen identity');
|
|
314
|
+
const existing = JSON.parse(await this.decrypt(existingValue));
|
|
315
|
+
validateContactIdentityRecord(existing);
|
|
316
|
+
const replacement = acceptRotation(existing, identity, Date.now(), suppliedCommitment);
|
|
317
|
+
validateContactIdentityRecord(replacement);
|
|
318
|
+
await this.storageBackend.atomicWrite([
|
|
319
|
+
{ type: 'check', key: contactKey, expectedValue: existingValue },
|
|
320
|
+
{ type: 'set', key: contactKey, value: await this.encrypt(JSON.stringify(replacement)) },
|
|
321
|
+
{
|
|
322
|
+
type: 'removeSessionsForUser',
|
|
323
|
+
keyPrefix: STORAGE_KEYS.SESSION_PREFIX,
|
|
324
|
+
userId: address.userId,
|
|
325
|
+
},
|
|
326
|
+
]);
|
|
327
|
+
return replacement;
|
|
328
|
+
}
|
|
329
|
+
async verifyContactIdentity(address, identity, identityType, suppliedCommitment) {
|
|
330
|
+
this.ensureInitialized();
|
|
331
|
+
const contactKey = this.getContactStorageKey(address, identityType);
|
|
332
|
+
const existingValue = await this.storageBackend.getItem(contactKey);
|
|
333
|
+
const existing = existingValue
|
|
334
|
+
? JSON.parse(await this.decrypt(existingValue))
|
|
335
|
+
: null;
|
|
336
|
+
if (!existing)
|
|
337
|
+
throw new Error('Cannot verify an unseen identity');
|
|
338
|
+
validateContactIdentityRecord(existing);
|
|
339
|
+
const verified = verifyContactIdentityRecord(existing, identity, Date.now(), suppliedCommitment);
|
|
340
|
+
await this.storageBackend.atomicWrite([
|
|
341
|
+
{ type: 'check', key: contactKey, expectedValue: existingValue },
|
|
342
|
+
{ type: 'set', key: contactKey, value: await this.encrypt(JSON.stringify(verified)) },
|
|
343
|
+
]);
|
|
344
|
+
return verified;
|
|
345
|
+
}
|
|
346
|
+
async isTrustedIdentity(address, identity, _direction, identityType) {
|
|
347
|
+
this.ensureInitialized();
|
|
348
|
+
const status = evaluateContactIdentityCandidate(await this.readContactRecord(address, identityType), identity);
|
|
349
|
+
return status === 'NEW' || status === 'MATCH';
|
|
350
|
+
}
|
|
351
|
+
// ============================================================================
|
|
352
|
+
// PreKey Management
|
|
353
|
+
// ============================================================================
|
|
354
|
+
async storeEcSignedPreKey(signedPreKey, identityType) {
|
|
355
|
+
this.ensureInitialized();
|
|
356
|
+
// Get existing prekeys and add/update the new one
|
|
357
|
+
const allPrekeys = await this.getAllEcSignedPreKeys(identityType);
|
|
358
|
+
const existingIndex = allPrekeys.findIndex((k) => k.keyId === signedPreKey.keyId);
|
|
359
|
+
if (existingIndex >= 0) {
|
|
360
|
+
allPrekeys[existingIndex] = signedPreKey;
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
allPrekeys.push(signedPreKey);
|
|
364
|
+
}
|
|
365
|
+
// Cleanup expired prekeys
|
|
366
|
+
const cutoff = Date.now() - MAX_UNACKNOWLEDGED_SESSION_AGE_MS;
|
|
367
|
+
const newest = allPrekeys.reduce((a, b) => (a.keyId > b.keyId ? a : b));
|
|
368
|
+
const filtered = allPrekeys.filter((k) => k.keyId === newest.keyId || (k.timestamp && k.timestamp > cutoff));
|
|
369
|
+
const encrypted = await this.encrypt(JSON.stringify(filtered));
|
|
370
|
+
await this.storageBackend.setItem(this.getSignedPreKeyStorageKey(identityType), encrypted);
|
|
371
|
+
}
|
|
372
|
+
async getEcSignedPreKey(keyId, identityType) {
|
|
373
|
+
this.ensureInitialized();
|
|
374
|
+
const allPrekeys = await this.getAllEcSignedPreKeys(identityType);
|
|
375
|
+
if (allPrekeys.length === 0)
|
|
376
|
+
return null;
|
|
377
|
+
if (keyId !== undefined) {
|
|
378
|
+
// Look up by specific keyId
|
|
379
|
+
return allPrekeys.find((k) => k.keyId === keyId) ?? null;
|
|
380
|
+
}
|
|
381
|
+
// Return the most recent (highest keyId)
|
|
382
|
+
return allPrekeys.reduce((latest, current) => current.keyId > latest.keyId ? current : latest);
|
|
383
|
+
}
|
|
384
|
+
async getAllEcSignedPreKeys(identityType) {
|
|
385
|
+
this.ensureInitialized();
|
|
386
|
+
const encrypted = await this.storageBackend.getItem(this.getSignedPreKeyStorageKey(identityType));
|
|
387
|
+
if (!encrypted) {
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
const decrypted = await this.decrypt(encrypted);
|
|
391
|
+
return JSON.parse(decrypted);
|
|
392
|
+
}
|
|
393
|
+
async removeEcSignedPreKey(keyId, identityType) {
|
|
394
|
+
this.ensureInitialized();
|
|
395
|
+
const allPrekeys = await this.getAllEcSignedPreKeys(identityType);
|
|
396
|
+
const filtered = allPrekeys.filter((k) => k.keyId !== keyId);
|
|
397
|
+
const encrypted = await this.encrypt(JSON.stringify(filtered));
|
|
398
|
+
await this.storageBackend.setItem(this.getSignedPreKeyStorageKey(identityType), encrypted);
|
|
399
|
+
}
|
|
400
|
+
async storeEcOneTimePreKeys(prekeys, identityType) {
|
|
401
|
+
this.ensureInitialized();
|
|
402
|
+
const existing = await this.getEcOneTimePreKeys(identityType);
|
|
403
|
+
await this.storageBackend.setItem(this.getEcOneTimePreKeyStorageKey(identityType), await this.encrypt(JSON.stringify([...existing, ...prekeys])));
|
|
404
|
+
}
|
|
405
|
+
async getEcOneTimePreKeys(identityType) {
|
|
406
|
+
this.ensureInitialized();
|
|
407
|
+
const encrypted = await this.storageBackend.getItem(this.getEcOneTimePreKeyStorageKey(identityType));
|
|
408
|
+
if (!encrypted)
|
|
409
|
+
return [];
|
|
410
|
+
const decrypted = await this.decrypt(encrypted);
|
|
411
|
+
return JSON.parse(decrypted);
|
|
412
|
+
}
|
|
413
|
+
async removeEcOneTimePreKey(preKeyId, identityType) {
|
|
414
|
+
this.ensureInitialized();
|
|
415
|
+
const prekeys = await this.getEcOneTimePreKeys(identityType);
|
|
416
|
+
const filtered = prekeys.filter((pk) => pk.keyId !== preKeyId);
|
|
417
|
+
await this.storageBackend.setItem(this.getEcOneTimePreKeyStorageKey(identityType), await this.encrypt(JSON.stringify(filtered)));
|
|
418
|
+
}
|
|
419
|
+
async storeKyberPreKey(kyberPreKey, identityType) {
|
|
420
|
+
this.ensureInitialized();
|
|
421
|
+
const it = identityType ?? 'aci';
|
|
422
|
+
const storageKey = this.getKyberPreKeyStorageKey(it);
|
|
423
|
+
const stored = await this.storageBackend.getItem(storageKey);
|
|
424
|
+
const state = stored
|
|
425
|
+
? JSON.parse(await this.decrypt(stored))
|
|
426
|
+
: createKyberPreKeyState();
|
|
427
|
+
const instanceId = await createKyberPreKeyInstanceId();
|
|
428
|
+
if (!retainKyberPreKey(state, kyberPreKey, it, Date.now(), instanceId))
|
|
429
|
+
return;
|
|
430
|
+
await this.storageBackend.atomicWrite([
|
|
431
|
+
{ type: 'check', key: storageKey, expectedValue: stored },
|
|
432
|
+
{ type: 'set', key: storageKey, value: await this.encrypt(JSON.stringify(state)) },
|
|
433
|
+
]);
|
|
434
|
+
}
|
|
435
|
+
async getKyberPreKey(identityType) {
|
|
436
|
+
this.ensureInitialized();
|
|
437
|
+
const encrypted = await this.storageBackend.getItem(this.getKyberPreKeyStorageKey(identityType));
|
|
438
|
+
if (!encrypted)
|
|
439
|
+
return null;
|
|
440
|
+
const decrypted = await this.decrypt(encrypted);
|
|
441
|
+
return getCurrentKyberPreKey(JSON.parse(decrypted));
|
|
442
|
+
}
|
|
443
|
+
async getKyberPreKeyById(keyId, identityType) {
|
|
444
|
+
this.ensureInitialized();
|
|
445
|
+
const encrypted = await this.storageBackend.getItem(this.getKyberPreKeyStorageKey(identityType));
|
|
446
|
+
if (!encrypted)
|
|
447
|
+
return null;
|
|
448
|
+
return getRetainedKyberPreKey(JSON.parse(await this.decrypt(encrypted)), keyId);
|
|
449
|
+
}
|
|
450
|
+
async markKyberPreKeyUsed(kyberPreKeyId, signedPreKeyId, baseKeyBytes, identityType) {
|
|
451
|
+
this.ensureInitialized();
|
|
452
|
+
const it = identityType ?? 'aci';
|
|
453
|
+
const storageKey = this.getKyberPreKeyStorageKey(it);
|
|
454
|
+
const stored = await this.storageBackend.getItem(storageKey);
|
|
455
|
+
const state = stored
|
|
456
|
+
? JSON.parse(await this.decrypt(stored))
|
|
457
|
+
: null;
|
|
458
|
+
recordKyberPreKeyUseById(state, it, { kyberPreKeyId, signedPreKeyId, baseKeyBytes });
|
|
459
|
+
await this.storageBackend.atomicWrite([
|
|
460
|
+
{ type: 'check', key: storageKey, expectedValue: stored },
|
|
461
|
+
{ type: 'set', key: storageKey, value: await this.encrypt(JSON.stringify(state)) },
|
|
462
|
+
]);
|
|
463
|
+
}
|
|
464
|
+
// ============================================================================
|
|
465
|
+
// KEM One-Time PreKey Management (Per-Session Post-Quantum Forward Secrecy)
|
|
466
|
+
// ============================================================================
|
|
467
|
+
// NOTE: This adapter runs over an application-supplied key-value engine.
|
|
468
|
+
// Expo and bare React Native apps should prefer the SDK-owned SQLite stores.
|
|
469
|
+
async storeKemOneTimePreKeys(prekeys, identityType) {
|
|
470
|
+
this.ensureInitialized();
|
|
471
|
+
const existing = await this.getKemOneTimePreKeys(identityType);
|
|
472
|
+
await this.storageBackend.setItem(this.getKemOneTimePreKeyStorageKey(identityType), await this.encrypt(JSON.stringify([...existing, ...prekeys])));
|
|
473
|
+
}
|
|
474
|
+
async getKemOneTimePreKeys(identityType) {
|
|
475
|
+
this.ensureInitialized();
|
|
476
|
+
const encrypted = await this.storageBackend.getItem(this.getKemOneTimePreKeyStorageKey(identityType));
|
|
477
|
+
if (!encrypted)
|
|
478
|
+
return [];
|
|
479
|
+
const decrypted = await this.decrypt(encrypted);
|
|
480
|
+
return JSON.parse(decrypted);
|
|
481
|
+
}
|
|
482
|
+
async getKemOneTimePreKey(keyId, identityType) {
|
|
483
|
+
const prekeys = await this.getKemOneTimePreKeys(identityType);
|
|
484
|
+
return prekeys.find((prekey) => prekey.keyId === keyId) ?? null;
|
|
485
|
+
}
|
|
486
|
+
async removeKemOneTimePreKey(keyId, identityType) {
|
|
487
|
+
this.ensureInitialized();
|
|
488
|
+
const prekeys = await this.getKemOneTimePreKeys(identityType);
|
|
489
|
+
const filtered = prekeys.filter((prekey) => prekey.keyId !== keyId);
|
|
490
|
+
await this.storageBackend.setItem(this.getKemOneTimePreKeyStorageKey(identityType), await this.encrypt(JSON.stringify(filtered)));
|
|
491
|
+
}
|
|
492
|
+
async getKemOneTimePreKeyCount(identityType) {
|
|
493
|
+
const prekeys = await this.getKemOneTimePreKeys(identityType);
|
|
494
|
+
return prekeys.length;
|
|
495
|
+
}
|
|
496
|
+
// ============================================================================
|
|
497
|
+
// Session Management - Modern API
|
|
498
|
+
// ============================================================================
|
|
499
|
+
async storeSessionRecord(address, record) {
|
|
500
|
+
this.ensureInitialized();
|
|
501
|
+
assertCurrentSessionRecord(record);
|
|
502
|
+
const addressKey = this.serializeAddress(address);
|
|
503
|
+
const storageKey = `${STORAGE_KEYS.SESSION_PREFIX}${addressKey}`;
|
|
504
|
+
const encrypted = await this.encrypt(serializeSessionRecord(record));
|
|
505
|
+
const storedRecord = {
|
|
506
|
+
userId: address.userId,
|
|
507
|
+
deviceId: address.deviceId,
|
|
508
|
+
data: encrypted,
|
|
509
|
+
updatedAt: Date.now(),
|
|
510
|
+
};
|
|
511
|
+
await this.storageBackend.setItem(storageKey, JSON.stringify(storedRecord));
|
|
512
|
+
}
|
|
513
|
+
async getSessionRecord(address) {
|
|
514
|
+
this.ensureInitialized();
|
|
515
|
+
const addressKey = this.serializeAddress(address);
|
|
516
|
+
const storageKey = `${STORAGE_KEYS.SESSION_PREFIX}${addressKey}`;
|
|
517
|
+
const storedJson = await this.storageBackend.getItem(storageKey);
|
|
518
|
+
if (!storedJson)
|
|
519
|
+
return null;
|
|
520
|
+
const stored = JSON.parse(storedJson);
|
|
521
|
+
const decrypted = await this.decrypt(stored.data);
|
|
522
|
+
try {
|
|
523
|
+
return deserializeSessionRecord(decrypted);
|
|
524
|
+
}
|
|
525
|
+
catch {
|
|
526
|
+
await this.storageBackend.removeItem(storageKey);
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
async commitSessionTrust(commit) {
|
|
531
|
+
this.ensureInitialized();
|
|
532
|
+
assertCurrentSessionRecord(commit.record);
|
|
533
|
+
const addressKey = this.serializeAddress(commit.address);
|
|
534
|
+
const contactStorageKey = this.getContactStorageKey(commit.address, commit.contactIdentityType);
|
|
535
|
+
const existingContactValue = await this.storageBackend.getItem(contactStorageKey);
|
|
536
|
+
const existingContact = existingContactValue
|
|
537
|
+
? JSON.parse(await this.decrypt(existingContactValue))
|
|
538
|
+
: null;
|
|
539
|
+
if (existingContact)
|
|
540
|
+
validateContactIdentityRecord(existingContact);
|
|
541
|
+
const contactStatus = evaluateContactIdentityCandidate(existingContact, commit.contactIdentity);
|
|
542
|
+
if (contactStatus !== 'NEW' && contactStatus !== 'MATCH') {
|
|
543
|
+
throw new Error(`Atomic session/trust commit rejected contact identity status ${contactStatus}`);
|
|
544
|
+
}
|
|
545
|
+
const sessionStorageKey = `${STORAGE_KEYS.SESSION_PREFIX}${addressKey}`;
|
|
546
|
+
const sessionValue = JSON.stringify({
|
|
547
|
+
userId: commit.address.userId,
|
|
548
|
+
deviceId: commit.address.deviceId,
|
|
549
|
+
data: await this.encrypt(serializeSessionRecord(commit.record)),
|
|
550
|
+
updatedAt: Date.now(),
|
|
551
|
+
});
|
|
552
|
+
const ecStorageKey = this.getEcOneTimePreKeyStorageKey(commit.localIdentityType);
|
|
553
|
+
const kemStorageKey = this.getKemOneTimePreKeyStorageKey(commit.localIdentityType);
|
|
554
|
+
const ecPrekeyValue = commit.oneTimePreKeyId === undefined ? null : await this.storageBackend.getItem(ecStorageKey);
|
|
555
|
+
const kemPrekeyValue = commit.kemOneTimePreKeyId === undefined
|
|
556
|
+
? null
|
|
557
|
+
: await this.storageBackend.getItem(kemStorageKey);
|
|
558
|
+
const kyberStorageKey = this.getKyberPreKeyStorageKey(commit.localIdentityType);
|
|
559
|
+
const kyberStateValue = commit.kyberPreKeyUse
|
|
560
|
+
? await this.storageBackend.getItem(kyberStorageKey)
|
|
561
|
+
: null;
|
|
562
|
+
const ecPrekeys = ecPrekeyValue
|
|
563
|
+
? JSON.parse(await this.decrypt(ecPrekeyValue))
|
|
564
|
+
: [];
|
|
565
|
+
const kemPrekeys = kemPrekeyValue
|
|
566
|
+
? JSON.parse(await this.decrypt(kemPrekeyValue))
|
|
567
|
+
: [];
|
|
568
|
+
if (commit.oneTimePreKeyId !== undefined &&
|
|
569
|
+
!ecPrekeys.some((key) => key.keyId === commit.oneTimePreKeyId)) {
|
|
570
|
+
throw new Error('Atomic session/trust commit cannot consume a missing EC one-time prekey');
|
|
571
|
+
}
|
|
572
|
+
if (commit.kemOneTimePreKeyId !== undefined &&
|
|
573
|
+
!kemPrekeys.some((key) => key.keyId === commit.kemOneTimePreKeyId)) {
|
|
574
|
+
throw new Error('Atomic session/trust commit cannot consume a missing KEM one-time prekey');
|
|
575
|
+
}
|
|
576
|
+
let updatedKyberStateValue = null;
|
|
577
|
+
if (commit.kyberPreKeyUse) {
|
|
578
|
+
const state = kyberStateValue
|
|
579
|
+
? JSON.parse(await this.decrypt(kyberStateValue))
|
|
580
|
+
: null;
|
|
581
|
+
recordKyberPreKeyUse(state, commit.localIdentityType, commit.kyberPreKeyUse);
|
|
582
|
+
updatedKyberStateValue = await this.encrypt(JSON.stringify(state));
|
|
583
|
+
}
|
|
584
|
+
const operations = [
|
|
585
|
+
{ type: 'check', key: contactStorageKey, expectedValue: existingContactValue },
|
|
586
|
+
{ type: 'set', key: sessionStorageKey, value: sessionValue },
|
|
587
|
+
];
|
|
588
|
+
if (contactStatus === 'NEW') {
|
|
589
|
+
const newContact = createUnverifiedContactIdentityRecord(commit.contactIdentity, Date.now());
|
|
590
|
+
operations.push({
|
|
591
|
+
type: 'set',
|
|
592
|
+
key: contactStorageKey,
|
|
593
|
+
value: await this.encrypt(JSON.stringify(newContact)),
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
if (commit.oneTimePreKeyId !== undefined) {
|
|
597
|
+
operations.push({ type: 'check', key: ecStorageKey, expectedValue: ecPrekeyValue });
|
|
598
|
+
operations.push({
|
|
599
|
+
type: 'set',
|
|
600
|
+
key: ecStorageKey,
|
|
601
|
+
value: await this.encrypt(JSON.stringify(ecPrekeys.filter((key) => key.keyId !== commit.oneTimePreKeyId))),
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
if (commit.kemOneTimePreKeyId !== undefined) {
|
|
605
|
+
operations.push({ type: 'check', key: kemStorageKey, expectedValue: kemPrekeyValue });
|
|
606
|
+
operations.push({
|
|
607
|
+
type: 'set',
|
|
608
|
+
key: kemStorageKey,
|
|
609
|
+
value: await this.encrypt(JSON.stringify(kemPrekeys.filter((key) => key.keyId !== commit.kemOneTimePreKeyId))),
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
if (commit.kyberPreKeyUse && updatedKyberStateValue) {
|
|
613
|
+
operations.push({ type: 'check', key: kyberStorageKey, expectedValue: kyberStateValue });
|
|
614
|
+
operations.push({ type: 'set', key: kyberStorageKey, value: updatedKyberStateValue });
|
|
615
|
+
}
|
|
616
|
+
if (commit.receivedContent)
|
|
617
|
+
operations.push({
|
|
618
|
+
type: 'set',
|
|
619
|
+
key: `@signal:received-content:${commit.receivedContent.id}`,
|
|
620
|
+
value: await this.encrypt(JSON.stringify(commit.receivedContent)),
|
|
621
|
+
});
|
|
622
|
+
await this.storageBackend.atomicWrite(operations);
|
|
623
|
+
}
|
|
624
|
+
async getReceivedContent(id) {
|
|
625
|
+
this.ensureInitialized();
|
|
626
|
+
const value = await this.storageBackend.getItem(`@signal:received-content:${id}`);
|
|
627
|
+
return value ? parseReceivedContent(await this.decrypt(value), id) : null;
|
|
628
|
+
}
|
|
629
|
+
async deleteReceivedContent(id) {
|
|
630
|
+
this.ensureInitialized();
|
|
631
|
+
await this.storageBackend.removeItem(`@signal:received-content:${id}`);
|
|
632
|
+
}
|
|
633
|
+
async deleteExpiredReceivedContent(before) {
|
|
634
|
+
this.ensureInitialized();
|
|
635
|
+
const prefix = '@signal:received-content:';
|
|
636
|
+
const keys = (await this.storageBackend.getAllKeys()).filter((key) => key.startsWith(prefix));
|
|
637
|
+
const expired = [];
|
|
638
|
+
for (const key of keys) {
|
|
639
|
+
const value = await this.getReceivedContent(key.slice(prefix.length));
|
|
640
|
+
if (value && value.receivedAt < before)
|
|
641
|
+
expired.push(key);
|
|
642
|
+
}
|
|
643
|
+
if (expired.length)
|
|
644
|
+
await this.storageBackend.atomicWrite(expired.map((key) => ({ type: 'remove', key })));
|
|
645
|
+
return expired.length;
|
|
646
|
+
}
|
|
647
|
+
async deleteSessionRecord(address) {
|
|
648
|
+
this.ensureInitialized();
|
|
649
|
+
const addressKey = this.serializeAddress(address);
|
|
650
|
+
const storageKey = `${STORAGE_KEYS.SESSION_PREFIX}${addressKey}`;
|
|
651
|
+
await this.storageBackend.removeItem(storageKey);
|
|
652
|
+
}
|
|
653
|
+
async archiveCurrentSession(address, newSession) {
|
|
654
|
+
this.ensureInitialized();
|
|
655
|
+
// With SessionRecord, archiving is handled at the DeviceRecord level
|
|
656
|
+
// This method updates or replaces the session state
|
|
657
|
+
if (newSession) {
|
|
658
|
+
const record = await this.getSessionRecord(address);
|
|
659
|
+
if (record) {
|
|
660
|
+
// Update existing session with new state
|
|
661
|
+
record.currentSession = newSession;
|
|
662
|
+
if (record.metadata) {
|
|
663
|
+
record.metadata.lastSentAt = Date.now();
|
|
664
|
+
}
|
|
665
|
+
await this.storeSessionRecord(address, record);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
// If no new session, just delete the old one
|
|
670
|
+
await this.deleteSessionRecord(address);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
async getSessionsForUser(userId) {
|
|
674
|
+
this.ensureInitialized();
|
|
675
|
+
// Get all keys with session prefix
|
|
676
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
677
|
+
const sessionKeys = allKeys.filter((key) => key.startsWith(STORAGE_KEYS.SESSION_PREFIX));
|
|
678
|
+
const sessions = [];
|
|
679
|
+
for (const key of sessionKeys) {
|
|
680
|
+
const storedJson = await this.storageBackend.getItem(key);
|
|
681
|
+
if (storedJson) {
|
|
682
|
+
const stored = JSON.parse(storedJson);
|
|
683
|
+
if (stored.userId === userId) {
|
|
684
|
+
const decrypted = await this.decrypt(stored.data);
|
|
685
|
+
try {
|
|
686
|
+
sessions.push(deserializeSessionRecord(decrypted));
|
|
687
|
+
}
|
|
688
|
+
catch {
|
|
689
|
+
await this.storageBackend.removeItem(key);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return sessions;
|
|
695
|
+
}
|
|
696
|
+
async hasSession(address) {
|
|
697
|
+
this.ensureInitialized();
|
|
698
|
+
const record = await this.getSessionRecord(address);
|
|
699
|
+
return record !== null && record.currentSession !== null;
|
|
700
|
+
}
|
|
701
|
+
// ============================================================================
|
|
702
|
+
// Utility
|
|
703
|
+
// ============================================================================
|
|
704
|
+
async getSessionCount() {
|
|
705
|
+
this.ensureInitialized();
|
|
706
|
+
// Get all session keys
|
|
707
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
708
|
+
const sessionKeys = allKeys.filter((key) => key.startsWith(STORAGE_KEYS.SESSION_PREFIX));
|
|
709
|
+
return sessionKeys.length;
|
|
710
|
+
}
|
|
711
|
+
/** Delete every store record, then replace the value key, as {@link KeyValueSignalProtocolStore.reset} does. */
|
|
712
|
+
async clearAllKeys() {
|
|
713
|
+
this.ensureInitialized();
|
|
714
|
+
await this.resetData();
|
|
715
|
+
}
|
|
716
|
+
// ============================================================================
|
|
717
|
+
// Metadata storage
|
|
718
|
+
// ============================================================================
|
|
719
|
+
async getMetadata(key) {
|
|
720
|
+
return this.storageBackend.getItem(this.getMetadataStorageKey(key));
|
|
721
|
+
}
|
|
722
|
+
async setMetadata(key, value) {
|
|
723
|
+
await this.storageBackend.setItem(this.getMetadataStorageKey(key), value);
|
|
724
|
+
}
|
|
725
|
+
async deleteMetadata(key) {
|
|
726
|
+
await this.storageBackend.removeItem(this.getMetadataStorageKey(key));
|
|
727
|
+
}
|
|
728
|
+
async compareAndSetMetadata(key, expected, value) {
|
|
729
|
+
const storageKey = this.getMetadataStorageKey(key);
|
|
730
|
+
try {
|
|
731
|
+
await this.storageBackend.atomicWrite([
|
|
732
|
+
{ type: 'check', key: storageKey, expectedValue: expected },
|
|
733
|
+
value === null
|
|
734
|
+
? { type: 'remove', key: storageKey }
|
|
735
|
+
: { type: 'set', key: storageKey, value },
|
|
736
|
+
]);
|
|
737
|
+
return true;
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
// A failed batch commits nothing. Do not report capacity failure as contention.
|
|
741
|
+
if (error &&
|
|
742
|
+
typeof error === 'object' &&
|
|
743
|
+
'name' in error &&
|
|
744
|
+
error.name === 'QuotaExceededError')
|
|
745
|
+
throw error;
|
|
746
|
+
if ((await this.storageBackend.getItem(storageKey)) !== expected)
|
|
747
|
+
return false;
|
|
748
|
+
throw error;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
// ============================================================================
|
|
752
|
+
// Private Helper Methods
|
|
753
|
+
// ============================================================================
|
|
754
|
+
/**
|
|
755
|
+
* Throw when the adapter is not initialized
|
|
756
|
+
* @throws Error if not initialized
|
|
757
|
+
*/
|
|
758
|
+
ensureInitialized() {
|
|
759
|
+
if (!this.initialized || !this.databaseKey) {
|
|
760
|
+
throw new Error('KeyValueSignalProtocolStore was used before initialization completed. Create it with KeyValueSignalProtocolStore.create(...).');
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Serialize ProtocolAddress to string key
|
|
765
|
+
*/
|
|
766
|
+
serializeAddress(address) {
|
|
767
|
+
return `${address.userId}.${address.deviceId}`;
|
|
768
|
+
}
|
|
769
|
+
getContactStorageKey(address, identityType) {
|
|
770
|
+
return `${STORAGE_KEYS.CONTACT_V1_PREFIX}${address.userId}:${identityType ?? 'aci'}`;
|
|
771
|
+
}
|
|
772
|
+
async readContactRecord(address, identityType) {
|
|
773
|
+
const encrypted = await this.storageBackend.getItem(this.getContactStorageKey(address, identityType));
|
|
774
|
+
if (!encrypted) {
|
|
775
|
+
// A miss here can still leave an obsolete single-publicKey row under the
|
|
776
|
+
// unsuffixed key, which no read path can interpret. Delete it.
|
|
777
|
+
await this.storageBackend.removeItem(`${STORAGE_KEYS.CONTACT_PREFIX}${address.userId}`);
|
|
778
|
+
return null;
|
|
779
|
+
}
|
|
780
|
+
const record = JSON.parse(await this.decrypt(encrypted));
|
|
781
|
+
validateContactIdentityRecord(record);
|
|
782
|
+
return record;
|
|
783
|
+
}
|
|
784
|
+
async writeContactRecord(storageKey, record) {
|
|
785
|
+
validateContactIdentityRecord(record);
|
|
786
|
+
await this.storageBackend.setItem(storageKey, await this.encrypt(JSON.stringify(record)));
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Encrypt data using AES-256-GCM
|
|
790
|
+
*
|
|
791
|
+
* Uses the Signal Protocol crypto module.
|
|
792
|
+
*
|
|
793
|
+
* @param plaintext - Data to encrypt
|
|
794
|
+
* @returns Base64-encoded: IV (12 bytes) + ciphertext + auth tag (16 bytes)
|
|
795
|
+
*/
|
|
796
|
+
async encrypt(plaintext) {
|
|
797
|
+
if (!this.databaseKey) {
|
|
798
|
+
throw new Error('Database key not available');
|
|
799
|
+
}
|
|
800
|
+
// Generate random IV (12 bytes for GCM)
|
|
801
|
+
const iv = await SignalProtocolCrypto.generateRandomBytes(12);
|
|
802
|
+
// Encode plaintext
|
|
803
|
+
const data = new TextEncoder().encode(plaintext);
|
|
804
|
+
// Encrypt using proper AES-256-GCM via Signal Protocol crypto module
|
|
805
|
+
const encrypted = await this.aesGcmEncrypt(data, this.databaseKey, iv);
|
|
806
|
+
// Combine IV + ciphertext and encode as base64
|
|
807
|
+
const combined = new Uint8Array(12 + encrypted.length);
|
|
808
|
+
combined.set(iv, 0);
|
|
809
|
+
combined.set(encrypted, 12);
|
|
810
|
+
return this.uint8ArrayToBase64(combined);
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Decrypt data using AES-256-GCM
|
|
814
|
+
*
|
|
815
|
+
* @param encrypted - Base64-encoded: IV (12 bytes) + ciphertext + auth tag (16 bytes)
|
|
816
|
+
* @returns Decrypted plaintext
|
|
817
|
+
*/
|
|
818
|
+
async decrypt(encrypted) {
|
|
819
|
+
if (!this.databaseKey) {
|
|
820
|
+
throw new Error('Database key not available');
|
|
821
|
+
}
|
|
822
|
+
// Decode from base64
|
|
823
|
+
const combined = this.base64ToUint8Array(encrypted);
|
|
824
|
+
// Extract IV and ciphertext
|
|
825
|
+
const iv = combined.slice(0, 12);
|
|
826
|
+
const ciphertext = combined.slice(12);
|
|
827
|
+
// Decrypt
|
|
828
|
+
const plaintext = await this.aesGcmDecrypt(ciphertext, this.databaseKey, iv);
|
|
829
|
+
// Decode plaintext
|
|
830
|
+
return utf8Decode(plaintext);
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* AES-256-GCM encryption
|
|
834
|
+
*
|
|
835
|
+
* Uses the Signal Protocol crypto module, which uses Web Crypto when
|
|
836
|
+
* `crypto.subtle` exists and the bundled Noble AES otherwise.
|
|
837
|
+
*
|
|
838
|
+
* @param data - Plaintext to encrypt
|
|
839
|
+
* @param key - 32-byte AES-256 key
|
|
840
|
+
* @param iv - 12-byte initialization vector (already generated in encrypt())
|
|
841
|
+
* @returns Ciphertext with appended 16-byte auth tag
|
|
842
|
+
*/
|
|
843
|
+
async aesGcmEncrypt(data, key, iv) {
|
|
844
|
+
// Use the Signal Protocol crypto module's AES-GCM implementation
|
|
845
|
+
const result = await SignalProtocolCrypto.aesGcmEncryptWithIV(key, data, iv);
|
|
846
|
+
// Combine ciphertext + authTag into single buffer
|
|
847
|
+
const ciphertextBytes = SignalProtocolCrypto.base64ToBytes(result.ciphertext);
|
|
848
|
+
const authTagBytes = SignalProtocolCrypto.base64ToBytes(result.authTag);
|
|
849
|
+
const combined = new Uint8Array(ciphertextBytes.length + authTagBytes.length);
|
|
850
|
+
combined.set(ciphertextBytes, 0);
|
|
851
|
+
combined.set(authTagBytes, ciphertextBytes.length);
|
|
852
|
+
return combined;
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* AES-256-GCM decryption
|
|
856
|
+
*
|
|
857
|
+
* Uses the Signal Protocol crypto module, which uses Web Crypto when
|
|
858
|
+
* `crypto.subtle` exists and the bundled Noble AES otherwise.
|
|
859
|
+
*
|
|
860
|
+
* @param ciphertext - Encrypted data with appended 16-byte auth tag
|
|
861
|
+
* @param key - 32-byte AES-256 key
|
|
862
|
+
* @param iv - 12-byte initialization vector
|
|
863
|
+
* @returns Decrypted plaintext
|
|
864
|
+
*/
|
|
865
|
+
async aesGcmDecrypt(ciphertext, key, iv) {
|
|
866
|
+
// Validate minimum ciphertext length (must be at least 16 bytes for auth tag)
|
|
867
|
+
const AES_GCM_TAG_BYTES = 16;
|
|
868
|
+
if (ciphertext.length < AES_GCM_TAG_BYTES) {
|
|
869
|
+
throw new Error(`Ciphertext too short: ${ciphertext.length} bytes (minimum ${AES_GCM_TAG_BYTES} for auth tag)`);
|
|
870
|
+
}
|
|
871
|
+
// Split ciphertext and auth tag (last 16 bytes)
|
|
872
|
+
const ciphertextOnly = ciphertext.slice(0, -AES_GCM_TAG_BYTES);
|
|
873
|
+
const authTag = ciphertext.slice(-AES_GCM_TAG_BYTES);
|
|
874
|
+
// Use the Signal Protocol crypto module's AES-GCM implementation
|
|
875
|
+
return await SignalProtocolCrypto.aesGcmDecrypt(key, SignalProtocolCrypto.bytesToBase64(ciphertextOnly), SignalProtocolCrypto.bytesToBase64(iv), SignalProtocolCrypto.bytesToBase64(authTag));
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Log security event
|
|
879
|
+
*/
|
|
880
|
+
async logSecurityEvent(event) {
|
|
881
|
+
const eventsJson = await this.storageBackend.getItem(STORAGE_KEYS.SECURITY_EVENTS);
|
|
882
|
+
const events = eventsJson ? JSON.parse(eventsJson) : [];
|
|
883
|
+
events.push(event);
|
|
884
|
+
// Keep only last 100 events
|
|
885
|
+
const trimmed = events.slice(-100);
|
|
886
|
+
await this.storageBackend.setItem(STORAGE_KEYS.SECURITY_EVENTS, JSON.stringify(trimmed));
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Convert Uint8Array to base64
|
|
890
|
+
*/
|
|
891
|
+
uint8ArrayToBase64(bytes) {
|
|
892
|
+
return SignalProtocolCrypto.bytesToBase64(bytes);
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Convert base64 to Uint8Array
|
|
896
|
+
*/
|
|
897
|
+
base64ToUint8Array(base64) {
|
|
898
|
+
return SignalProtocolCrypto.base64ToBytes(asBase64(base64));
|
|
899
|
+
}
|
|
900
|
+
getIdentityKeyStorageKey(identityType) {
|
|
901
|
+
return `${STORAGE_KEYS.IDENTITY_KEY_PREFIX}${identityType ?? 'aci'}`;
|
|
902
|
+
}
|
|
903
|
+
getRegistrationIdStorageKey(identityType) {
|
|
904
|
+
return `${STORAGE_KEYS.REGISTRATION_ID_PREFIX}${identityType ?? 'aci'}`;
|
|
905
|
+
}
|
|
906
|
+
getMetadataStorageKey(key) {
|
|
907
|
+
return `${STORAGE_KEYS.METADATA_PREFIX}${key}`;
|
|
908
|
+
}
|
|
909
|
+
getSignedPreKeyStorageKey(identityType) {
|
|
910
|
+
return `${STORAGE_KEYS.PREKEY_PREFIX}signed-all:${identityType ?? 'aci'}`;
|
|
911
|
+
}
|
|
912
|
+
getEcOneTimePreKeyStorageKey(identityType) {
|
|
913
|
+
return `${STORAGE_KEYS.PREKEY_PREFIX}one-time:${identityType ?? 'aci'}`;
|
|
914
|
+
}
|
|
915
|
+
getKyberPreKeyStorageKey(identityType) {
|
|
916
|
+
return `${STORAGE_KEYS.PREKEY_PREFIX}kyber:${identityType ?? 'aci'}`;
|
|
917
|
+
}
|
|
918
|
+
getKemOneTimePreKeyStorageKey(identityType) {
|
|
919
|
+
return `${STORAGE_KEYS.PREKEY_PREFIX}kem-one-time:${identityType ?? 'aci'}`;
|
|
920
|
+
}
|
|
921
|
+
getSesameUserStorageKey(userId) {
|
|
922
|
+
return `${STORAGE_KEYS.SESAME_USER_PREFIX}${userId}`;
|
|
923
|
+
}
|
|
924
|
+
getSenderKeyRecordStorageKey(groupId, userId, deviceId) {
|
|
925
|
+
return `${this.getSenderKeyRecordGroupPrefix(groupId)}${escapeKeyComponent(userId)}:${deviceId}`;
|
|
926
|
+
}
|
|
927
|
+
/** Prefix matching every sender key record stored for one group. */
|
|
928
|
+
getSenderKeyRecordGroupPrefix(groupId) {
|
|
929
|
+
return `${STORAGE_KEYS.SENDER_KEY_RECORD_PREFIX}${escapeKeyComponent(groupId)}:`;
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Prefix matching every skipped key of one sender in one group.
|
|
933
|
+
*
|
|
934
|
+
* Trailing `:` included: the chain index follows it, and without it the
|
|
935
|
+
* prefix for device 1 would also match device 10's keys.
|
|
936
|
+
*/
|
|
937
|
+
getSkippedSenderKeyPrefix(groupId, senderId, senderDeviceId) {
|
|
938
|
+
return `${this.getSkippedSenderKeyGroupPrefix(groupId)}${escapeKeyComponent(senderId)}:${senderDeviceId}:`;
|
|
939
|
+
}
|
|
940
|
+
/** Prefix matching every skipped key stored for one group. */
|
|
941
|
+
getSkippedSenderKeyGroupPrefix(groupId) {
|
|
942
|
+
return `${STORAGE_KEYS.SKIPPED_SENDER_KEY_PREFIX}${escapeKeyComponent(groupId)}:`;
|
|
943
|
+
}
|
|
944
|
+
getSkippedSenderKeyStorageKey(groupId, senderId, senderDeviceId, chainIndex) {
|
|
945
|
+
return `${this.getSkippedSenderKeyPrefix(groupId, senderId, senderDeviceId)}${chainIndex}`;
|
|
946
|
+
}
|
|
947
|
+
getMessageRecordStorageKey(sessionId, timestamp) {
|
|
948
|
+
return `${this.getMessageRecordSessionPrefix(sessionId)}${timestamp}`;
|
|
949
|
+
}
|
|
950
|
+
/** Prefix matching every retry record stored for one session. */
|
|
951
|
+
getMessageRecordSessionPrefix(sessionId) {
|
|
952
|
+
return `${STORAGE_KEYS.MESSAGE_RECORD_PREFIX}${escapeKeyComponent(sessionId)}:`;
|
|
953
|
+
}
|
|
954
|
+
// ============================================================================
|
|
955
|
+
// SESAME Multi-Device Session Management
|
|
956
|
+
// ============================================================================
|
|
957
|
+
// A device's session lives only under its session key; see `./device-record`.
|
|
958
|
+
async getUserRecord(userId) {
|
|
959
|
+
const record = await this.getStoredUserRecord(userId);
|
|
960
|
+
if (!record)
|
|
961
|
+
return null;
|
|
962
|
+
for (const [deviceId, device] of record.devices) {
|
|
963
|
+
const session = await this.getSessionRecord(ProtocolAddress.create(userId, deviceId));
|
|
964
|
+
record.devices.set(deviceId, withSession(userId, deviceId, device, session));
|
|
965
|
+
}
|
|
966
|
+
return record;
|
|
967
|
+
}
|
|
968
|
+
async setUserRecord(userId, record) {
|
|
969
|
+
this.ensureInitialized();
|
|
970
|
+
const encrypted = await this.encrypt(JSON.stringify(encodeUserRecord(record)));
|
|
971
|
+
await this.storageBackend.setItem(this.getSesameUserStorageKey(userId), encrypted);
|
|
972
|
+
}
|
|
973
|
+
async getDeviceRecord(userId, deviceId) {
|
|
974
|
+
const stored = (await this.getStoredUserRecord(userId))?.devices.get(deviceId) ?? null;
|
|
975
|
+
const session = await this.getSessionRecord(ProtocolAddress.create(userId, deviceId));
|
|
976
|
+
return withSession(userId, deviceId, stored, session);
|
|
977
|
+
}
|
|
978
|
+
async setDeviceRecord(userId, deviceId, record) {
|
|
979
|
+
const now = Date.now();
|
|
980
|
+
const userRecord = (await this.getStoredUserRecord(userId)) ?? {
|
|
981
|
+
userId,
|
|
982
|
+
devices: new Map(),
|
|
983
|
+
createdAt: now,
|
|
984
|
+
updatedAt: now,
|
|
985
|
+
};
|
|
986
|
+
userRecord.devices.set(deviceId, record);
|
|
987
|
+
userRecord.updatedAt = now;
|
|
988
|
+
const operations = [
|
|
989
|
+
{
|
|
990
|
+
type: 'set',
|
|
991
|
+
key: this.getSesameUserStorageKey(userId),
|
|
992
|
+
value: await this.encrypt(JSON.stringify(encodeUserRecord(userRecord))),
|
|
993
|
+
},
|
|
994
|
+
];
|
|
995
|
+
if (record.session) {
|
|
996
|
+
operations.push({
|
|
997
|
+
type: 'set',
|
|
998
|
+
key: `${STORAGE_KEYS.SESSION_PREFIX}${this.serializeAddress(ProtocolAddress.create(userId, deviceId))}`,
|
|
999
|
+
value: JSON.stringify({
|
|
1000
|
+
userId,
|
|
1001
|
+
deviceId,
|
|
1002
|
+
data: await this.encrypt(serializeSessionRecord(record.session)),
|
|
1003
|
+
updatedAt: now,
|
|
1004
|
+
}),
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
await this.storageBackend.atomicWrite(operations);
|
|
1008
|
+
}
|
|
1009
|
+
async deleteDeviceRecord(userId, deviceId) {
|
|
1010
|
+
const userRecord = await this.getStoredUserRecord(userId);
|
|
1011
|
+
if (!userRecord) {
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
userRecord.devices.delete(deviceId);
|
|
1015
|
+
userRecord.updatedAt = Date.now();
|
|
1016
|
+
const userKey = this.getSesameUserStorageKey(userId);
|
|
1017
|
+
const sessionKey = `${STORAGE_KEYS.SESSION_PREFIX}${this.serializeAddress(ProtocolAddress.create(userId, deviceId))}`;
|
|
1018
|
+
await this.storageBackend.atomicWrite([
|
|
1019
|
+
{ type: 'remove', key: sessionKey },
|
|
1020
|
+
userRecord.devices.size === 0
|
|
1021
|
+
? { type: 'remove', key: userKey }
|
|
1022
|
+
: {
|
|
1023
|
+
type: 'set',
|
|
1024
|
+
key: userKey,
|
|
1025
|
+
value: await this.encrypt(JSON.stringify(encodeUserRecord(userRecord))),
|
|
1026
|
+
},
|
|
1027
|
+
]);
|
|
1028
|
+
}
|
|
1029
|
+
async getStoredUserRecord(userId) {
|
|
1030
|
+
this.ensureInitialized();
|
|
1031
|
+
const encrypted = await this.storageBackend.getItem(this.getSesameUserStorageKey(userId));
|
|
1032
|
+
if (!encrypted)
|
|
1033
|
+
return null;
|
|
1034
|
+
return decodeUserRecord(JSON.parse(await this.decrypt(encrypted)));
|
|
1035
|
+
}
|
|
1036
|
+
async deleteStaleRecords(maxLatency) {
|
|
1037
|
+
const now = Date.now();
|
|
1038
|
+
let deletedCount = 0;
|
|
1039
|
+
for (const userId of await this.getAllUserIds()) {
|
|
1040
|
+
const userRecord = await this.getUserRecord(userId);
|
|
1041
|
+
if (!userRecord)
|
|
1042
|
+
continue;
|
|
1043
|
+
const devicesToDelete = [];
|
|
1044
|
+
for (const [deviceId, deviceRecord] of userRecord.devices) {
|
|
1045
|
+
const hasNoSessions = !deviceRecord.session ||
|
|
1046
|
+
(!deviceRecord.session.currentSession &&
|
|
1047
|
+
Object.keys(deviceRecord.session.archivedSessions).length === 0);
|
|
1048
|
+
const isStale = now - deviceRecord.updatedAt > maxLatency;
|
|
1049
|
+
if (hasNoSessions && isStale) {
|
|
1050
|
+
devicesToDelete.push(deviceId);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
for (const deviceId of devicesToDelete) {
|
|
1054
|
+
await this.deleteDeviceRecord(userId, deviceId);
|
|
1055
|
+
deletedCount++;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
return deletedCount;
|
|
1059
|
+
}
|
|
1060
|
+
async cleanupExpiredSessions(maxRecv) {
|
|
1061
|
+
const now = Date.now();
|
|
1062
|
+
let deletedCount = 0;
|
|
1063
|
+
for (const userId of await this.getAllUserIds()) {
|
|
1064
|
+
const userRecord = await this.getUserRecord(userId);
|
|
1065
|
+
if (!userRecord)
|
|
1066
|
+
continue;
|
|
1067
|
+
for (const [deviceId, deviceRecord] of userRecord.devices) {
|
|
1068
|
+
if (!deviceRecord.session)
|
|
1069
|
+
continue;
|
|
1070
|
+
let modified = false;
|
|
1071
|
+
if (deviceRecord.session.currentSession) {
|
|
1072
|
+
const createdAt = deviceRecord.session.metadata?.createdAt ??
|
|
1073
|
+
deviceRecord.session.currentSession.createdAt;
|
|
1074
|
+
if (now - createdAt > maxRecv) {
|
|
1075
|
+
deviceRecord.session.currentSession = null;
|
|
1076
|
+
modified = true;
|
|
1077
|
+
deletedCount++;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
const originalCount = Object.keys(deviceRecord.session.archivedSessions).length;
|
|
1081
|
+
const archivedKeys = Object.keys(deviceRecord.session.archivedSessions);
|
|
1082
|
+
for (const baseKey of archivedKeys) {
|
|
1083
|
+
const session = deviceRecord.session.archivedSessions[baseKey];
|
|
1084
|
+
if (session && now - session.createdAt > maxRecv) {
|
|
1085
|
+
delete deviceRecord.session.archivedSessions[baseKey];
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
const removed = originalCount - Object.keys(deviceRecord.session.archivedSessions).length;
|
|
1089
|
+
if (removed > 0) {
|
|
1090
|
+
deletedCount += removed;
|
|
1091
|
+
modified = true;
|
|
1092
|
+
}
|
|
1093
|
+
if (modified) {
|
|
1094
|
+
deviceRecord.updatedAt = Date.now();
|
|
1095
|
+
await this.setDeviceRecord(userId, deviceId, deviceRecord);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return deletedCount;
|
|
1100
|
+
}
|
|
1101
|
+
async getAllUserIds() {
|
|
1102
|
+
this.ensureInitialized();
|
|
1103
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1104
|
+
return allKeys
|
|
1105
|
+
.filter((key) => key.startsWith(STORAGE_KEYS.SESAME_USER_PREFIX))
|
|
1106
|
+
.map((key) => key.slice(STORAGE_KEYS.SESAME_USER_PREFIX.length));
|
|
1107
|
+
}
|
|
1108
|
+
async getSesameDeviceIds(userId) {
|
|
1109
|
+
const userRecord = await this.getStoredUserRecord(userId);
|
|
1110
|
+
return userRecord ? Array.from(userRecord.devices.keys()) : [];
|
|
1111
|
+
}
|
|
1112
|
+
async getDeviceSession(userId, deviceId) {
|
|
1113
|
+
const deviceRecord = await this.getDeviceRecord(userId, deviceId);
|
|
1114
|
+
return deviceRecord?.session ?? null;
|
|
1115
|
+
}
|
|
1116
|
+
async setDeviceSession(userId, deviceId, session) {
|
|
1117
|
+
const deviceRecord = await this.getDeviceRecord(userId, deviceId);
|
|
1118
|
+
if (deviceRecord) {
|
|
1119
|
+
deviceRecord.session = session;
|
|
1120
|
+
await this.setDeviceRecord(userId, deviceId, deviceRecord);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
// ============================================================================
|
|
1124
|
+
// Sender Keys Management (Group Messaging)
|
|
1125
|
+
// ============================================================================
|
|
1126
|
+
async storeSenderKey(groupId, userId, deviceId, state) {
|
|
1127
|
+
await this.storeSenderKeyRecord(groupId, userId, deviceId, [state]);
|
|
1128
|
+
}
|
|
1129
|
+
async getSenderKey(groupId, userId, deviceId) {
|
|
1130
|
+
const record = await this.getSenderKeyRecord(groupId, userId, deviceId);
|
|
1131
|
+
return record?.[0] ?? null;
|
|
1132
|
+
}
|
|
1133
|
+
async deleteSenderKey(groupId, userId, deviceId) {
|
|
1134
|
+
this.ensureInitialized();
|
|
1135
|
+
await this.removeSenderKeyIdPointers(groupId, userId, deviceId);
|
|
1136
|
+
await this.storageBackend.removeItem(this.getSenderKeyRecordStorageKey(groupId, userId, deviceId));
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Build the storage key for a `senderKeyId` pointer.
|
|
1140
|
+
*
|
|
1141
|
+
* Scoped by sender, so one sender's id can never displace another's. That
|
|
1142
|
+
* scoping is only worth anything if the join is injective, and a
|
|
1143
|
+
* `senderKeyId` is the component least entitled to trust. It is whatever
|
|
1144
|
+
* bytes the sender put in the frame's distribution field, validated
|
|
1145
|
+
* nowhere, so it is escaped rather than reasoned about. With every
|
|
1146
|
+
* component escaped, no choice of id can synthesize another sender's
|
|
1147
|
+
* `:<deviceId>:<userId>` tail.
|
|
1148
|
+
*/
|
|
1149
|
+
getSenderKeyIdPointerKey(senderKeyId, userId, deviceId) {
|
|
1150
|
+
return `${STORAGE_KEYS.SENDER_KEY_ID_INDEX_PREFIX}${escapeKeyComponent(senderKeyId)}:${deviceId}:${escapeKeyComponent(userId)}`;
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Drop the `senderKeyId` pointers a record owns, before the record goes.
|
|
1154
|
+
*
|
|
1155
|
+
* Ordered that way deliberately. A pointer that outlives its record is
|
|
1156
|
+
* self-healing, because `resolveGroupForSenderKeyId` confirms and deletes
|
|
1157
|
+
* it. A record that outlives its pointers is simply unreachable by id.
|
|
1158
|
+
*/
|
|
1159
|
+
async removeSenderKeyIdPointers(groupId, userId, deviceId) {
|
|
1160
|
+
const record = await this.getSenderKeyRecord(groupId, userId, deviceId);
|
|
1161
|
+
if (!record)
|
|
1162
|
+
return;
|
|
1163
|
+
await this.storageBackend.removeMany(record
|
|
1164
|
+
.map((state) => state.senderKeyId)
|
|
1165
|
+
.filter(Boolean)
|
|
1166
|
+
.map((senderKeyId) => this.getSenderKeyIdPointerKey(senderKeyId, userId, deviceId)));
|
|
1167
|
+
}
|
|
1168
|
+
async resolveGroupForSenderKeyId(senderKeyId, userId, deviceId) {
|
|
1169
|
+
this.ensureInitialized();
|
|
1170
|
+
if (!senderKeyId)
|
|
1171
|
+
return null;
|
|
1172
|
+
const recordKey = await this.storageBackend.getItem(this.getSenderKeyIdPointerKey(senderKeyId, userId, deviceId));
|
|
1173
|
+
if (!recordKey)
|
|
1174
|
+
return null;
|
|
1175
|
+
// The pointer is a cache over the records, so it is confirmed against the
|
|
1176
|
+
// record rather than trusted. The sender check is an invariant assertion
|
|
1177
|
+
// now that the pointer key is itself sender-scoped. A mismatch would mean
|
|
1178
|
+
// the two disagree, so resolve to nothing rather than reaching into
|
|
1179
|
+
// another sender's record. It is deliberately not deleted. The key
|
|
1180
|
+
// belongs to this sender, and a wrong value is a bug to notice, not a
|
|
1181
|
+
// stale entry to sweep.
|
|
1182
|
+
const parsed = this.parseSenderKeyRecordStorageKey(recordKey);
|
|
1183
|
+
if (!parsed || parsed.userId !== userId || parsed.deviceId !== deviceId) {
|
|
1184
|
+
return null;
|
|
1185
|
+
}
|
|
1186
|
+
const record = await this.getSenderKeyRecord(parsed.groupId, userId, deviceId);
|
|
1187
|
+
if (!record?.some((state) => state.senderKeyId === senderKeyId)) {
|
|
1188
|
+
await this.storageBackend.removeItem(this.getSenderKeyIdPointerKey(senderKeyId, userId, deviceId));
|
|
1189
|
+
return null;
|
|
1190
|
+
}
|
|
1191
|
+
return parsed.groupId;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Split a record storage key back into its parts.
|
|
1195
|
+
*
|
|
1196
|
+
* The components were escaped on the way in, so the body holds exactly three
|
|
1197
|
+
* `:`-separated fields and the split is unambiguous. Anything else is a key
|
|
1198
|
+
* this adapter did not write.
|
|
1199
|
+
*/
|
|
1200
|
+
parseSenderKeyRecordStorageKey(recordKey) {
|
|
1201
|
+
if (!recordKey.startsWith(STORAGE_KEYS.SENDER_KEY_RECORD_PREFIX))
|
|
1202
|
+
return null;
|
|
1203
|
+
const body = recordKey.slice(STORAGE_KEYS.SENDER_KEY_RECORD_PREFIX.length);
|
|
1204
|
+
const parts = body.split(':');
|
|
1205
|
+
if (parts.length !== 3)
|
|
1206
|
+
return null;
|
|
1207
|
+
const [groupId, userId, device] = parts;
|
|
1208
|
+
if (!groupId || !userId || !device)
|
|
1209
|
+
return null;
|
|
1210
|
+
const deviceId = Number(device);
|
|
1211
|
+
if (!Number.isInteger(deviceId))
|
|
1212
|
+
return null;
|
|
1213
|
+
return {
|
|
1214
|
+
groupId: unescapeKeyComponent(groupId),
|
|
1215
|
+
userId: unescapeKeyComponent(userId),
|
|
1216
|
+
deviceId,
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
async getAllSenderKeysForGroup(groupId) {
|
|
1220
|
+
this.ensureInitialized();
|
|
1221
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1222
|
+
const senderKeys = allKeys.filter((key) => key.startsWith(this.getSenderKeyRecordGroupPrefix(groupId)));
|
|
1223
|
+
const states = [];
|
|
1224
|
+
for (const key of senderKeys) {
|
|
1225
|
+
const encrypted = await this.storageBackend.getItem(key);
|
|
1226
|
+
if (!encrypted)
|
|
1227
|
+
continue;
|
|
1228
|
+
const decrypted = await this.decrypt(encrypted);
|
|
1229
|
+
const record = JSON.parse(decrypted);
|
|
1230
|
+
if (record[0]) {
|
|
1231
|
+
states.push(record[0]);
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
return states;
|
|
1235
|
+
}
|
|
1236
|
+
async deleteAllSenderKeysForGroup(groupId) {
|
|
1237
|
+
this.ensureInitialized();
|
|
1238
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1239
|
+
const senderKeys = allKeys.filter((key) => key.startsWith(this.getSenderKeyRecordGroupPrefix(groupId)));
|
|
1240
|
+
for (const recordKey of senderKeys) {
|
|
1241
|
+
const parsed = this.parseSenderKeyRecordStorageKey(recordKey);
|
|
1242
|
+
if (parsed) {
|
|
1243
|
+
await this.removeSenderKeyIdPointers(parsed.groupId, parsed.userId, parsed.deviceId);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
const skippedKeys = allKeys.filter((key) => key.startsWith(this.getSkippedSenderKeyGroupPrefix(groupId)));
|
|
1247
|
+
await this.storageBackend.removeMany([...senderKeys, ...skippedKeys]);
|
|
1248
|
+
return senderKeys.length;
|
|
1249
|
+
}
|
|
1250
|
+
async storeSenderKeyRecord(groupId, userId, deviceId, states, receive) {
|
|
1251
|
+
this.ensureInitialized();
|
|
1252
|
+
if (states.length === 0) {
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
const recordKey = this.getSenderKeyRecordStorageKey(groupId, userId, deviceId);
|
|
1256
|
+
// Rotation drops the oldest state out of the record. Its pointer has to go
|
|
1257
|
+
// with it. Otherwise a replayed message naming a discarded key still
|
|
1258
|
+
// resolves to a group, and fails one layer deeper than it should.
|
|
1259
|
+
const superseded = await this.getSenderKeyRecord(groupId, userId, deviceId);
|
|
1260
|
+
const retained = new Set(states.map((state) => state.senderKeyId));
|
|
1261
|
+
const operations = [];
|
|
1262
|
+
if (superseded) {
|
|
1263
|
+
operations.push(...superseded
|
|
1264
|
+
.map((state) => state.senderKeyId)
|
|
1265
|
+
.filter((senderKeyId) => senderKeyId && !retained.has(senderKeyId))
|
|
1266
|
+
.map((senderKeyId) => ({
|
|
1267
|
+
type: 'remove',
|
|
1268
|
+
key: this.getSenderKeyIdPointerKey(senderKeyId, userId, deviceId),
|
|
1269
|
+
})));
|
|
1270
|
+
}
|
|
1271
|
+
operations.push({
|
|
1272
|
+
type: 'set',
|
|
1273
|
+
key: recordKey,
|
|
1274
|
+
value: await this.encrypt(JSON.stringify(states)),
|
|
1275
|
+
});
|
|
1276
|
+
for (const senderKeyId of retained) {
|
|
1277
|
+
if (!senderKeyId)
|
|
1278
|
+
continue;
|
|
1279
|
+
operations.push({
|
|
1280
|
+
type: 'set',
|
|
1281
|
+
key: this.getSenderKeyIdPointerKey(senderKeyId, userId, deviceId),
|
|
1282
|
+
value: recordKey,
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
if (receive) {
|
|
1286
|
+
operations.push({
|
|
1287
|
+
type: 'set',
|
|
1288
|
+
key: `@signal:received-content:${receive.content.id}`,
|
|
1289
|
+
value: await this.encrypt(JSON.stringify(receive.content)),
|
|
1290
|
+
});
|
|
1291
|
+
if (receive.consumedChainIndex !== undefined)
|
|
1292
|
+
operations.push({
|
|
1293
|
+
type: 'remove',
|
|
1294
|
+
key: this.getSkippedSenderKeyStorageKey(groupId, userId, deviceId, receive.consumedChainIndex),
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
await this.storageBackend.atomicWrite(operations);
|
|
1298
|
+
}
|
|
1299
|
+
async getSenderKeyRecord(groupId, userId, deviceId) {
|
|
1300
|
+
this.ensureInitialized();
|
|
1301
|
+
const encrypted = await this.storageBackend.getItem(this.getSenderKeyRecordStorageKey(groupId, userId, deviceId));
|
|
1302
|
+
if (!encrypted) {
|
|
1303
|
+
return null;
|
|
1304
|
+
}
|
|
1305
|
+
const decrypted = await this.decrypt(encrypted);
|
|
1306
|
+
return JSON.parse(decrypted);
|
|
1307
|
+
}
|
|
1308
|
+
// ============================================================================
|
|
1309
|
+
// Skipped Sender Keys
|
|
1310
|
+
// ============================================================================
|
|
1311
|
+
async storeSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex, messageKey) {
|
|
1312
|
+
this.ensureInitialized();
|
|
1313
|
+
await this.storageBackend.setItem(this.getSkippedSenderKeyStorageKey(groupId, senderId, senderDeviceId, chainIndex), await this.encrypt(JSON.stringify(messageKey)));
|
|
1314
|
+
}
|
|
1315
|
+
async getSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex) {
|
|
1316
|
+
this.ensureInitialized();
|
|
1317
|
+
const encrypted = await this.storageBackend.getItem(this.getSkippedSenderKeyStorageKey(groupId, senderId, senderDeviceId, chainIndex));
|
|
1318
|
+
if (!encrypted) {
|
|
1319
|
+
return null;
|
|
1320
|
+
}
|
|
1321
|
+
const decrypted = await this.decrypt(encrypted);
|
|
1322
|
+
return JSON.parse(decrypted);
|
|
1323
|
+
}
|
|
1324
|
+
async deleteSkippedSenderKey(groupId, senderId, senderDeviceId, chainIndex) {
|
|
1325
|
+
this.ensureInitialized();
|
|
1326
|
+
await this.storageBackend.removeItem(this.getSkippedSenderKeyStorageKey(groupId, senderId, senderDeviceId, chainIndex));
|
|
1327
|
+
}
|
|
1328
|
+
async countSkippedSenderKeys(groupId, senderId, senderDeviceId) {
|
|
1329
|
+
this.ensureInitialized();
|
|
1330
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1331
|
+
return allKeys.filter((key) => key.startsWith(this.getSkippedSenderKeyPrefix(groupId, senderId, senderDeviceId))).length;
|
|
1332
|
+
}
|
|
1333
|
+
async deleteOldestSkippedSenderKeys(groupId, senderId, senderDeviceId, count) {
|
|
1334
|
+
this.ensureInitialized();
|
|
1335
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1336
|
+
const prefix = this.getSkippedSenderKeyPrefix(groupId, senderId, senderDeviceId);
|
|
1337
|
+
const matches = allKeys
|
|
1338
|
+
.filter((key) => key.startsWith(prefix))
|
|
1339
|
+
.map((key) => ({ key, chainIndex: parseInt(key.split(':').pop() ?? '0', 10) }))
|
|
1340
|
+
.sort((a, b) => a.chainIndex - b.chainIndex)
|
|
1341
|
+
.slice(0, count);
|
|
1342
|
+
await this.storageBackend.removeMany(matches.map(({ key }) => key));
|
|
1343
|
+
return matches.length;
|
|
1344
|
+
}
|
|
1345
|
+
// ============================================================================
|
|
1346
|
+
// Message Record Storage (Not Yet Implemented)
|
|
1347
|
+
// Retry records are indexed by the client timestamp assigned before encryption.
|
|
1348
|
+
// ============================================================================
|
|
1349
|
+
async storeMessageRecord(record) {
|
|
1350
|
+
this.ensureInitialized();
|
|
1351
|
+
await this.storageBackend.setItem(this.getMessageRecordStorageKey(record.sessionId, record.timestamp), await this.encrypt(JSON.stringify(record)));
|
|
1352
|
+
}
|
|
1353
|
+
async getMessageRecord(sessionId, timestamp) {
|
|
1354
|
+
this.ensureInitialized();
|
|
1355
|
+
const encrypted = await this.storageBackend.getItem(this.getMessageRecordStorageKey(sessionId, timestamp));
|
|
1356
|
+
if (!encrypted) {
|
|
1357
|
+
return null;
|
|
1358
|
+
}
|
|
1359
|
+
const decrypted = await this.decrypt(encrypted);
|
|
1360
|
+
return JSON.parse(decrypted);
|
|
1361
|
+
}
|
|
1362
|
+
async deleteMessageRecord(sessionId, timestamp) {
|
|
1363
|
+
this.ensureInitialized();
|
|
1364
|
+
await this.storageBackend.removeItem(this.getMessageRecordStorageKey(sessionId, timestamp));
|
|
1365
|
+
}
|
|
1366
|
+
async deleteExpiredMessageRecords(maxAgeMs) {
|
|
1367
|
+
this.ensureInitialized();
|
|
1368
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
1369
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1370
|
+
const messageKeys = allKeys.filter((key) => key.startsWith(STORAGE_KEYS.MESSAGE_RECORD_PREFIX));
|
|
1371
|
+
const toDelete = [];
|
|
1372
|
+
for (const key of messageKeys) {
|
|
1373
|
+
const encrypted = await this.storageBackend.getItem(key);
|
|
1374
|
+
if (!encrypted)
|
|
1375
|
+
continue;
|
|
1376
|
+
const decrypted = await this.decrypt(encrypted);
|
|
1377
|
+
const record = JSON.parse(decrypted);
|
|
1378
|
+
if (record.createdAt < cutoff) {
|
|
1379
|
+
toDelete.push(key);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
if (toDelete.length > 0) {
|
|
1383
|
+
await this.storageBackend.removeMany(toDelete);
|
|
1384
|
+
}
|
|
1385
|
+
return toDelete.length;
|
|
1386
|
+
}
|
|
1387
|
+
async clearAllMessageRecords() {
|
|
1388
|
+
this.ensureInitialized();
|
|
1389
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1390
|
+
const messageKeys = allKeys.filter((key) => key.startsWith(STORAGE_KEYS.MESSAGE_RECORD_PREFIX));
|
|
1391
|
+
if (messageKeys.length > 0) {
|
|
1392
|
+
await this.storageBackend.removeMany(messageKeys);
|
|
1393
|
+
}
|
|
1394
|
+
return messageKeys.length;
|
|
1395
|
+
}
|
|
1396
|
+
async deleteMessageRecordsForSession(sessionId) {
|
|
1397
|
+
this.ensureInitialized();
|
|
1398
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1399
|
+
const messageKeys = allKeys.filter((key) => key.startsWith(this.getMessageRecordSessionPrefix(sessionId)));
|
|
1400
|
+
if (messageKeys.length > 0) {
|
|
1401
|
+
await this.storageBackend.removeMany(messageKeys);
|
|
1402
|
+
}
|
|
1403
|
+
return messageKeys.length;
|
|
1404
|
+
}
|
|
1405
|
+
// ============================================================================
|
|
1406
|
+
// Key Recovery Methods (Bug #7 - Identifier Collision Recovery)
|
|
1407
|
+
// ============================================================================
|
|
1408
|
+
async getEcSignedPreKeyMaxId(identityType) {
|
|
1409
|
+
const prekeys = await this.getAllEcSignedPreKeys(identityType);
|
|
1410
|
+
return prekeys.reduce((max, prekey) => Math.max(max, prekey.keyId), 0);
|
|
1411
|
+
}
|
|
1412
|
+
async getKyberPreKeyMaxId(identityType) {
|
|
1413
|
+
const stored = await this.storageBackend.getItem(this.getKyberPreKeyStorageKey(identityType));
|
|
1414
|
+
if (!stored)
|
|
1415
|
+
return 0;
|
|
1416
|
+
const state = JSON.parse(await this.decrypt(stored));
|
|
1417
|
+
return Object.keys(state.instances).reduce((max, id) => Math.max(max, Number(id)), 0);
|
|
1418
|
+
}
|
|
1419
|
+
async deleteAllPreKeys(identityType) {
|
|
1420
|
+
this.ensureInitialized();
|
|
1421
|
+
const signedPreKeys = await this.getAllEcSignedPreKeys(identityType);
|
|
1422
|
+
const ecOneTimePreKeys = await this.getEcOneTimePreKeys(identityType);
|
|
1423
|
+
const kyberStateValue = await this.storageBackend.getItem(this.getKyberPreKeyStorageKey(identityType));
|
|
1424
|
+
const kyberPreKeyCount = kyberStateValue
|
|
1425
|
+
? Object.keys(JSON.parse(await this.decrypt(kyberStateValue)).instances).length
|
|
1426
|
+
: 0;
|
|
1427
|
+
const kemOneTimePreKeys = await this.getKemOneTimePreKeys(identityType);
|
|
1428
|
+
const it = identityType ?? 'aci';
|
|
1429
|
+
await this.storageBackend.removeMany([
|
|
1430
|
+
this.getSignedPreKeyStorageKey(identityType),
|
|
1431
|
+
this.getEcOneTimePreKeyStorageKey(identityType),
|
|
1432
|
+
this.getKyberPreKeyStorageKey(identityType),
|
|
1433
|
+
this.getKemOneTimePreKeyStorageKey(identityType),
|
|
1434
|
+
]);
|
|
1435
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1436
|
+
const kyberUsageKeys = allKeys.filter((key) => key.startsWith(`${STORAGE_KEYS.PREKEY_PREFIX}kyber-usage:${it}:`));
|
|
1437
|
+
if (kyberUsageKeys.length > 0) {
|
|
1438
|
+
await this.storageBackend.removeMany(kyberUsageKeys);
|
|
1439
|
+
}
|
|
1440
|
+
return {
|
|
1441
|
+
ecSignedPreKeys: signedPreKeys.length,
|
|
1442
|
+
ecOneTimePreKeys: ecOneTimePreKeys.length,
|
|
1443
|
+
kyberPreKeys: kyberPreKeyCount,
|
|
1444
|
+
kemOneTimePreKeys: kemOneTimePreKeys.length,
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
async clearAllSessions() {
|
|
1448
|
+
this.ensureInitialized();
|
|
1449
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1450
|
+
const sessionKeys = allKeys.filter((key) => key.startsWith(STORAGE_KEYS.SESSION_PREFIX) ||
|
|
1451
|
+
key.startsWith(STORAGE_KEYS.SESAME_USER_PREFIX));
|
|
1452
|
+
if (sessionKeys.length > 0) {
|
|
1453
|
+
await this.storageBackend.removeMany(sessionKeys);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
async getDetailedStats() {
|
|
1457
|
+
this.ensureInitialized();
|
|
1458
|
+
const allKeys = await this.storageBackend.getAllKeys();
|
|
1459
|
+
return {
|
|
1460
|
+
sessions: allKeys.filter((key) => key.startsWith(STORAGE_KEYS.SESSION_PREFIX)).length,
|
|
1461
|
+
ecSignedPreKeys: (await this.getAllEcSignedPreKeys('aci')).length +
|
|
1462
|
+
(await this.getAllEcSignedPreKeys('pni')).length,
|
|
1463
|
+
ecOneTimePreKeys: (await this.getEcOneTimePreKeys('aci')).length +
|
|
1464
|
+
(await this.getEcOneTimePreKeys('pni')).length,
|
|
1465
|
+
kyberPreKeys: ((await this.getKyberPreKey('aci')) ? 1 : 0) + ((await this.getKyberPreKey('pni')) ? 1 : 0),
|
|
1466
|
+
kemOneTimePreKeys: (await this.getKemOneTimePreKeys('aci')).length +
|
|
1467
|
+
(await this.getKemOneTimePreKeys('pni')).length,
|
|
1468
|
+
users: allKeys.filter((key) => key.startsWith(STORAGE_KEYS.SESAME_USER_PREFIX)).length,
|
|
1469
|
+
};
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
//# sourceMappingURL=adapter.js.map
|