Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Nov 30, 2023
1 parent adbef80 commit e992471
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
20 changes: 13 additions & 7 deletions node/src/actors/json_rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
use witnet_crypto::key::KeyPath;
use witnet_data_structures::{
chain::{
tapi::ActiveWips, Block, DataRequestOutput, Epoch, Hash, Hashable,
tapi::ActiveWips, Block, DataRequestOutput, Environment, Epoch, Hash, Hashable,
PublicKeyHash, RADType, StateMachine, SyncStatus,
},
transaction::Transaction,
Expand Down Expand Up @@ -1969,15 +1969,19 @@ pub async fn stake(params: Result<BuildStake, Error>) -> JsonRpcResult {
}

/// Create a stake authorization for the given address.
///
///
/// The output of this method is a required argument to call the Stake method.
/* test
{"jsonrpc": "2.0","method": "authorizeStake", "params": {"withdrawer":"wit000000000000000...1"}, "id": "1"}
*/
pub async fn authorize_stake(params: Result<AuthorizeStake, Error>) -> JsonRpcResult {
print!("Inside authorize_stake");
log::debug!("Creating an authorization stake from JSON-RPC.");

match params {
Ok(msg) => {
let pkh = PublicKeyHash::from_bech32(Environment::Mainnet, &msg.withdrawer).unwrap();
let mut data = [0u8; witnet_crypto::secp256k1::constants::MESSAGE_SIZE];
data[0..20].clone_from_slice(msg.withdrawer.as_ref());
data[0..20].clone_from_slice(pkh.as_ref());

signature_mngr::sign_data(data)
.map(|res| {
Expand Down Expand Up @@ -2385,14 +2389,16 @@ mod tests {
// {"jsonrpc":"1.0","method":"authorizeStake","params":{"withdrawer": "wit16dvdn4ypw9jmpgvu8w7vzch5qx79kgjkacrusj"},"id":1}
// Check that the inventory method accepts blocks
use witnet_data_structures::chain::*;
let params = AuthorizeStake {
withdrawer: PublicKeyHash::from_bech32(Environment::Mainnet, "wit16dvdn4ypw9jmpgvu8w7vzch5qx79kgjkacrusj").unwrap()
}; let msg = format!(
let params = AuthorizeStake {
withdrawer: String::from("wit16dvdn4ypw9jmpgvu8w7vzch5qx79kgjkacrusj"),
};
let msg = format!(
r#"{{"jsonrpc":"2.0","method":"authorizeStake","params":{},"id":1}}"#,
serde_json::to_string(&params).unwrap()
);
let expected = r#"{"jsonrpc":"2.0","result":{"public_key":{"bytes":[100,244,90,52,4,0,184,230,184,217,70,145,28,225,23,238,66,52,106,254,213,237,201,152,171,126,231,92,31,241,251,102],"compressed":3},"signature":{"Secp256k1":{"der":[48,69,2,33,0,240,5,92,61,31,222,226,164,208,51,115,183,140,129,73,163,46,44,170,95,91,239,105,246,220,204,231,198,61,82,158,223,2,32,123,85,76,252,227,110,106,69,170,135,36,180,166,6,212,42,73,191,203,226,101,28,147,75,242,168,197,11,95,83,206,38]}}},"id":"1"} "#.to_string();
let mut server = WittyMultiServer::new();
// TODO; create a system with a signature manager attached
attach_api(&mut server, true, Subscriptions::default(), &None);
let response = server.handle_request_sync(&msg, Default::default());
assert_eq!(response, Some(expected));
Expand Down
4 changes: 2 additions & 2 deletions node/src/actors/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ impl Message for BuildStake {
type Result = Result<StakeTransaction, failure::Error>;
}

/// Builds a `AuthorizeStake`
/// Builds an `AuthorizeStake`
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct AuthorizeStake {
/// Address that can withdraw the stake
// #[serde(default)]
pub withdrawer: PublicKeyHash,
pub withdrawer: String,
}

impl Message for AuthorizeStake {
Expand Down
7 changes: 4 additions & 3 deletions src/cli/node/json_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
use witnet_config::defaults::PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO;
use witnet_crypto::{
hash::calculate_sha256,
key::{ExtendedPK, ExtendedSK}
key::{ExtendedPK, ExtendedSK},
};
use witnet_data_structures::{
chain::{
Expand Down Expand Up @@ -979,13 +979,14 @@ pub fn send_st(
}

pub fn authorize_st(addr: SocketAddr, withdrawer: String) -> Result<(), failure::Error> {
let address: PublicKeyHash = PublicKeyHash::from_bech32(Environment::Mainnet, &withdrawer)?;
// TODO: validate withdrawer
PublicKeyHash::from_bech32(Environment::Mainnet, &withdrawer)?;

let mut stream = start_client(addr)?;
let mut id = SequentialId::initialize(1u8);

let params = AuthorizeStake {
withdrawer: address,
withdrawer: withdrawer,
};
let (authorization, (_, response)): (KeyedSignature, _) =
issue_method("authorizeStake", Some(params), &mut stream, id.next())?;
Expand Down

0 comments on commit e992471

Please sign in to comment.