Skip to content

Commit

Permalink
Merge pull request #727 from ElrondNetwork/multisig-dns
Browse files Browse the repository at this point in the history
multisig dnsRegister
  • Loading branch information
andrei-marinica authored Jul 8, 2022
2 parents ea45941 + ccb43fd commit 7380de7
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 2 deletions.
4 changes: 4 additions & 0 deletions contracts/examples/multisig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ path = "src/multisig.rs"
version = "0.33.1"
path = "../../../elrond-wasm"

[dependencies.elrond-wasm-modules]
version = "0.33.1"
path = "../../../elrond-wasm-modules"

[dev-dependencies.elrond-wasm-debug]
version = "0.33.1"
path = "../../../elrond-wasm-debug"
Expand Down
4 changes: 4 additions & 0 deletions contracts/examples/multisig/interact-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ path = "src/multisig_interact.rs"
[dependencies.multisig]
path = ".."

[dependencies.elrond-wasm-modules]
version = "0.33.1"
path = "../../../../elrond-wasm-modules"

[dependencies.elrond-interaction]
version = "0.1.0"
path = "../../../../elrond-interaction"
17 changes: 16 additions & 1 deletion contracts/examples/multisig/interact-rs/src/multisig_interact.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod multisig_interact_nfts;

use elrond_interaction::{
dns_address_for_name,
elrond_wasm::{
elrond_codec::multi_types::MultiValueVec,
storage::mappers::SingleValue,
Expand All @@ -14,6 +14,7 @@ use elrond_interaction::{
erdrs::interactors::wallet::Wallet,
tokio, Interactor,
};
use elrond_wasm_modules::dns::ProxyTrait as _;
use multisig::{
multisig_perform::ProxyTrait as _, multisig_propose::ProxyTrait as _,
multisig_state::ProxyTrait as _, ProxyTrait as _,
Expand Down Expand Up @@ -49,6 +50,7 @@ async fn main() {
"nft-items" => state.create_items().await,
"quorum" => state.print_quorum().await,
"board" => state.print_board().await,
"dns-register" => state.dns_register().await,
_ => panic!("unknown command: {}", &cmd),
}
}
Expand Down Expand Up @@ -153,6 +155,19 @@ impl State {
println!(" {}", bech32::encode(board_member));
}
}

async fn dns_register(&mut self) {
let name = self.args.next().expect("name argument missing");
let dns_address = dns_address_for_name(&name);
let dns_register_call: ScCallStep = self
.multisig
.dns_register(dns_address, name)
.into_blockchain_call()
.from(&self.wallet_address)
.gas_limit("30,000,000")
.into();
self.interactor.sc_call(dns_register_call).await;
}
}

