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 8cd803d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
15 changes: 9 additions & 6 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 Down
34 changes: 4 additions & 30 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 @@ -23,35 +23,7 @@ fn main() -> Result<(), NEARLedgerError> {

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(public_key)
.into_iter()
.map(|action| action.try_into().unwrap())
.collect::<Vec<_>>();
Expand All @@ -76,5 +48,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 8cd803d

Please sign in to comment.