generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Bearer Did Impl #247
Merged
Merged
Bearer Did Impl #247
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1b78de
bearer did wip
nitro-neal bc36ee0
update
nitro-neal 44eb694
documentation
nitro-neal 803d960
Merge branch 'main' into bearer-did-impl
nitro-neal f5347ca
update
nitro-neal b3ca53d
Merge branch 'main' into bearer-did-impl
nitro-neal 4a35fca
comply to bearerdid spec
nitro-neal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 36 additions & 15 deletions
51
bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,54 @@ | ||
package web5.sdk.crypto.keys | ||
|
||
import web5.sdk.crypto.signers.OuterSigner | ||
import web5.sdk.crypto.signers.Signer | ||
|
||
import web5.sdk.rust.InMemoryKeyManager as RustCoreInMemoryKeyManager | ||
import web5.sdk.rust.KeyManager as RustCoreKeyManager | ||
|
||
/** | ||
* A class for managing cryptographic keys in-memory. | ||
*/ | ||
class InMemoryKeyManager { | ||
private val rustCoreKeyManager = RustCoreInMemoryKeyManager() | ||
class InMemoryKeyManager : KeyManager { | ||
private val rustCoreInMemoryKeyManager = RustCoreInMemoryKeyManager() | ||
|
||
/** | ||
* Constructs an InMemoryKeyManager with the given private keys. | ||
* | ||
* @param privateJwks A list of private keys represented as JWKs (JSON Web Keys). | ||
*/ | ||
constructor(privateJwks: List<Jwk>) { | ||
privateJwks.forEach { | ||
this.rustCoreInMemoryKeyManager.importPrivateJwk(it) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Signer for the given public key. | ||
* | ||
* @param publicJwk The public key represented as a JWK. | ||
* @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 Ed25519Signer for the given public key. | ||
* Returns the RustCoreKeyManager. | ||
* | ||
* @param publicKey the public key represented as a Jwk. | ||
* @return Ed25519Signer the signer for the given public key. | ||
* @return RustCoreKeyManager The rust core key manager. | ||
*/ | ||
fun getSigner(publicKey: Jwk): Signer { | ||
return rustCoreKeyManager.getSigner(publicKey) | ||
override fun getRustCoreKeyManager(): RustCoreKeyManager { | ||
return this.rustCoreInMemoryKeyManager.getAsKeyManager() | ||
} | ||
|
||
/** | ||
* For importing private keys which may be stored somewhere such as environment variables. | ||
* Returns Jwk which is the public key for the given private key. | ||
* Imports a private key which may be stored somewhere such as environment variables. | ||
* | ||
* @param privateKey the private key represented as a Jwk. | ||
* @return Jwk the public key for the given private key. | ||
* @param privateJwk The private key represented as a JWK. | ||
* @return Jwk The public key represented as a JWK. | ||
*/ | ||
fun importPrivateKey(privateKey: Jwk): Jwk { | ||
return rustCoreKeyManager.importPrivateJwk(privateKey) | ||
fun importPrivateJwk(privateJwk: Jwk): Jwk { | ||
return this.rustCoreInMemoryKeyManager.importPrivateJwk(privateJwk) | ||
} | ||
} | ||
} |
17 changes: 5 additions & 12 deletions
17
bound/kt/src/main/kotlin/web5/sdk/crypto/keys/KeyManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
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 { | ||
/** | ||
* Generates new key material and returns the public key represented as a Jwk. | ||
* | ||
* @return Jwk The generated public key. | ||
*/ | ||
fun generateKeyMaterial(): Jwk | ||
|
||
/** | ||
* 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(publicKey: Jwk): Signer | ||
fun getSigner(publicJwk: Jwk): Signer | ||
|
||
/** | ||
* Imports a key which may be stored somewhere such as environment variables. | ||
* Returns the public key for the given private key. | ||
* Returns the RustCoreKeyManager | ||
* | ||
* @param privateKey The private key represented as a Jwk. | ||
* @return Jwk The public key for the given private key. | ||
* @return RustCoreKeyManager The rust core key manager | ||
*/ | ||
fun importKey(privateKey: Jwk): Jwk | ||
fun getRustCoreKeyManager(): RustCoreKeyManager | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Signer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
package web5.sdk.crypto.signers | ||
|
||
import web5.sdk.rust.SignerInterface as RustCoreSignerInterface | ||
import web5.sdk.rust.Signer as RustCoreSigner | ||
|
||
typealias Signer = RustCoreSignerInterface | ||
interface Signer { | ||
fun sign(payload: ByteArray): ByteArray | ||
} | ||
|
||
class OuterSigner: Signer { | ||
private val rustCoreSigner: RustCoreSigner | ||
|
||
constructor(rustCoreSigner: RustCoreSigner) { | ||
this.rustCoreSigner = rustCoreSigner | ||
} | ||
|
||
@OptIn(ExperimentalUnsignedTypes::class) | ||
override fun sign(payload: ByteArray): ByteArray { | ||
return this.rustCoreSigner.sign(payload.toUByteArray().toList()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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.BearerDid as RustCoreBearerDid | ||
|
||
import web5.sdk.rust.didJwkResolve as rustCoreDidJwkResolve | ||
import web5.sdk.rust.didWebResolve as rustCoreDidWebResolve | ||
import web5.sdk.rust.didDhtResolve as rustCoreDidDhtResolve | ||
|
||
/** | ||
* Represents a Decentralized Identifier (DID) along with its DID document, key manager, metadata, | ||
* and convenience functions. | ||
* | ||
* @property did The DID associated with this instance. | ||
* @property document The DID document associated with this instance. | ||
*/ | ||
class BearerDid { | ||
val did: Did | ||
val document: Document | ||
val keyManager: KeyManager | ||
|
||
private val rustCoreBearerDid: RustCoreBearerDid | ||
|
||
/** | ||
* Constructs a BearerDid instance using a DID URI and a key manager. | ||
* | ||
* @param uri The DID URI. | ||
* @param keyManager The key manager to handle keys. | ||
*/ | ||
constructor(uri: String, keyManager: KeyManager) { | ||
this.rustCoreBearerDid = RustCoreBearerDid(uri, keyManager.getRustCoreKeyManager()) | ||
|
||
this.did = this.rustCoreBearerDid.getData().did | ||
this.document = this.rustCoreBearerDid.getData().document | ||
this.keyManager = 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) | ||
} | ||
|
||
companion object { | ||
/** | ||
* Resolves a DID URI to a ResolutionResult. | ||
* | ||
* @param uri The DID URI to resolve. | ||
* @return ResolutionResult The result of the DID resolution. | ||
*/ | ||
@JvmStatic | ||
suspend fun resolve(uri: String): ResolutionResult { | ||
// TODO: Add a concept of a universal resolver - https://github.com/TBD54566975/web5-rs/issues/246 | ||
val method = uri.substringAfter(":").substringBefore(":") | ||
if(method == "jwk") { | ||
return rustCoreDidJwkResolve(uri).getData() | ||
} | ||
|
||
if(method == "dht") { | ||
return rustCoreDidDhtResolve(uri).getData() | ||
} | ||
|
||
if(method == "web") { | ||
return rustCoreDidWebResolve(uri).getData() | ||
} | ||
|
||
throw IllegalArgumentException("Unknown method '$method'") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's your thinking with this function? This isn't in the APID, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, I think got influenced by current impl in web5-kt.
That does beg the question though, how would we resovle a bearer did:
It would have to be like:
But how would you know the type, (the bearer did could be JWK..)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bearer DID could be resolvable via the
CONSTRUCTOR(uri: string, key_manager: KeyManager)
, because internal to that constructor would be three things:ResolutionResult(uri)
to resolve theDocument
Did(uri)
to parse theDid
key_manager
to the data memberI'm doing this in web5-r
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right genius, its already resolved basically and we have document right there :duhh: I'll update it