Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PortableDid, use existing UniFFI interfaces #275

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
}

/**
Expand Down
21 changes: 1 addition & 20 deletions bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 1 addition & 20 deletions bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
typealias Signer = RustCoreSigner
20 changes: 16 additions & 4 deletions bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,21 +31,34 @@ 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.
*
* @return Signer The signer for the DID.
*/
fun getSigner(): Signer {
val keyId = this.document.verificationMethod.first().id
val innerSigner = this.rustCoreBearerDid.getSigner(keyId)
return OuterSigner(innerSigner)
return this.rustCoreBearerDid.getSigner(keyId)
}
}
25 changes: 25 additions & 0 deletions bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt
Original file line number Diff line number Diff line change
@@ -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<Jwk>

internal val rustCorePortableDid: RustCorePortableDid

constructor(json: String) {
KendallWeihe marked this conversation as resolved.
Show resolved Hide resolved
this.rustCorePortableDid = RustCorePortableDid(json)

this.didUri = rustCorePortableDid.getData().didUri
this.document = rustCorePortableDid.getData().document
this.privateKeys = rustCorePortableDid.getData().privateJwks
}
}
Loading