const SAVED_ADDRESS_FILE_NAME: &str = "multisig_address.txt";
Expand Down
1 change: 1 addition & 0 deletions contracts/examples/multisig/src/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub trait Multisig:
multisig_state::MultisigStateModule
+ multisig_propose::MultisigProposeModule
+ multisig_perform::MultisigPerformModule
+ elrond_wasm_modules::dns::DnsModule
{
#[init]
fn init(&self, quorum: usize, board: MultiValueEncoded<ManagedAddress>) {
Expand Down
1 change: 1 addition & 0 deletions contracts/examples/multisig/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ elrond_wasm_node::wasm_endpoints! {
(
deposit
discardAction
dnsRegister
getActionData
getActionLastIndex
getActionSignerCount
Expand Down
77 changes: 77 additions & 0 deletions elrond-interaction/src/interactor_dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use elrond_wasm_debug::{elrond_wasm::types::Address, mandos::value_interpreter::keccak256};

#[cfg(test)]
use elrond_wasm_debug::bech32;

fn get_initial_dns_address() -> Address {
Address::from_slice(&[1u8; 32])
}

fn compute_smart_contract_address(owner_address: Address, owner_nonce: u64) -> Address {
// 8 bytes of zero + 2 bytes for VM type + 20 bytes of hash(owner) + 2 bytes of shard(owner)
let owner_bytes = owner_address.as_bytes();
let nonce_bytes = owner_nonce.to_le_bytes();
let bytes_to_hash = [owner_bytes, &nonce_bytes].concat();
let initial_padding = [0u8; 8];
let vm_type: [u8; 2] = [5, 0];
let address = keccak256(&bytes_to_hash);
let address = [
initial_padding.as_slice(),
vm_type.as_slice(),
&address[10..30],
&owner_bytes[30..],
]
.concat();
Address::from_slice(&address)
}

fn compute_dns_address_for_shard_id(shard_id: u8) -> Address {
let initial_dns_address = get_initial_dns_address();
let initial_dns_address_slice = initial_dns_address.as_array();
let shard_identifier = &[0u8, shard_id];
let deployer_pubkey_prefix =
&initial_dns_address_slice[0..initial_dns_address_slice.len() - shard_identifier.len()];

let deployer_pubkey = [deployer_pubkey_prefix, shard_identifier].concat();
let deployer_address = Address::from_slice(&deployer_pubkey);
let deployer_nonce = 0;
compute_smart_contract_address(deployer_address, deployer_nonce)
}

fn shard_id_from_name(name: &str) -> u8 {
let name_hash = keccak256(name.as_bytes());
name_hash[31]
}

pub fn dns_address_for_name(name: &str) -> Address {
let shard_id = shard_id_from_name(name);
compute_dns_address_for_shard_id(shard_id)
}

#[test]
fn test_compute_dns_address() {
assert_eq!(
bech32::encode(&compute_dns_address_for_shard_id(0)),
"erd1qqqqqqqqqqqqqpgqnhvsujzd95jz6fyv3ldmynlf97tscs9nqqqq49en6w"
);
assert_eq!(
bech32::encode(&compute_dns_address_for_shard_id(1)),
"erd1qqqqqqqqqqqqqpgqysmcsfkqed279x6jvs694th4e4v50p4pqqqsxwywm0"
);
assert_eq!(
bech32::encode(&compute_dns_address_for_shard_id(2)),
"erd1qqqqqqqqqqqqqpgqnk5fq8sgg4vc63ffzf7qez550xe2l5jgqqpqe53dcq"
);
}

#[test]
fn test_dns_for_name() {
assert_eq!(
bech32::encode(&dns_address_for_name("test.elrond")),
"erd1qqqqqqqqqqqqqpgqx4ca3eu4k6w63hl8pjjyq2cp7ul7a4ukqz0skq6fxj"
);
assert_eq!(
bech32::encode(&dns_address_for_name("helloworld.elrond")),
"erd1qqqqqqqqqqqqqpgqhcm9k2xkk75e47wpmvfgj8fuzwaguvzyqqrqsteg8w"
);
}
2 changes: 2 additions & 0 deletions elrond-interaction/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod interactor;
mod interactor_dns;
mod interactor_result;
mod interactor_retrieve;
mod interactor_sc_call;
Expand All @@ -11,6 +12,7 @@ pub use elrond_wasm_debug::{self, elrond_wasm};
pub use env_logger;
pub use hex;
pub use interactor::*;
pub use interactor_dns::*;
pub use interactor_result::*;
pub use interactor_sender::*;
pub use log;
Expand Down
2 changes: 1 addition & 1 deletion mandos/src/value_interpreter/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sha3::{Digest, Keccak256};

const SC_ADDRESS_NUM_LEADING_ZEROS: usize = 8;

pub(crate) fn keccak256(data: &[u8]) -> Vec<u8> {
pub fn keccak256(data: &[u8]) -> Vec<u8> {
let mut hasher = Keccak256::new();
hasher.update(data);
let hash: [u8; 32] = hasher.finalize().into();
Expand Down
1 change: 1 addition & 0 deletions mandos/src/value_interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mod interpreter;
mod parse_num;
mod prefixes;

pub use functions::keccak256;
pub use interpreter::{interpret_string, interpret_subtree};

0 comments on commit 7380de7

Please sign in to comment.