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.
Add PortableDid, use existing UniFFI interfaces (etro-js#275)
- Loading branch information
1 parent
5a79dc7
commit 8350fc2
Showing
6 changed files
with
78 additions
and
57 deletions.
There are no files selected for viewing
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
21 changes: 1 addition & 20 deletions
21
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,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 | ||
} |
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,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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 | ||
|
||
/** | ||
* Constructs a PortableDid from a JSON string. | ||
* | ||
* @param json The JSON string. | ||
*/ | ||
constructor(json: String) { | ||
this.rustCorePortableDid = RustCorePortableDid(json) | ||
|
||
this.didUri = rustCorePortableDid.getData().didUri | ||
this.document = rustCorePortableDid.getData().document | ||
this.privateKeys = rustCorePortableDid.getData().privateJwks | ||
} | ||
} |
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,29 @@ | ||
package web5.sdk.dids | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.Assertions.assertDoesNotThrow | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import web5.sdk.rust.RustCoreException | ||
|
||
class PortableDidTest { | ||
@Test | ||
fun `can instantiate from json string`() { | ||
val jsonString = """ | ||
{"uri":"did:web:tbd.website%3A9002:alice","document":{"id":"did:web:tbd.website%3A9002:alice","@context":["https://www.w3.org/ns/did/v1"],"verificationMethod":[{"id":"did:web:tbd.website%3A9002:alice#key-0","type":"JsonWebKey","controller":"did:web:tbd.website%3A9002:alice","publicKeyJwk":{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","x":"NNoVSv_v34ombmylF572t9HYYDiJtMgfckRT1W0vW0g"}}]},"privateKeys":[{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","d":"SwuWbL-Fm64OUFy6x3FBt3RiB79RcnZZrllGT24m4BA","x":"NNoVSv_v34ombmylF572t9HYYDiJtMgfckRT1W0vW0g"}]} | ||
""".trimIndent() | ||
|
||
assertDoesNotThrow { | ||
val portableDid = PortableDid(jsonString) | ||
assertEquals("did:web:tbd.website%3A9002:alice", portableDid.didUri) | ||
} | ||
} | ||
|
||
@Test | ||
fun `instantiation from json string throws with invalid json string`() { | ||
val invalidJsonString = "something not valid" | ||
assertThrows(RustCoreException::class.java) { | ||
PortableDid(invalidJsonString) | ||
} | ||
} | ||
} |