-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,746 additions
and
19 deletions.
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 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 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 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 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 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 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 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,22 @@ | ||
[package] | ||
name = "near-wallet-contract" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
description = "A temporary Wallet Contract placeholder." | ||
repository.workspace = true | ||
license.workspace = true | ||
publish = false | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
near-vm-runner.workspace = true | ||
wat.workspace = true | ||
wasm-encoder.workspace = true | ||
wasm-smith.workspace = true | ||
|
||
[features] | ||
nightly = [] |
This file contains 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 @@ | ||
../../licenses/LICENSE-APACHE |
This file contains 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 @@ | ||
../../licenses/LICENSE-MIT |
This file contains 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,21 @@ | ||
A temporary (placeholder) implementation of the `Wallet Contract`. | ||
|
||
See https://github.com/near/NEPs/issues/518. | ||
|
||
Must not use in production! | ||
|
||
To use the contract you need to make sure that this crate was compiled. | ||
The contract is built via `build.rs` and WASM file is generated to the `./res` directory. | ||
You might want to `touch build.rs` before `cargo build` to force cargo to generate the WASM file. | ||
|
||
If you want to use the contract from rust core, add | ||
|
||
```toml | ||
[dev-dependencies] | ||
near-wallet-contract = { path = "../near-wallet-contract" } | ||
``` | ||
|
||
to the Cargo.toml and use `near_wallet_contract::wallet_contract()`. | ||
|
||
If you want to use a contract from an integration test, you can read | ||
the wasm file directly from the `./res` directory. |
This file contains 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,69 @@ | ||
use std::process::Command; | ||
|
||
type Error = Box<dyn std::error::Error>; | ||
|
||
fn main() { | ||
if let Err(err) = try_main() { | ||
eprintln!("{}", err); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
fn try_main() -> Result<(), Error> { | ||
build_contract("./wallet-contract", &["--features", "latest_protocol"], "wallet_contract")?; | ||
build_contract( | ||
"./wallet-contract", | ||
&["--features", "latest_protocol,nightly"], | ||
"nightly_wallet_contract", | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
fn build_contract(dir: &str, args: &[&str], output: &str) -> Result<(), Error> { | ||
let target_dir = out_dir(); | ||
|
||
let mut cmd = cargo_build_cmd(&target_dir); | ||
cmd.args(args); | ||
cmd.current_dir(dir); | ||
check_status(cmd)?; | ||
|
||
let src = | ||
target_dir.join(format!("wasm32-unknown-unknown/release/{}.wasm", dir.replace('-', "_"))); | ||
std::fs::copy(&src, format!("./res/{}.wasm", output)) | ||
.map_err(|err| format!("failed to copy `{}`: {}", src.display(), err))?; | ||
println!("cargo:rerun-if-changed=./{}/src/lib.rs", dir); | ||
println!("cargo:rerun-if-changed=./{}/Cargo.toml", dir); | ||
Ok(()) | ||
} | ||
|
||
fn cargo_build_cmd(target_dir: &std::path::Path) -> Command { | ||
let mut res = Command::new("cargo"); | ||
|
||
res.env_remove("CARGO_BUILD_RUSTFLAGS"); | ||
res.env_remove("CARGO_ENCODED_RUSTFLAGS"); | ||
res.env_remove("RUSTC_WORKSPACE_WRAPPER"); | ||
|
||
res.env("RUSTFLAGS", "-Dwarnings"); | ||
res.env("CARGO_TARGET_DIR", target_dir); | ||
|
||
res.args(["build", "--target=wasm32-unknown-unknown", "--release"]); | ||
|
||
res | ||
} | ||
|
||
fn check_status(mut cmd: Command) -> Result<(), Error> { | ||
cmd.status() | ||
.map_err(|err| format!("command `{cmd:?}` failed to run: {err}")) | ||
.and_then(|status| { | ||
if status.success() { | ||
Ok(()) | ||
} else { | ||
Err(format!("command `{cmd:?}` exited with non-zero status: {status:?}")) | ||
} | ||
}) | ||
.map_err(Error::from) | ||
} | ||
|
||
fn out_dir() -> std::path::PathBuf { | ||
std::env::var("OUT_DIR").unwrap().into() | ||
} |
Binary file not shown.
Binary file not shown.
This file contains 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,32 @@ | ||
#![doc = include_str!("../README.md")] | ||
|
||
use near_vm_runner::ContractCode; | ||
use std::path::Path; | ||
|
||
/// Temporary (placeholder) Wallet Contract. | ||
pub fn wallet_contract() -> ContractCode { | ||
read_contract("wallet_contract.wasm") | ||
} | ||
|
||
/// Temporary (placeholder) Wallet Contract that has access to all host functions from | ||
/// the nightly protocol. | ||
pub fn nightly_wallet_contract() -> ContractCode { | ||
read_contract("nightly_wallet_contract.wasm") | ||
} | ||
|
||
/// Read given wasm file or panic if unable to. | ||
fn read_contract(file_name: &str) -> ContractCode { | ||
let base = Path::new(env!("CARGO_MANIFEST_DIR")); | ||
let path = base.join("res").join(file_name); | ||
let code = match std::fs::read(&path) { | ||
Ok(data) => data, | ||
Err(err) => panic!("{}: {}", path.display(), err), | ||
}; | ||
ContractCode::new(code, None) | ||
} | ||
|
||
#[test] | ||
fn smoke_test() { | ||
assert!(!wallet_contract().code().is_empty()); | ||
assert!(!nightly_wallet_contract().code().is_empty()); | ||
} |
Oops, something went wrong.