Skip to content

Commit

Permalink
Add sign_message, verify_sign, check_trust_layter_refund interface di…
Browse files Browse the repository at this point in the history
…finition to udl file
  • Loading branch information
rantan committed Nov 29, 2024
1 parent 6b87def commit 40166f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
30 changes: 15 additions & 15 deletions tapyrus-wallet-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ impl HdWallet {
txid: String,
color_id: String,
) -> Result<u64, CheckTrustLayerRefundError> {
let mut wallet = self.get_wallet();
let wallet = self.get_wallet();
let client = self.esplora_client();
let txid = txid
.parse::<MalFixTxid>()
Expand All @@ -900,7 +900,7 @@ impl HdWallet {
};

// filter outputs that send the color_id token to other wallet
let mut transfer_txouts = tx.output.iter().enumerate().filter(|(index, txout)| {
let mut transfer_txouts = tx.output.iter().enumerate().filter(|(_, txout)| {
// filter outputs that send the color_id token to other wallet
let output_color_id = txout.script_pubkey.color_id();
let script_pubkey = txout.script_pubkey.remove_color();
Expand All @@ -912,7 +912,7 @@ impl HdWallet {
// fold the amount of refund txout value that is sent back to the wallet
transfer_txouts.try_fold(
0u64,
|acc, (index, txout)| -> Result<u64, CheckTrustLayerRefundError> {
|acc, (index, _)| -> Result<u64, CheckTrustLayerRefundError> {
let output_status = client.get_output_status(&txid, index as u64).map_err(|e| {
CheckTrustLayerRefundError::EsploraClientError {
cause: e.to_string(),
Expand Down Expand Up @@ -961,8 +961,8 @@ impl HdWallet {

pub fn sign_message(
&self,
public_key: &String,
message: &String,
public_key: String,
message: String,
) -> Result<String, SignMessageError> {
let wallet = self.get_wallet();
let public_key = PublicKey::from_str(&public_key)
Expand All @@ -983,7 +983,7 @@ impl HdWallet {
.find_derivation_index_for_spk(wallet.secp_ctx(), &spk, 0..next_index)
.unwrap()
{
Some((index, desc)) => {
Some((index, _)) => {
let signers = wallet.get_signers(KeychainKind::External);
let key_map = signers.as_key_map(wallet.secp_ctx());

Expand All @@ -1007,11 +1007,11 @@ impl HdWallet {
}
}

fn verify_sign(
pub fn verify_sign(
&self,
public_key: &String,
message: &String,
sign: &String,
public_key: String,
message: String,
sign: String,
) -> Result<bool, VerifySignError> {
let public_key = PublicKey::from_str(&public_key)
.map_err(|_| VerifySignError::FailedToParsePublicKey)?;
Expand Down Expand Up @@ -1548,12 +1548,12 @@ mod test {
let wallet = get_wallet();
let message = "message".to_string();
let GetNewAddressResult { public_key, .. } = wallet.get_new_address(None).unwrap();
let sig = wallet.sign_message(&public_key, &message).unwrap();
let sig = wallet.sign_message(public_key.clone(), message.clone()).unwrap();

assert!(wallet.verify_sign(&public_key, &message, &sig).unwrap());
assert!(wallet.verify_sign(public_key.clone(), message.clone(), sig.clone()).unwrap());

let message = "another message".to_string();
assert!(!wallet.verify_sign(&public_key, &message, &sig).unwrap());
assert!(!wallet.verify_sign(public_key.clone(), message.clone(), sig.clone()).unwrap());
}

#[test]
Expand All @@ -1564,7 +1564,7 @@ mod test {
"039be0d2b0c3b6f7fad77f142257aee12b2a34047aa3191edc0424cd15e0fa15da".to_string();
assert_eq!(
Err(SignMessageError::PublicKeyNotFoundInWallet),
wallet.sign_message(&public_key, &message)
wallet.sign_message(public_key, message)
);
}

Expand All @@ -1578,7 +1578,7 @@ mod test {

assert_eq!(
Err(VerifySignError::FailedToParseSignature),
wallet.verify_sign(&public_key, &message, &invalid_sign)
wallet.verify_sign(public_key, message, invalid_sign)
);
}
}
46 changes: 46 additions & 0 deletions tapyrus-wallet-ffi/src/wallet.udl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,39 @@ interface UpdateContractError {
ContractError(string cause);
};

/// The error for HDWallet#sign_message
[Error]
interface SignMessageError {
/// Occur if the public key is invalid
FailedToParsePublicKey();
/// Occur if the public key is not found in the wallet
PublicKeyNotFoundInWallet();
};

/// The error for HDWallet#verify_sign
[Error]
interface VerifySignError {
/// Occur if the public key is invalid
FailedToParsePublicKey();
/// Occur if the signature format is invalid
FailedToParseSignature();
};

/// The error for HDWallet#check_trust_layer_refund
[Error]
interface CheckTrustLayerRefundError {
/// Occur if the txid is invalid
FailedToParseTxid(string txid);
/// Occur if the esplora client fails to connect
EsploraClientError(string cause);
/// Occur if the transaction is not found in the esplora
UnknownTxid();
/// Occur if the refund transaction is not found in the esplora
CannotFoundRefundTransaction(string txid);
/// Occur if the color id is invalid
InvalidColorId();
};

/// The HDWallet
interface HdWallet {
/// Create a new HDWallet instance
Expand Down Expand Up @@ -236,4 +269,17 @@ interface HdWallet {
/// Update the contract payable
[Throws=UpdateContractError]
void update_contract(string contract_id, boolean payable);

/// Sign the message
[Throws=SignMessageError]
string sign_message(string public_key, string message);
/// Verify the signature
[Throws=VerifySignError]
boolean verify_sign(string public_key, string message, string signature);

/// Check the trust layer refund transaction and return amount of the refund token
/// - txid: The transaction id to transfer token to email receiver wallet
/// - color_id: The color id of the token
[Throws=CheckTrustLayerRefundError]
u64 check_trust_layer_refund(string txid, string color_id);
};

0 comments on commit 40166f8

Please sign in to comment.