Skip to content

Commit

Permalink
Merge pull request #10 from EdsonAlcala/feat/ethereum-json-serialisation
Browse files Browse the repository at this point in the history
wip: Feat/ethereum json serialisation
  • Loading branch information
EdsonAlcala authored Oct 28, 2024
2 parents 7980115 + d9585a9 commit eb62431
Show file tree
Hide file tree
Showing 12 changed files with 1,115 additions and 90 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ overflow-checks = true
[features]
default = ["all"]
all = ["near", "bitcoin", "evm"]
bitcoin = ["sha2", "schemars"]
bitcoin = ["sha2"]
evm = []
near = []

[dependencies]
rlp = "0.6.1"
hex = "0.4.3"
borsh = { version = "1.0.0", features = ["derive"] }
near-sdk = { version = "5.3.0" }
near-sdk = { version = "5.3.0", features = ["schemars"] }
near-account-id = { version = "1.0.0", features = ["schemars"] }
serde-big-array = "0.5.1"
bs58 = "0.5.1"
serde = "1.0"
serde_json = "1.0"
schemars = { version = "0.8" }
sha2 = { version = "0.10.8", optional = true }
schemars = { version = "0.8.11", optional = true }

[dev-dependencies]
# ethereum
Expand Down
6 changes: 6 additions & 0 deletions src/bitcoin/types/script_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ impl ScriptBuf {
pub const fn from_bytes(bytes: Vec<u8>) -> Self {
Self(bytes)
}

pub fn p2wpkh_script_code(&self) -> Self {
let mut script = vec![0x00, 0x14];
script.extend_from_slice(&self.0);
Self(script)
}
}

pub trait FromHex: Sized {
Expand Down
16 changes: 16 additions & 0 deletions src/bitcoin/types/tx_in/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ impl Sequence {
///
/// This sequence number enables replace-by-fee and absolute lock time.
pub const ZERO: Self = Self(0);

/// The sequence number that enables absolute lock time but disables replace-by-fee
/// and relative lock time.
pub const ENABLE_LOCKTIME_NO_RBF: Self = Self::MIN_NO_RBF;
/// The sequence number that enables replace-by-fee and absolute lock time but
/// disables relative lock time.
pub const ENABLE_RBF_NO_LOCKTIME: Self = Self(0xFFFFFFFD);

/// The lowest sequence number that does not opt-in for replace-by-fee.
///
/// A transaction is considered to have opted in to replacement of itself
/// if any of it's inputs have a `Sequence` number less than this value
/// (Explicit Signalling [BIP-125]).
///
/// [BIP-125]: <https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki]>
const MIN_NO_RBF: Self = Self(0xFFFFFFFE);
}

impl Default for Sequence {
Expand Down
3 changes: 2 additions & 1 deletion src/evm/evm_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use near_sdk::serde::{Deserialize, Serialize};
use rlp::RlpStream;
use schemars::JsonSchema;

use crate::constants::EIP_1559_TYPE;

use super::types::{AccessList, Address, Signature};
use super::utils::parse_eth_address;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct EVMTransaction {
pub chain_id: u64,
Expand Down
3 changes: 2 additions & 1 deletion src/evm/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use near_sdk::serde::{Deserialize, Serialize};
use schemars::JsonSchema;

pub type Address = [u8; 20];

pub type AccessList = Vec<(Address, Vec<[u8; 32]>)>;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct Signature {
pub v: u64,
Expand Down
3 changes: 2 additions & 1 deletion src/near/near_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{borsh, AccountId};
use schemars::JsonSchema;

use super::types::{Action, BlockHash, PublicKey, Signature, U64};

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize)]
#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, JsonSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct NearTransaction {
/// An account on which behalf transaction is signed
Expand Down
145 changes: 133 additions & 12 deletions src/near/types/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ use crate::near::types::PublicKey;
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::AccountId;
use schemars::JsonSchema;

use super::{U128, U64};

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub enum Action {
/// Create an (sub)account using a transaction `receiver_id` as an ID for
Expand All @@ -22,17 +33,47 @@ pub enum Action {
DeleteAccount(DeleteAccountAction),
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct CreateAccountAction {}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct DeployContractAction {
pub code: Vec<u8>,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct FunctionCallAction {
pub method_name: String,
Expand All @@ -41,13 +82,33 @@ pub struct FunctionCallAction {
pub deposit: U128,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct TransferAction {
pub deposit: U128,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct StakeAction {
/// Amount of tokens to stake.
Expand All @@ -56,7 +117,17 @@ pub struct StakeAction {
pub public_key: PublicKey,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct AddKeyAction {
/// A public key which will be associated with an access_key
Expand All @@ -65,7 +136,17 @@ pub struct AddKeyAction {
pub access_key: AccessKey,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct AccessKey {
/// Nonce for this access key, used for tx nonce generation. When access key is created, nonce
Expand All @@ -76,7 +157,17 @@ pub struct AccessKey {
pub permission: AccessKeyPermission,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub enum AccessKeyPermission {
FunctionCall(FunctionCallPermission),
Expand All @@ -85,22 +176,52 @@ pub enum AccessKeyPermission {
FullAccess,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct FunctionCallPermission {
pub allowance: Option<U128>,
pub receiver_id: String,
pub method_names: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct DeleteKeyAction {
/// A public key associated with the access_key to be deleted.
pub public_key: PublicKey,
}

#[derive(Serialize, Deserialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[derive(
Serialize,
Deserialize,
Debug,
Clone,
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
JsonSchema,
)]
#[serde(crate = "near_sdk::serde")]
pub struct DeleteAccountAction {
pub beneficiary_id: AccountId,
Expand Down
7 changes: 3 additions & 4 deletions src/near/types/block_hash.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Deserializer, Serialize};
use schemars::JsonSchema;
use serde::de;
use serde_big_array::BigArray;

#[derive(Serialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
#[serde(crate = "near_sdk::serde")]
pub struct BlockHash(#[serde(with = "BigArray")] pub [u8; 32]);
#[derive(Serialize, Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq, JsonSchema)]
pub struct BlockHash(pub [u8; 32]);

impl From<[u8; 32]> for BlockHash {
fn from(data: [u8; 32]) -> Self {
Expand Down
5 changes: 3 additions & 2 deletions src/near/types/integers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Deserializer, Serialize};
use schemars::JsonSchema;
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize, JsonSchema)]
pub struct U64(pub u64);

#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize, JsonSchema)]
pub struct U128(pub u128);

impl From<u64> for U64 {
Expand Down
Loading

0 comments on commit eb62431

Please sign in to comment.