generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f42eee
commit 2afb464
Showing
11 changed files
with
963 additions
and
0 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod ed25519; | ||
pub mod secp256k1; | ||
|
||
use crate::errors::Result; | ||
use std::sync::Arc; | ||
|
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,44 @@ | ||
use super::{Signer, Verifier}; | ||
use crate::errors::Result; | ||
use web5::crypto::{ | ||
dsa::{ | ||
secp256k1::{ | ||
Secp256k1Generator as InnerSecp256k1Generator, Secp256k1Signer as InnerSecp256k1Signer, | ||
Secp256k1Verifier as InnerSecp256k1Verifier, | ||
}, | ||
Signer as InnerSigner, Verifier as InnerVerifier, | ||
}, | ||
jwk::Jwk, | ||
}; | ||
|
||
pub fn secp256k1_generator_generate() -> Jwk { | ||
InnerSecp256k1Generator::generate() | ||
} | ||
|
||
pub struct Secp256k1Signer(pub InnerSecp256k1Signer); | ||
|
||
impl Secp256k1Signer { | ||
pub fn new(private_jwk: Jwk) -> Self { | ||
Self(InnerSecp256k1Signer::new(private_jwk)) | ||
} | ||
} | ||
|
||
impl Signer for Secp256k1Signer { | ||
fn sign(&self, payload: Vec<u8>) -> Result<Vec<u8>> { | ||
Ok(self.0.sign(&payload)?) | ||
} | ||
} | ||
|
||
pub struct Secp256k1Verifier(pub InnerSecp256k1Verifier); | ||
|
||
impl Secp256k1Verifier { | ||
pub fn new(public_jwk: Jwk) -> Self { | ||
Self(InnerSecp256k1Verifier::new(public_jwk)) | ||
} | ||
} | ||
|
||
impl Verifier for Secp256k1Verifier { | ||
fn verify(&self, payload: Vec<u8>, signature: Vec<u8>) -> Result<()> { | ||
Ok(self.0.verify(&payload, &signature)?) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
bound/kt/src/main/kotlin/web5/sdk/crypto/Secp256k1Generator.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package web5.sdk.crypto | ||
|
||
import web5.sdk.Web5Exception | ||
import web5.sdk.crypto.keys.Jwk | ||
import web5.sdk.rust.secp256k1GeneratorGenerate | ||
import web5.sdk.rust.Web5Exception.Exception as RustCoreException | ||
|
||
/** | ||
* Generates private key material for secp256k1. | ||
*/ | ||
class Secp256k1Generator { | ||
companion object { | ||
/** | ||
* Generate the private key material; return Jwk includes private key material. | ||
* | ||
* @return Jwk the JWK with private key material included. | ||
*/ | ||
fun generate(): Jwk { | ||
try { | ||
val rustCoreJwkData = secp256k1GeneratorGenerate() | ||
return Jwk.fromRustCoreJwkData(rustCoreJwkData) | ||
} catch (e: RustCoreException) { | ||
throw Web5Exception.fromRustCore(e) | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Secp256k1Signer.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package web5.sdk.crypto.signers | ||
|
||
import web5.sdk.Web5Exception | ||
import web5.sdk.crypto.keys.Jwk | ||
import web5.sdk.rust.Secp256k1Signer as RustCoreSecp256k1Signer | ||
import web5.sdk.rust.Web5Exception.Exception as RustCoreException | ||
|
||
/** | ||
* Implementation of Signer for secp256k1. | ||
*/ | ||
class Secp256k1Signer(privateJwk: Jwk) : Signer { | ||
private val rustCoreSigner = RustCoreSecp256k1Signer(privateJwk.rustCoreJwkData) | ||
|
||
/** | ||
* Implementation of Signer's sign instance method for secp256k1. | ||
* | ||
* @param payload the data to be signed. | ||
* @return ByteArray the signature. | ||
*/ | ||
override fun sign(payload: ByteArray): ByteArray { | ||
try { | ||
return rustCoreSigner.sign(payload) | ||
} catch (e: RustCoreException) { | ||
throw Web5Exception.fromRustCore(e) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
bound/kt/src/main/kotlin/web5/sdk/crypto/verifiers/Secp256k1Verifier.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package web5.sdk.crypto.verifiers | ||
|
||
import web5.sdk.Web5Exception | ||
import web5.sdk.crypto.keys.Jwk | ||
import web5.sdk.rust.Secp256k1Verifier as RustCoreSecp256k1Verifier | ||
import web5.sdk.rust.Web5Exception.Exception as RustCoreException | ||
|
||
/** | ||
* Implementation of Verifier for secp256k1. | ||
*/ | ||
class Secp256k1Verifier(publicJwk: Jwk) : Verifier { | ||
private val rustCoreVerifier = RustCoreSecp256k1Verifier(publicJwk.rustCoreJwkData) | ||
|
||
/** | ||
* Implementation of Signer's verify instance method for secp256k1. | ||
* | ||
* @param message the data to be verified. | ||
* @param signature the signature to be verified. | ||
* @throws Web5Exception in the case of a failed verification | ||
*/ | ||
override fun verify(message: ByteArray, signature: ByteArray) { | ||
try { | ||
rustCoreVerifier.verify(message, signature) | ||
} catch (e: RustCoreException) { | ||
throw Web5Exception.fromRustCore(e) | ||
} | ||
} | ||
} |
Oops, something went wrong.