Skip to content

Commit

Permalink
chore: expand sign_nep_366_delegate_action to include all types of …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
dj8yf0μl committed Feb 6, 2024
1 parent 4cd0e56 commit 72fde37
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
23 changes: 13 additions & 10 deletions examples/common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ pub fn serialize_and_display_tx(transaction: near_primitives::transaction::Trans
log::info!("---");
bytes
}

pub fn display_and_verify_signature(
msg: Vec<u8>,
signature_bytes: Vec<u8>,
public_key: ed25519_dalek::PublicKey,
) {
pub fn display_signature(signature_bytes: Vec<u8>) -> ed25519_dalek::Signature {
log::info!("---");
log::info!("Signature:");
let signature = Signature::from_bytes(&signature_bytes).unwrap();
Expand All @@ -239,7 +234,15 @@ pub fn display_and_verify_signature(
.expect("Signature is not expected to fail on deserialization");
log::info!("{:<20} : {}", "signature (hex)", signature);
log::info!("{:<20} : {}", "signature (base58)", signature_near);
signature
}

pub fn display_and_verify_signature(
msg: Vec<u8>,
signature_bytes: Vec<u8>,
public_key: ed25519_dalek::PublicKey,
) {
let signature = display_signature(signature_bytes);
assert!(public_key
.verify(&CryptoHash::hash_bytes(&msg).as_ref(), &signature)
.is_ok());
Expand All @@ -253,15 +256,15 @@ where
env_logger::builder().init();
let hd_path = BIP32Path::from_str("44'/397'/0'/0'/1'").unwrap();

let public_key = near_ledger::get_public_key_with_display_flag(hd_path.clone(), false)?;
display_pub_key(public_key);
let ledger_pub_key = near_ledger::get_public_key_with_display_flag(hd_path.clone(), false)?;
display_pub_key(ledger_pub_key);

let unsigned_transaction = f_transaction(public_key);
let unsigned_transaction = f_transaction(ledger_pub_key);

let bytes = serialize_and_display_tx(unsigned_transaction);
let signature_bytes = near_ledger::sign_transaction(bytes.clone(), hd_path)?;

display_and_verify_signature(bytes, signature_bytes, public_key);
display_and_verify_signature(bytes, signature_bytes, ledger_pub_key);

Ok(())
}
45 changes: 10 additions & 35 deletions examples/sign_nep_366_delegate_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{convert::TryInto, str::FromStr};

use near_account_id::AccountId;
use near_crypto::{SecretKey, Signature};
use near_crypto::Signature;
use near_ledger::NEARLedgerError;
use near_primitives::action::delegate::{DelegateAction, SignedDelegateAction};
use slip10::BIP32Path;
Expand All @@ -15,54 +15,27 @@ fn main() -> Result<(), NEARLedgerError> {
env_logger::builder().init();

let hd_path = BIP32Path::from_str("44'/397'/0'/0'/1'").unwrap();
let public_key = near_ledger::get_public_key_with_display_flag(hd_path.clone(), false)?;
display_pub_key(public_key);
let ledger_pub_key = near_ledger::get_public_key_with_display_flag(hd_path.clone(), false)?;
display_pub_key(ledger_pub_key);

let delegate_public_key =
near_crypto::PublicKey::ED25519(near_crypto::ED25519PublicKey::from(public_key.to_bytes()));

let sender_id = AccountId::from_str("bob.near").unwrap();

let sk = SecretKey::from_seed(
near_crypto::KeyType::SECP256K1,
&format!("{:?}", public_key),
);
let public_key_secp = sk.public_key();

let transfer = near_primitives::transaction::Action::Transfer(
near_primitives::transaction::TransferAction {
deposit: 150000000000000000000000, // 0.15 NEAR
},
);

let stake = near_primitives::transaction::Action::Stake(Box::new(
near_primitives::transaction::StakeAction {
stake: 1157130000000000000000000, // 1.15713 NEAR,
public_key: public_key_secp.clone(),
},
));

let add_key_fullaccess = near_primitives::transaction::Action::AddKey(Box::new(
near_primitives::transaction::AddKeyAction {
public_key: public_key_secp.clone(),
access_key: near_primitives_core::account::AccessKey {
nonce: 127127127127,
permission: near_primitives_core::account::AccessKeyPermission::FullAccess,
},
},
));
let actions = vec![transfer, stake, add_key_fullaccess]
let actions = common::batch_of_all_types_of_actions(ledger_pub_key)
.into_iter()
.map(|action| action.try_into().unwrap())
.collect::<Vec<_>>();

let ledger_pub_key =
near_crypto::PublicKey::ED25519(near_crypto::ED25519PublicKey::from(ledger_pub_key.to_bytes()));

let delegate_action = DelegateAction {
sender_id,
receiver_id: AccountId::from_str("alice.near").unwrap(),
actions,
nonce: 127127122121,
max_block_height: 100500,
public_key: delegate_public_key,
public_key: ledger_pub_key,
};

let signature_bytes =
Expand All @@ -76,5 +49,7 @@ fn main() -> Result<(), NEARLedgerError> {
};
log::info!("{:#?}", signed_delegate);
assert!(signed_delegate.verify());

common::display_signature(signature_bytes);
Ok(())
}
2 changes: 1 addition & 1 deletion examples/sign_nep_413_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() -> Result<(), NEARLedgerError> {
display_pub_key(public_key);

let msg = NEP413Payload {
messsage: "Makes it possible to authenticate users without having to add new access keys. This will improve UX, save money and will not increase the on-chain storage of the users' accounts.".to_string(),
messsage: "Makes it possible to authenticate users without having to add new access keys. This will improve UX, save money and will not increase the on-chain storage of the users' accounts./Makes it possible to authenticate users without having to add new access keys. This will improve UX, save money and will not increase the on-chain storage of the users' accounts./Makes it possible to authenticate users without having to add new access keys. This will improve UX, save money and will not increase the on-chain storage of the users' accounts.".to_string(),
nonce: [42; 32],
recipient: "alice.near".to_string(),
callback_url: Some("myapp.com/callback".to_string())
Expand Down

0 comments on commit 72fde37

Please sign in to comment.