This repository was archived by the owner on Jun 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
implement near tx signing in rust #1062
Merged
+145
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| use crate::signer::models::NearTransfer; | ||
| use crate::signer::signing; | ||
| use primitives::{ChainSigner, SignerError, SignerInput}; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct NearChainSigner; | ||
|
|
||
| impl ChainSigner for NearChainSigner { | ||
| fn sign_transfer(&self, input: &SignerInput, private_key: &[u8]) -> Result<String, SignerError> { | ||
| let transaction = NearTransfer::from_input(input)?; | ||
| signing::sign_transfer(&transaction, private_key) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use primitives::{TransactionFee, TransactionLoadInput}; | ||
|
|
||
| #[test] | ||
| fn test_sign_near_transfer() { | ||
| let private_key = bs58::decode("3hoMW1HvnRLSFCLZnvPzWeoGwtdHzke34B2cTHM8rhcbG3TbuLKtShTv3DvyejnXKXKBiV7YPkLeqUHN1ghnqpFv") | ||
| .into_vec() | ||
| .unwrap(); | ||
|
|
||
| let input = SignerInput::new( | ||
| TransactionLoadInput::mock_near("test.near", "whatever.near", "1", 1, "244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM"), | ||
| TransactionFee::new_from_fee(0.into()), | ||
| ); | ||
|
|
||
| let signed = NearChainSigner.sign_transfer(&input, &private_key[..32]).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| signed, | ||
| "CQAAAHRlc3QubmVhcgCRez0mjUtY9/7BsVC9aNab4+5dTMOYVeNBU4Rlu3eGDQEAAAAAAAAADQAAAHdoYXRldmVyLm5lYXIPpHP9JpAd8pa+atxMxN800EDvokNSJLaYaRDmMML+9gEAAAADAQAAAAAAAAAAAAAAAAAAAACWmoMzIYbul1Xkg5MlUlgG4Ymj0tK7S0dg6URD6X4cTyLe7vAFmo6XExAO2m4ZFE2n6KDvflObIHCLodjQIb0B" | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| mod chain_signer; | ||
| mod models; | ||
| mod serialization; | ||
| mod signing; | ||
|
|
||
| pub use chain_signer::NearChainSigner; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use primitives::{SignerError, SignerInput}; | ||
|
|
||
| pub struct NearTransfer { | ||
| pub signer_id: String, | ||
| pub receiver_id: String, | ||
| pub nonce: u64, | ||
| pub block_hash: [u8; 32], | ||
| pub deposit: [u8; 16], | ||
| } | ||
|
|
||
| impl NearTransfer { | ||
| pub fn from_input(input: &SignerInput) -> Result<Self, SignerError> { | ||
| let block_hash: [u8; 32] = bs58::decode(input.metadata.get_block_hash().map_err(SignerError::from_display)?) | ||
| .into_vec() | ||
| .map_err(|e| SignerError::invalid_input(format!("invalid NEAR block hash: {e}")))? | ||
| .try_into() | ||
| .map_err(|_| SignerError::invalid_input("NEAR block hash must be 32 bytes"))?; | ||
|
|
||
| Ok(Self { | ||
| signer_id: input.sender_address.clone(), | ||
| receiver_id: input.destination_address.clone(), | ||
| nonce: input.metadata.get_sequence().map_err(SignerError::from_display)?, | ||
| block_hash, | ||
| deposit: input.value.parse::<u128>().map_err(|_| SignerError::invalid_input("invalid NEAR amount"))?.to_le_bytes(), | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| use super::models::NearTransfer; | ||
|
|
||
| pub const ED25519_KEY_TYPE: u8 = 0; | ||
| const TRANSFER_ACTION: u8 = 3; | ||
|
|
||
| pub fn encode_transfer(transfer: &NearTransfer, public_key: &[u8; 32]) -> Vec<u8> { | ||
| let mut buf = Vec::with_capacity(128); | ||
| write_string(&mut buf, &transfer.signer_id); | ||
| buf.push(ED25519_KEY_TYPE); | ||
| buf.extend_from_slice(public_key); | ||
| buf.extend_from_slice(&transfer.nonce.to_le_bytes()); | ||
| write_string(&mut buf, &transfer.receiver_id); | ||
| buf.extend_from_slice(&transfer.block_hash); | ||
| // 1 action | ||
| buf.extend_from_slice(&1u32.to_le_bytes()); | ||
| buf.push(TRANSFER_ACTION); | ||
| buf.extend_from_slice(&transfer.deposit); | ||
| buf | ||
| } | ||
|
|
||
| fn write_string(buf: &mut Vec<u8>, value: &str) { | ||
| buf.extend_from_slice(&(value.len() as u32).to_le_bytes()); | ||
| buf.extend_from_slice(value.as_bytes()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use super::models::NearTransfer; | ||
| use super::serialization::{ED25519_KEY_TYPE, encode_transfer}; | ||
| use base64::{Engine, engine::general_purpose::STANDARD}; | ||
| use gem_hash::sha2::sha256; | ||
| use primitives::SignerError; | ||
| use signer::Ed25519KeyPair; | ||
|
|
||
| pub fn sign_transfer(transfer: &NearTransfer, private_key: &[u8]) -> Result<String, SignerError> { | ||
| let key_pair = Ed25519KeyPair::from_private_key(private_key)?; | ||
| let encoded = encode_transfer(transfer, &key_pair.public_key_bytes); | ||
| let digest = sha256(&encoded); | ||
| let signature = key_pair.sign(&digest); | ||
|
|
||
| let mut signed = encoded; | ||
| signed.push(ED25519_KEY_TYPE); | ||
| signed.extend_from_slice(&signature); | ||
| Ok(STANDARD.encode(&signed)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.