pub enum Signature {
Secp256k1: Secp256k1,
Secp256r1: Secp256r1,
Ed25519: Ed25519,
}
Expand description
An ECDSA signature.
Variants
Secp256k1: Secp256k1
Secp256r1: Secp256r1
Ed25519: Ed25519
Implementations
pub fn recover(
self,
message: Message,
) -> Result<PublicKey, SignatureError>
pub fn recover(
self,
message: Message,
) -> Result<PublicKey, SignatureError>
Recover the public key derived from the private key used to sign a message.
Returns a Result
to let the caller choose an error handling strategy.
Additional Information
Not applicable for Ed25519 signatures.
Arguments
message
: [Message] - The signed data.
Returns
- [Result<PublicKey, SignatureError>] - The recovered public key or an error.
Examples
use std::crypto::{Message, PublicKey, Secp256r1, Signature};
fn foo() {
let signature: Signature = Signature::Secp256r1(Secp256r1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
let public_key: PublicKey = PublicKey::from((
0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E,
0xC44415635160ACFC87A84300EED97928C949A2D958FC0947C535F7539C59AE75
));
// A recovered public key pair.
let result_public_key = signature.recover(message);
assert(result_public_key.is_ok());
assert(result_public_key.unwrap() == public_key);
}
pub fn address(
self,
message: Message,
) -> Result<Address, SignatureError>
pub fn address(
self,
message: Message,
) -> Result<Address, SignatureError>
Recover the address derived from the private key used to sign a message.
Returns a Result
to let the caller choose an error handling strategy.
Additional Information
Not applicable for Ed25519 signatures.
Arguments
message
: [Message] - The signed data.
Returns
- [Result<Address, SignatureError>] - The recovered Fuel address or an error.
Examples
use std::crypto::{Message, Secp256r1, Signature};
fn foo() {
let address = Address::from(0x7AAE2D980BE4C3275C72CE5B527FA23FFB97B766966559DD062E2B78FD9D3766);
let signature: Signature = Signature::Secp256r1(Secp256r1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
// A recovered Fuel address.
let result_address = signature.address(message);
assert(result_address.is_ok());
assert(result_address.unwrap() == address);
}
pub fn evm_address(
self,
message: Message,
) -> Result<EvmAddress, SignatureError>
pub fn evm_address(
self,
message: Message,
) -> Result<EvmAddress, SignatureError>
Recover the EVM address derived from the private key used to sign a message.
Returns a Result
to let the caller choose an error handling strategy.
Additional Information
Not applicable for Ed25519 signatures.
Arguments
message
: [Message] - The signed data.
Returns
- [Result<EvmAddress, SignatureError>] - The recovered evm address or an error.
Examples
use std::{vm::evm::evm_address::EvmAddress, crypto::{Signature, Secp256k1, Message}};
fn foo() {
let evm_address = EvmAddress::from(0x7AAE2D980BE4C3275C72CE5B527FA23FFB97B766966559DD062E2B78FD9D3766);
let signature: Signature = Signature::Secp256k1(Secp256k1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
// A recovered evm address.
let result_address = signature.evm_address(message).unwrap();
assert(result_address.is_ok());
assert(result_address.unwrap() == evm_address);
}
pub fn verify(
self,
public_key: PublicKey,
message: Message,
) -> Result<(), SignatureError>
pub fn verify(
self,
public_key: PublicKey,
message: Message,
) -> Result<(), SignatureError>
Verify that a signature matches given public key.
Arguments
public_key
: [PublicKey] - The public key to verify against.message
: Message - The signed data.
Returns
- [Result<(), SignatureError>] - An Ok result or an error.
Examples
use std::crypto::{Message, PublicKey, Secp256r1, Signature};
fn foo() {
let signature: Signature = Signature::Secp256r1(Secp256r1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
let public_key: PublicKey = PublicKey::from((
0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E,
0xC44415635160ACFC87A84300EED97928C949A2D958FC0947C535F7539C59AE75
));
// A valid result
let result = signature.verify(public_key, message);
assert(result.is_ok());
}
pub fn verify_address(
self,
address: Address,
message: Message,
) -> Result<(), SignatureError>
pub fn verify_address(
self,
address: Address,
message: Message,
) -> Result<(), SignatureError>
Verify that a signature matches given address.
Additional Information
Not applicable for Ed25519 signatures.
Arguments
address
: [Address] - The address to verify against.message
: Message - The signed data.
Returns
- [Result<(), SignatureError>] - An Ok result or an error.
Examples
use std::crypto::{Message, Secp256r1, Signature};
fn foo() {
let signature: Signature = Signature::Secp256r1(Secp256r1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
let address = Address::from(0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E);
// A valid result
let result = signature.verify_address(address, message);
assert(result.is_ok());
}
pub fn verify_evm_address(
self,
evm_address: EvmAddress,
message: Message,
) -> Result<(), SignatureError>
pub fn verify_evm_address(
self,
evm_address: EvmAddress,
message: Message,
) -> Result<(), SignatureError>
Verify that an signature matches given evm address.
Additional Information
Not applicable for Ed25519 signatures.
Arguments
evm_address
: [EvmAddress] - The evm address to verify against.message
: Message - The signed data.
Returns
- [Result<(), SignatureError>] - An Ok result or an error.
Examples
use std::{crypto::{Message, Secp256r1, Signature}, vm::evm::evm_address::EvmAddress};
fn foo() {
let signature: Signature = Signature::Secp256r1(Secp256r1::from((
0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
)));
let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
let evm_address = EvmAddress::from(0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E);
// A valid result
let result = signature.verify_evm_address(evm_address, message);
assert(result.is_ok());
}
pub fn as_secp256k1(self) -> Option<Secp256k1>
pub fn as_secp256k1(self) -> Option<Secp256k1>
Returns the Secp256k1
of the Signature
.
Returns
- [Option] -
Some(Secp256k1)
if the underlying type is anSecp256k1
, otherwiseNone
.
Examples
use std::crypto::{Signature, Secp256k1};
fn foo() {
let signature = Signature::Secp256k1(Secp256k1::new());
let secp256k1 = signature.as_secp256k1();
assert(secp256k1 == Secp256k1::new());
}
pub fn as_secp256r1(self) -> Option<Secp256r1>
pub fn as_secp256r1(self) -> Option<Secp256r1>
Returns the Secp256r1
of the Signature
.
Returns
- [Option] -
Some(Secp256r1)
if the underlying type is anSecp256r1
, otherwiseNone
.
Examples
use std::crypto::{Signature, Secp256r1};
fn foo() {
let signature = Signature::Secp256r1(Secp256r1::new());
let secp256r1 = signature.as_secp256k1();
assert(secp256r1 == Secp256r1::new());
}
pub fn as_ed25519(self) -> Option<Ed25519>
pub fn as_ed25519(self) -> Option<Ed25519>
Returns the Ed25519
of the Signature
.
Returns
- [Option] -
Some(Ed25519)
if the underlying type is anEd25519
, otherwiseNone
.
Examples
use std::crypto::{Signature, Ed25519};
fn foo() {
let signature = Signature::Ed25519(Ed25519::new());
let ed25519 = signature.as_secp256k1();
assert(ed25519 == Ed25519::new());
}
pub fn is_secp256k1(self) -> bool
pub fn is_secp256k1(self) -> bool
Returns whether the Signature
represents an Secp256k1
.
Returns
- [bool] - Indicates whether the
Signature
holds anSecp256k1
.
Examples
use std::crypto::{Signature, Secp256k1};
fn foo() {
let signature = Signature::Secp256k1(Secp256k1::new());
assert(signature.is_secp256k1());
}
pub fn is_secp256r1(self) -> bool
pub fn is_secp256r1(self) -> bool
Returns whether the Signature
represents an Secp256r1
.
Returns
- [bool] - Indicates whether the
Signature
holds anSecp256r1
.
Examples
use std::crypto::{Signature, Secp256r1};
fn foo() {
let signature = Signature::Secp256r1(Secp256r1::new());
assert(signature.is_secp256r1());
}
pub fn is_ed25519(self) -> bool
pub fn is_ed25519(self) -> bool
Returns whether the Signature
represents an Ed25519
.
Returns
- [bool] - Indicates whether the
Signature
holds anEd25519
.
Examples
use std::crypto::{Signature, Ed25519};
fn foo() {
let signature = Signature::Ed25519(Ed25519::new());
assert(signature.is_ed25519());
}
pub fn bits(self) -> [u8; 64]
pub fn bits(self) -> [u8; 64]
Returns the underlying raw [u8; 64]
data of the Signature.
Returns
- [[u8; 64]] - The raw data of the signature.
Examples
use std::crypto::{Signature, Ed25519};
fn foo() -> {
let my_signature = Signature::Ed25519(Ed25519::new());
assert(my_signature.bits()[0] == 0u8);
}