From 73ec37410700fcb27b70bee6ef585908fcdcd14a Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Tue, 30 Jul 2024 17:50:22 -0400 Subject: [PATCH] Add PortableDid, use existing UniFFI interfaces --- .../sdk/crypto/keys/InMemoryKeyManager.kt | 14 +---------- .../kotlin/web5/sdk/crypto/keys/KeyManager.kt | 21 +--------------- .../kotlin/web5/sdk/crypto/signers/Signer.kt | 21 +--------------- .../main/kotlin/web5/sdk/dids/BearerDid.kt | 20 ++++++++++++--- .../main/kotlin/web5/sdk/dids/PortableDid.kt | 25 +++++++++++++++++++ 5 files changed, 44 insertions(+), 57 deletions(-) create mode 100644 bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt diff --git a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt index 8de76a03..095f0360 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt @@ -1,10 +1,8 @@ package web5.sdk.crypto.keys -import web5.sdk.crypto.signers.OuterSigner import web5.sdk.crypto.signers.Signer import web5.sdk.rust.SystemTarget import web5.sdk.rust.InMemoryKeyManager as RustCoreInMemoryKeyManager -import web5.sdk.rust.KeyManager as RustCoreKeyManager /** * A class for managing cryptographic keys in-memory. @@ -34,17 +32,7 @@ class InMemoryKeyManager : KeyManager { * @return Signer The signer for the given public key. */ override fun getSigner(publicJwk: Jwk): Signer { - val innerSigner = this.rustCoreInMemoryKeyManager.getSigner(publicJwk) - return OuterSigner(innerSigner) - } - - /** - * Returns the RustCoreKeyManager. - * - * @return RustCoreKeyManager The rust core key manager. - */ - override fun getRustCoreKeyManager(): RustCoreKeyManager { - return this.rustCoreInMemoryKeyManager.getAsKeyManager() + return this.rustCoreInMemoryKeyManager.getSigner(publicJwk) } /** diff --git a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt index d7fa07d5..d8c82a22 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt @@ -1,25 +1,6 @@ package web5.sdk.crypto.keys -import web5.sdk.crypto.signers.Signer import web5.sdk.rust.KeyManager as RustCoreKeyManager -/** - * An interface representing a key manager for cryptographic operations. - */ -interface KeyManager { +typealias KeyManager = RustCoreKeyManager - /** - * Returns the signer for the given public key. - * - * @param publicKey The public key represented as a Jwk. - * @return Signer The signer for the given public key. - */ - fun getSigner(publicJwk: Jwk): Signer - - /** - * Returns the RustCoreKeyManager - * - * @return RustCoreKeyManager The rust core key manager - */ - fun getRustCoreKeyManager(): RustCoreKeyManager -} diff --git a/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt b/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt index aa241c3b..413a9129 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt @@ -1,24 +1,5 @@ package web5.sdk.crypto.signers -import web5.sdk.rust.SystemTarget import web5.sdk.rust.Signer as RustCoreSigner -interface Signer { - fun sign(payload: ByteArray): ByteArray -} - -class OuterSigner: Signer { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - - private val rustCoreSigner: RustCoreSigner - - constructor(rustCoreSigner: RustCoreSigner) { - this.rustCoreSigner = rustCoreSigner - } - - override fun sign(payload: ByteArray): ByteArray { - return this.rustCoreSigner.sign(payload) - } -} \ No newline at end of file +typealias Signer = RustCoreSigner \ No newline at end of file diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt index adf92a29..09928ffc 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt @@ -2,7 +2,6 @@ package web5.sdk.dids import web5.sdk.crypto.signers.Signer import web5.sdk.crypto.keys.KeyManager -import web5.sdk.crypto.signers.OuterSigner import web5.sdk.rust.SystemTarget import web5.sdk.rust.BearerDid as RustCoreBearerDid @@ -32,13 +31,27 @@ class BearerDid { * @param keyManager The key manager to handle keys. */ constructor(uri: String, keyManager: KeyManager) { - this.rustCoreBearerDid = RustCoreBearerDid(uri, keyManager.getRustCoreKeyManager()) + this.rustCoreBearerDid = RustCoreBearerDid(uri, keyManager) this.did = this.rustCoreBearerDid.getData().did this.document = this.rustCoreBearerDid.getData().document this.keyManager = keyManager } + /** + * Constructs a BearerDid instance from a PortableDid. + * + * @param portableDid The PortableDid. + */ + constructor(portableDid: PortableDid) { + this.rustCoreBearerDid = RustCoreBearerDid.fromPortableDid(portableDid.rustCorePortableDid) + + val data = this.rustCoreBearerDid.getData() + this.did = data.did + this.document = data.document + this.keyManager = data.keyManager + } + /** * Returns a signer for the DID. * @@ -46,7 +59,6 @@ class BearerDid { */ fun getSigner(): Signer { val keyId = this.document.verificationMethod.first().id - val innerSigner = this.rustCoreBearerDid.getSigner(keyId) - return OuterSigner(innerSigner) + return this.rustCoreBearerDid.getSigner(keyId) } } diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt new file mode 100644 index 00000000..b3a98daa --- /dev/null +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt @@ -0,0 +1,25 @@ +package web5.sdk.dids + +import web5.sdk.crypto.keys.Jwk +import web5.sdk.rust.SystemTarget +import web5.sdk.rust.PortableDid as RustCorePortableDid + +class PortableDid { + init { + SystemTarget.set() // ensure the sys arch is set for first-time loading + } + + val didUri: String + val document: Document + val privateKeys: List + + internal val rustCorePortableDid: RustCorePortableDid + + constructor(json: String) { + this.rustCorePortableDid = RustCorePortableDid(json) + + this.didUri = rustCorePortableDid.getData().didUri + this.document = rustCorePortableDid.getData().document + this.privateKeys = rustCorePortableDid.getData().privateJwks + } +} \ No newline at end of file