forked from etro-js/etro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bearer did wip * update * documentation * update * comply to bearerdid spec
- Loading branch information
1 parent
7e196fe
commit 1155fed
Showing
16 changed files
with
235 additions
and
114 deletions.
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,47 @@ | ||
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 | ||
|
||
/** | ||
* 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 | ||
|
||
private 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) | ||
} | ||
} |
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.