From c51eb87caf61e61558d5ab80eeb5a54b3a7c5d0e Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:37:03 -0400 Subject: [PATCH] Add support for keygen commands and some internal refactors --- .github/workflows/ci.yml | 64 +- Cargo.lock | 550 +++++++++++++++--- forc-plugins/forc-crypto/Cargo.toml | 4 +- forc-plugins/forc-crypto/src/address.rs | 3 + forc-plugins/forc-crypto/src/ecrecover.rs | 3 +- forc-plugins/forc-crypto/src/keccak256.rs | 1 + forc-plugins/forc-crypto/src/keygen/mod.rs | 11 + .../forc-crypto/src/keygen/new_key.rs | 66 +++ .../forc-crypto/src/keygen/parse_secret.rs | 61 ++ forc-plugins/forc-crypto/src/main.rs | 54 +- forc-plugins/forc-crypto/src/maybe_file.rs | 65 +++ forc-plugins/forc-crypto/src/sha256.rs | 1 + 12 files changed, 740 insertions(+), 143 deletions(-) create mode 100644 forc-plugins/forc-crypto/src/keygen/mod.rs create mode 100644 forc-plugins/forc-crypto/src/keygen/new_key.rs create mode 100644 forc-plugins/forc-crypto/src/keygen/parse_secret.rs create mode 100644 forc-plugins/forc-crypto/src/maybe_file.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d2fc966374..c90555f49c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -122,7 +122,7 @@ jobs: - name: Run mdbook build uses: peaceiris/actions-mdbook@v1 with: - mdbook-version: '0.4.25' + mdbook-version: "0.4.25" - name: Emit book logs to tmp.txt, fail if build logs contain 'ERROR' run: | MDBOOK_preprocessor__FORC_documenter__STRICT="true" mdbook build docs/book &> tmp.txt @@ -204,10 +204,14 @@ jobs: with: toolchain: ${{ env.RUST_VERSION }} - uses: Swatinem/rust-cache@v2 - - name: 'Build Workspace' + - name: Install Protoc + run: | + sudo apt-get update + sudo apt-get install protobuf-compiler + - name: "Build Workspace" run: cargo build --locked --workspace --all-features --all-targets env: - RUSTFLAGS: '-D warnings' + RUSTFLAGS: "-D warnings" cargo-clippy: runs-on: ubuntu-latest @@ -234,7 +238,7 @@ jobs: uses: baptiste0928/cargo-install@v1 with: crate: cargo-toml-lint - version: '0.1' + version: "0.1" - name: Run Cargo.toml linter run: git ls-files | grep Cargo.toml$ | grep -v 'templates/sway-test-rs' | xargs --verbose -n 1 cargo-toml-lint @@ -339,8 +343,8 @@ jobs: run: ./benchmark.sh --prepare-for-commit - uses: EndBug/add-and-commit@v9 with: - cwd: './performance-data' - message: 'Updated benchmark data' + cwd: "./performance-data" + message: "Updated benchmark data" default_author: github_actions forc-unit-tests: @@ -396,6 +400,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - name: Install Protoc + run: | + sudo apt-get update + sudo apt-get install protobuf-compiler - name: Install toolchain uses: dtolnay/rust-toolchain@master with: @@ -443,10 +451,10 @@ jobs: with: status: ${{ job.status }} token: ${{ secrets.GITHUB_TOKEN }} - notification_title: '{workflow} has {status_message}' - message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>' - footer: '' - notify_when: 'failure' + notification_title: "{workflow} has {status_message}" + message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>" + footer: "" + notify_when: "failure" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_NOTIFY_BUILD }} @@ -494,10 +502,10 @@ jobs: with: status: ${{ job.status }} token: ${{ secrets.GITHUB_TOKEN }} - notification_title: '{workflow} has {status_message}' - message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>' - footer: '' - notify_when: 'failure' + notification_title: "{workflow} has {status_message}" + message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>" + footer: "" + notify_when: "failure" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_NOTIFY_BUILD }} @@ -543,10 +551,10 @@ jobs: with: status: ${{ job.status }} token: ${{ secrets.GITHUB_TOKEN }} - notification_title: '{workflow} has {status_message}' - message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>' - footer: '' - notify_when: 'failure' + notification_title: "{workflow} has {status_message}" + message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>" + footer: "" + notify_when: "failure" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_NOTIFY_BUILD }} @@ -619,10 +627,10 @@ jobs: with: status: ${{ job.status }} token: ${{ secrets.GITHUB_TOKEN }} - notification_title: '{workflow} has {status_message}' - message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>' - footer: '' - notify_when: 'failure' + notification_title: "{workflow} has {status_message}" + message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>" + footer: "" + notify_when: "failure" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_NOTIFY_BUILD }} @@ -674,10 +682,10 @@ jobs: with: status: ${{ job.status }} token: ${{ secrets.GITHUB_TOKEN }} - notification_title: '{workflow} has {status_message}' - message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>' - footer: '' - notify_when: 'failure' + notification_title: "{workflow} has {status_message}" + message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> : <{run_url}|View Run Results>" + footer: "" + notify_when: "failure" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_NOTIFY_BUILD }} @@ -722,13 +730,13 @@ jobs: - uses: Swatinem/rust-cache@v2 with: cache-on-failure: true - key: '${{ matrix.job.target }}' + key: "${{ matrix.job.target }}" - name: Use Cross uses: baptiste0928/cargo-install@v1 with: crate: cross - cache-key: '${{ matrix.job.target }}' + cache-key: "${{ matrix.job.target }}" - name: Build forc binaries run: | diff --git a/Cargo.lock b/Cargo.lock index fc16f656396..ec12c36be1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,12 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" +[[package]] +name = "asn1_der" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "155a5a185e42c6b77ac7b88a15143d930a9e9727a5b7b77eed417404ab15c247" + [[package]] name = "assert-json-diff" version = "2.0.2" @@ -277,6 +283,12 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" +[[package]] +name = "base16ct" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" + [[package]] name = "base16ct" version = "0.2.0" @@ -462,6 +474,12 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + [[package]] name = "bs58" version = "0.5.0" @@ -722,10 +740,10 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b6be4a5df2098cd811f3194f64ddb96c267606bffd9689ac7b0160097b01ad3" dependencies = [ - "bs58", + "bs58 0.5.0", "coins-core", "digest 0.10.7", - "hmac", + "hmac 0.12.1", "k256", "serde", "sha2 0.10.8", @@ -740,10 +758,10 @@ checksum = "3db8fba409ce3dc04f7d804074039eb68b960b0829161f8e06c95fea3f122528" dependencies = [ "bitvec", "coins-bip32", - "hmac", + "hmac 0.12.1", "once_cell", "pbkdf2 0.12.2", - "rand", + "rand 0.8.5", "sha2 0.10.8", "thiserror", ] @@ -756,7 +774,7 @@ checksum = "5286a0843c21f8367f7be734f89df9b822e0321d8bcce8d6e735aadff7d74979" dependencies = [ "base64 0.21.4", "bech32", - "bs58", + "bs58 0.5.0", "digest 0.10.7", "generic-array", "hex", @@ -810,7 +828,7 @@ dependencies = [ "http", "mime", "mime_guess", - "rand", + "rand 0.8.5", "thiserror", ] @@ -1055,7 +1073,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "740fe28e594155f10cfc383984cbefd529d7396050557148f79cb0f621204124" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "subtle", "zeroize", ] @@ -1070,6 +1088,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "csv" version = "1.2.2" @@ -1109,6 +1137,19 @@ dependencies = [ "cipher", ] +[[package]] +name = "curve25519-dalek" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +dependencies = [ + "byteorder", + "digest 0.9.0", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + [[package]] name = "curve25519-dalek" version = "4.1.0" @@ -1285,6 +1326,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "der" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a467a65c5e759bce6e65eaf91cc29f466cdc57cb65777bd646872a8a1fd4de" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "der" version = "0.7.8" @@ -1445,12 +1496,21 @@ version = "0.16.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4b1e0c257a9e9f25f90ff76d7a68360ed497ee519c8e428d1825ef0000799d4" dependencies = [ - "der", + "der 0.7.8", "digest 0.10.7", "elliptic-curve", "rfc6979", - "signature", - "spki", + "signature 2.1.0", + "spki 0.7.2", +] + +[[package]] +name = "ed25519" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" +dependencies = [ + "signature 1.6.4", ] [[package]] @@ -1459,7 +1519,21 @@ version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60f6d271ca33075c88028be6f04d502853d63a5ece419d269c15315d4fc1cf1d" dependencies = [ - "signature", + "signature 2.1.0", +] + +[[package]] +name = "ed25519-dalek" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +dependencies = [ + "curve25519-dalek 3.2.0", + "ed25519 1.5.3", + "rand 0.7.3", + "serde", + "sha2 0.9.9", + "zeroize", ] [[package]] @@ -1468,8 +1542,8 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7277392b266383ef8396db7fdeb1e77b6c52fed775f5df15bb24f35b72156980" dependencies = [ - "curve25519-dalek", - "ed25519", + "curve25519-dalek 4.1.0", + "ed25519 2.2.2", "sha2 0.10.8", ] @@ -1497,15 +1571,15 @@ version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "968405c8fdc9b3bf4df0a6638858cc0b52462836ab6b1c87377785dd09cf1c0b" dependencies = [ - "base16ct", + "base16ct 0.2.0", "crypto-bigint", "digest 0.10.7", "ff", "generic-array", "group", - "pkcs8", - "rand_core", - "sec1", + "pkcs8 0.10.2", + "rand_core 0.6.4", + "sec1 0.7.3", "subtle", "zeroize", ] @@ -1600,9 +1674,9 @@ dependencies = [ "ctr", "digest 0.10.7", "hex", - "hmac", + "hmac 0.12.1", "pbkdf2 0.11.0", - "rand", + "rand 0.8.5", "scrypt", "serde", "serde_json", @@ -1704,7 +1778,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ - "rand_core", + "rand_core 0.6.4", "subtle", ] @@ -1754,7 +1828,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", - "rand", + "rand 0.8.5", "rustc-hex", "static_assertions", ] @@ -1838,7 +1912,7 @@ dependencies = [ "fuels-core", "futures", "hex", - "rand", + "rand 0.8.5", "rpassword", "serde", "serde_json", @@ -1857,17 +1931,17 @@ dependencies = [ "async-trait", "clap 3.2.25", "forc-tracing", + "fuel-core-types", "fuel-crypto", "fuels-core", "futures", "hex", - "rand", + "libp2p-core", + "rand 0.8.5", "serde", "serde_json", "sha2 0.10.8", "sha3", - "strum 0.25.0", - "strum_macros 0.25.2", "tokio", "tracing", ] @@ -1963,7 +2037,7 @@ dependencies = [ "fuel-abi-types 0.2.1", "fuel-tx", "fuel-vm", - "rand", + "rand 0.8.5", "rayon", "sway-core", "sway-types", @@ -2034,7 +2108,7 @@ dependencies = [ "futures", "hex", "home", - "rand", + "rand 0.8.5", "rpassword", "serde_json", "termion", @@ -2130,7 +2204,7 @@ checksum = "77ac38b692cf1d259c4576e96969ddc1b21880f3059744a730d1677b6f9fd4df" dependencies = [ "bitflags 1.3.2", "serde", - "strum 0.24.1", + "strum", ] [[package]] @@ -2146,7 +2220,7 @@ dependencies = [ "hex", "itertools", "postcard", - "rand", + "rand 0.8.5", "serde", "serde_json", "serde_with 1.14.0", @@ -2190,9 +2264,9 @@ dependencies = [ [[package]] name = "fuel-core-types" -version = "0.20.4" +version = "0.20.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ca102e8edbc81a2fd1e0701b8b9a2206fd3532ae3d4f0fd72ddadbdbb5a4962" +checksum = "01a004c665a6a522a7ae19c4b9127b9c9603bdc884d30df443bba119fd7484ca" dependencies = [ "anyhow", "derive_more", @@ -2214,11 +2288,11 @@ dependencies = [ "coins-bip32", "coins-bip39", "ecdsa", - "ed25519-dalek", + "ed25519-dalek 2.0.0", "fuel-types", "lazy_static", "p256", - "rand", + "rand 0.8.5", "secp256k1 0.26.0", "serde", "sha2 0.10.8", @@ -2265,7 +2339,7 @@ dependencies = [ "num-bigint", "pest", "pest_derive", - "rand", + "rand 0.8.5", "sha3", "snafu", ] @@ -2329,10 +2403,10 @@ dependencies = [ "fuel-types", "itertools", "num-integer", - "rand", + "rand 0.8.5", "serde", "serde_json", - "strum 0.24.1", + "strum", "strum_macros 0.24.3", ] @@ -2343,7 +2417,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d467a3b9deae49d7b4272b4a191b0e4b87c6ed9030a846c2d0d2c6394772832" dependencies = [ "hex", - "rand", + "rand 0.8.5", "serde", ] @@ -2366,7 +2440,7 @@ dependencies = [ "itertools", "paste", "primitive-types", - "rand", + "rand 0.8.5", "serde", "sha3", "tai64", @@ -2407,7 +2481,7 @@ dependencies = [ "fuels-core", "hex", "itertools", - "rand", + "rand 0.8.5", "serde", "sha2 0.10.8", "tai64", @@ -2454,7 +2528,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.8", - "strum 0.24.1", + "strum", "strum_macros 0.24.3", "thiserror", "uint", @@ -2472,7 +2546,7 @@ dependencies = [ "itertools", "proc-macro2", "quote", - "rand", + "rand 0.8.5", "regex", "serde_json", "syn 2.0.33", @@ -2495,12 +2569,12 @@ dependencies = [ "hex", "itertools", "proc-macro2", - "rand", + "rand 0.8.5", "regex", "serde", "serde_json", "sha2 0.10.8", - "strum 0.24.1", + "strum", "strum_macros 0.24.3", "thiserror", "tokio", @@ -2522,7 +2596,7 @@ dependencies = [ "futures", "hex", "portpicker", - "rand", + "rand 0.8.5", "serde", "serde_json", "serde_with 2.3.3", @@ -2577,6 +2651,7 @@ dependencies = [ "futures-core", "futures-task", "futures-util", + "num_cpus", ] [[package]] @@ -2608,6 +2683,12 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" +[[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" + [[package]] name = "futures-util" version = "0.3.28" @@ -2767,7 +2848,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ "ff", - "rand_core", + "rand_core 0.6.4", "subtle", ] @@ -2899,6 +2980,16 @@ dependencies = [ "serde", ] +[[package]] +name = "hmac" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126888268dcc288495a26bf004b38c5fdbb31682f992c84ceb046a1f0fe38840" +dependencies = [ + "crypto-mac", + "digest 0.9.0", +] + [[package]] name = "hmac" version = "0.12.1" @@ -2908,6 +2999,17 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "hmac-drbg" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ea0a1394df5b6574da6e0c1ade9e78868c9fb0a4e5ef4428e32da4676b85b1" +dependencies = [ + "digest 0.9.0", + "generic-array", + "hmac 0.8.1", +] + [[package]] name = "home" version = "0.5.5" @@ -3126,7 +3228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0acd33ff0285af998aaf9b57342af478078f53492322fafc47450e09397e0e9" dependencies = [ "bitmaps", - "rand_core", + "rand_core 0.6.4", "rand_xoshiro", "sized-chunks", "typenum", @@ -3306,7 +3408,7 @@ dependencies = [ "dirs 4.0.0", "futures", "http", - "multiaddr", + "multiaddr 0.17.1", "multibase", "serde", "serde_json", @@ -3380,7 +3482,7 @@ dependencies = [ "elliptic-curve", "once_cell", "sha2 0.10.8", - "signature", + "signature 2.1.0", ] [[package]] @@ -3441,6 +3543,89 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libp2p-core" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6a8fcd392ff67af6cc3f03b1426c41f7f26b6b9aff2dc632c1c56dd649e571f" +dependencies = [ + "asn1_der", + "bs58 0.4.0", + "ed25519-dalek 1.0.1", + "either", + "fnv", + "futures", + "futures-timer", + "instant", + "libsecp256k1", + "log", + "multiaddr 0.16.0", + "multihash 0.16.3", + "multistream-select", + "once_cell", + "parking_lot 0.12.1", + "pin-project", + "prost", + "prost-build", + "rand 0.8.5", + "rw-stream-sink", + "sec1 0.3.0", + "sha2 0.10.8", + "smallvec", + "thiserror", + "unsigned-varint", + "void", + "zeroize", +] + +[[package]] +name = "libsecp256k1" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b09eff1b35ed3b33b877ced3a691fc7a481919c7e29c53c906226fcf55e2a1" +dependencies = [ + "arrayref", + "base64 0.13.1", + "digest 0.9.0", + "hmac-drbg", + "libsecp256k1-core", + "libsecp256k1-gen-ecmult", + "libsecp256k1-gen-genmult", + "rand 0.8.5", + "serde", + "sha2 0.9.9", + "typenum", +] + +[[package]] +name = "libsecp256k1-core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" +dependencies = [ + "crunchy", + "digest 0.9.0", + "subtle", +] + +[[package]] +name = "libsecp256k1-gen-ecmult" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" +dependencies = [ + "libsecp256k1-core", +] + +[[package]] +name = "libsecp256k1-gen-genmult" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" +dependencies = [ + "libsecp256k1-core", +] + [[package]] name = "libssh2-sys" version = "0.3.0" @@ -3754,6 +3939,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "multiaddr" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aebdb21e90f81d13ed01dc84123320838e53963c2ca94b60b305d3fa64f31e" +dependencies = [ + "arrayref", + "byteorder", + "data-encoding", + "multibase", + "multihash 0.16.3", + "percent-encoding", + "serde", + "static_assertions", + "unsigned-varint", + "url", +] + [[package]] name = "multiaddr" version = "0.17.1" @@ -3784,6 +3987,19 @@ dependencies = [ "data-encoding-macro", ] +[[package]] +name = "multihash" +version = "0.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c346cf9999c631f002d8f977c4eaeaa0e6386f16007202308d0b3757522c2cc" +dependencies = [ + "core2", + "digest 0.10.7", + "multihash-derive", + "sha2 0.10.8", + "unsigned-varint", +] + [[package]] name = "multihash" version = "0.17.0" @@ -3826,6 +4042,26 @@ dependencies = [ "synstructure", ] +[[package]] +name = "multimap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" + +[[package]] +name = "multistream-select" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8552ab875c1313b97b8d20cb857b9fd63e2d1d6a0a1b53ce9821e575405f27a" +dependencies = [ + "bytes", + "futures", + "log", + "pin-project", + "smallvec", + "unsigned-varint", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -4273,7 +4509,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" dependencies = [ "digest 0.10.7", - "hmac", + "hmac 0.12.1", ] [[package]] @@ -4384,7 +4620,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" dependencies = [ "phf_shared", - "rand", + "rand 0.8.5", ] [[package]] @@ -4442,14 +4678,24 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkcs8" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" +dependencies = [ + "der 0.6.1", + "spki 0.6.0", +] + [[package]] name = "pkcs8" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" dependencies = [ - "der", - "spki", + "der 0.7.8", + "spki 0.7.2", ] [[package]] @@ -4512,7 +4758,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be97d76faf1bfab666e1375477b23fde79eccf0276e9b63b92a39d676a889ba9" dependencies = [ - "rand", + "rand 0.8.5", ] [[package]] @@ -4555,6 +4801,16 @@ dependencies = [ "structopt", ] +[[package]] +name = "prettyplease" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" +dependencies = [ + "proc-macro2", + "syn 1.0.109", +] + [[package]] name = "prettytable-rs" version = "0.8.0" @@ -4654,6 +4910,60 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270" +dependencies = [ + "bytes", + "heck 0.4.1", + "itertools", + "lazy_static", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "regex", + "syn 1.0.109", + "tempfile", + "which", +] + +[[package]] +name = "prost-derive" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "prost-types" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213622a1460818959ac1181aaeb2dc9c7f63df720db7d788b3e24eacd1983e13" +dependencies = [ + "prost", +] + [[package]] name = "psl-types" version = "2.0.11" @@ -4705,6 +5015,19 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" @@ -4712,8 +5035,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", ] [[package]] @@ -4723,7 +5056,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", ] [[package]] @@ -4735,13 +5077,22 @@ dependencies = [ "getrandom 0.2.10", ] +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "rand_xoshiro" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" dependencies = [ - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -4950,7 +5301,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" dependencies = [ - "hmac", + "hmac 0.12.1", "subtle", ] @@ -5193,6 +5544,17 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +[[package]] +name = "rw-stream-sink" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26338f5e09bb721b85b135ea05af7767c90b52f6de4f087d4f4a3a9d64e7dc04" +dependencies = [ + "futures", + "pin-project", + "static_assertions", +] + [[package]] name = "ryu" version = "1.0.15" @@ -5244,7 +5606,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f9e24d2b632954ded8ab2ef9fea0a0c769ea56ea98bddbafbad22caeeadf45d" dependencies = [ - "hmac", + "hmac 0.12.1", "pbkdf2 0.11.0", "salsa20", "sha2 0.10.8", @@ -5270,16 +5632,29 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sec1" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3be24c1842290c45df0a7bf069e0c268a747ad05a192f2fd7dcfdbc1cba40928" +dependencies = [ + "base16ct 0.1.1", + "der 0.6.1", + "generic-array", + "pkcs8 0.9.0", + "zeroize", +] + [[package]] name = "sec1" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ - "base16ct", - "der", + "base16ct 0.2.0", + "der 0.7.8", "generic-array", - "pkcs8", + "pkcs8 0.10.2", "subtle", "zeroize", ] @@ -5299,7 +5674,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4124a35fe33ae14259c490fd70fa199a32b9ce9502f2ee6bc4f81ec06fa65894" dependencies = [ - "rand", + "rand 0.8.5", "secp256k1-sys 0.8.1", ] @@ -5558,6 +5933,12 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" + [[package]] name = "signature" version = "2.1.0" @@ -5565,7 +5946,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" dependencies = [ "digest 0.10.7", - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -5678,6 +6059,16 @@ dependencies = [ "lock_api", ] +[[package]] +name = "spki" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" +dependencies = [ + "base64ct", + "der 0.6.1", +] + [[package]] name = "spki" version = "0.7.2" @@ -5685,7 +6076,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d1e996ef02c474957d681f1b05213dfb0abab947b446a62d37770b23500184a" dependencies = [ "base64ct", - "der", + "der 0.7.8", ] [[package]] @@ -5751,12 +6142,6 @@ dependencies = [ "strum_macros 0.24.3", ] -[[package]] -name = "strum" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" - [[package]] name = "strum_macros" version = "0.21.1" @@ -5782,19 +6167,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "strum_macros" -version = "0.25.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad8d03b598d3d0fff69bf533ee3ef19b8eeb342729596df84bcc7e1f96ec4059" -dependencies = [ - "heck 0.4.1", - "proc-macro2", - "quote", - "rustversion", - "syn 2.0.33", -] - [[package]] name = "substrate-bn" version = "0.6.0" @@ -5804,7 +6176,7 @@ dependencies = [ "byteorder", "crunchy", "lazy_static", - "rand", + "rand 0.8.5", "rustc-hex", ] @@ -5854,7 +6226,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.9.9", - "strum 0.24.1", + "strum", "sway-ast", "sway-error", "sway-ir", @@ -6236,7 +6608,7 @@ dependencies = [ "hex", "miden", "prettydiff 0.6.4", - "rand", + "rand 0.8.5", "regex", "revm", "serde_json", @@ -6350,10 +6722,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62cc94d358b5a1e84a5cb9109f559aa3c4d634d2b1b4de3d0fa4adc7c78e2861" dependencies = [ "anyhow", - "hmac", + "hmac 0.12.1", "once_cell", "pbkdf2 0.11.0", - "rand", + "rand 0.8.5", "rustc-hash", "sha2 0.10.8", "thiserror", diff --git a/forc-plugins/forc-crypto/Cargo.toml b/forc-plugins/forc-crypto/Cargo.toml index 9a746e141ba..a6364cd184a 100644 --- a/forc-plugins/forc-crypto/Cargo.toml +++ b/forc-plugins/forc-crypto/Cargo.toml @@ -15,16 +15,16 @@ anyhow = "1.0.75" async-trait = "0.1.58" clap = { version = "3", features = ["derive", "env"] } forc-tracing = { version = "0.46.0", path = "../../forc-tracing" } +fuel-core-types = { version = "0.20.5" } fuel-crypto = { workspace = true } fuels-core = { workspace = true } futures = "0.3" hex = "0.4.3" +libp2p-core = { version = "0.38", features = ["secp256k1"] } rand = "0.8" serde = "1.0" serde_json = "1" sha2 = "0.10.8" sha3 = "0.10.8" -strum = "0.25.0" -strum_macros = "0.25.2" tokio = { version = "1.8", features = ["macros", "rt-multi-thread", "process"] } tracing = "0.1" diff --git a/forc-plugins/forc-crypto/src/address.rs b/forc-plugins/forc-crypto/src/address.rs index e9cd01d2b61..bdbf4d80746 100644 --- a/forc-plugins/forc-crypto/src/address.rs +++ b/forc-plugins/forc-crypto/src/address.rs @@ -3,6 +3,9 @@ use fuel_crypto::fuel_types::Address; use fuels_core::types::bech32::Bech32Address; use std::str::FromStr; +/// Takes a valid address in any supported format and returns them in all +/// supported format. This is meant to be a tool that can be used to convert any +/// address format to all other formats pub fn dump_address>(data: T) -> anyhow::Result { let bytes_32: Result<[u8; 32], _> = data.as_ref().try_into(); let (bech32, addr) = if let Ok(bytes) = bytes_32 { diff --git a/forc-plugins/forc-crypto/src/ecrecover.rs b/forc-plugins/forc-crypto/src/ecrecover.rs index af7de56f015..4fa661be8e8 100644 --- a/forc-plugins/forc-crypto/src/ecrecover.rs +++ b/forc-plugins/forc-crypto/src/ecrecover.rs @@ -1,6 +1,7 @@ -use fuel_crypto::Signature; +use fuel_crypto::{secp256r1::recover, Signature}; pub fn ecrecover>(data: T) -> anyhow::Result> { + //recover(signature, message); let bytes = <[u8; Signature::LEN]>::try_from(data.as_ref()).expect("invalid input"); let _ = Signature::from_bytes(bytes); todo!() diff --git a/forc-plugins/forc-crypto/src/keccak256.rs b/forc-plugins/forc-crypto/src/keccak256.rs index d8617c76c19..27989f2cb14 100644 --- a/forc-plugins/forc-crypto/src/keccak256.rs +++ b/forc-plugins/forc-crypto/src/keccak256.rs @@ -1,5 +1,6 @@ use sha3::{Digest, Keccak256}; +/// Hashes a given data using Keccak256 pub fn hash>(data: T) -> anyhow::Result> { let mut hasher = Keccak256::new(); hasher.update(data); diff --git a/forc-plugins/forc-crypto/src/keygen/mod.rs b/forc-plugins/forc-crypto/src/keygen/mod.rs new file mode 100644 index 00000000000..c9b764c8e74 --- /dev/null +++ b/forc-plugins/forc-crypto/src/keygen/mod.rs @@ -0,0 +1,11 @@ +use clap::ValueEnum; + +pub mod new_key; +pub mod parse_secret; + +#[derive(Clone, Debug, Default, ValueEnum)] +pub enum KeyType { + #[default] + BlockProduction, + Peering, +} diff --git a/forc-plugins/forc-crypto/src/keygen/new_key.rs b/forc-plugins/forc-crypto/src/keygen/new_key.rs new file mode 100644 index 00000000000..c04653a191a --- /dev/null +++ b/forc-plugins/forc-crypto/src/keygen/new_key.rs @@ -0,0 +1,66 @@ +//! This file is migrated from https://github.com/FuelLabs/fuel-core/blob/master/bin/keygen/src/keygen.rs +use super::KeyType; +use anyhow::Result; +use fuel_core_types::{ + fuel_crypto::{ + rand::{prelude::StdRng, SeedableRng}, + SecretKey, + }, + fuel_tx::Input, +}; +use libp2p_core::{ + identity::{secp256k1, Keypair}, + PeerId, +}; +use serde_json::json; +use std::ops::Deref; + +/// Generate a random new secret & public key in the format expected by fuel-core +#[derive(Debug, clap::Args)] +#[clap(author, version, about = "Creates a new key for use with fuel-core")] +pub struct Arg { + #[clap(long = "pretty", short = 'p')] + pretty: bool, + #[clap( + long = "key-type", + short = 'k', + value_enum, + default_value = "block-production" + )] + key_type: KeyType, +} + +pub fn handler(arg: Arg) -> Result { + let mut rng = StdRng::from_entropy(); + let secret = SecretKey::random(&mut rng); + let public_key = secret.public_key(); + let secret_str = secret.to_string(); + + let output = match arg.key_type { + KeyType::BlockProduction => { + let address = Input::owner(&public_key); + json!({ + "secret": secret_str, + "address": address, + "type": "block_production" + }) + } + KeyType::Peering => { + let mut bytes = *secret.deref(); + let p2p_secret = secp256k1::SecretKey::from_bytes(&mut bytes) + .expect("Should be a valid private key"); + let libp2p_keypair = Keypair::Secp256k1(p2p_secret.into()); + let peer_id = PeerId::from_public_key(&libp2p_keypair.public()); + json!({ + "secret": secret_str, + "peer_id": peer_id.to_string(), + "type": "p2p" + }) + } + }; + Ok(if arg.pretty { + serde_json::to_string_pretty(&output) + } else { + serde_json::to_string(&output) + }?) +} diff --git a/forc-plugins/forc-crypto/src/keygen/parse_secret.rs b/forc-plugins/forc-crypto/src/keygen/parse_secret.rs new file mode 100644 index 00000000000..6c17ccf6678 --- /dev/null +++ b/forc-plugins/forc-crypto/src/keygen/parse_secret.rs @@ -0,0 +1,61 @@ +//! This file is migrated from https://github.com/FuelLabs/fuel-core/blob/master/bin/keygen/src/keygen.rs +use super::KeyType; +use anyhow::Result; +use fuel_core_types::{fuel_crypto::SecretKey, fuel_tx::Input}; +use libp2p_core::{ + identity::{secp256k1, Keypair}, + PeerId, +}; +use serde_json::json; +use std::{ops::Deref, str::FromStr}; + +/// Parse a secret key to view the associated public key +#[derive(Debug, clap::Args)] +#[clap( + author, + version, + about = "Parses a private key to view the associated public key" +)] +pub struct Arg { + secret: String, + #[clap(long = "pretty", short = 'p')] + pretty: bool, + #[clap( + long = "key-type", + short = 'k', + value_enum, + default_value = "block-production" + )] + key_type: KeyType, +} + +pub fn handler(arg: Arg) -> Result { + let secret = SecretKey::from_str(&arg.secret)?; + let output = match arg.key_type { + KeyType::BlockProduction => { + let address = Input::owner(&secret.public_key()); + let output = json!({ + "address": address.to_string(), + "type": "block-production", + }); + output + } + KeyType::Peering => { + let mut bytes = *secret.deref(); + let p2p_secret = secp256k1::SecretKey::from_bytes(&mut bytes) + .expect("Should be a valid private key"); + let libp2p_keypair = Keypair::Secp256k1(p2p_secret.into()); + let peer_id = PeerId::from_public_key(&libp2p_keypair.public()); + let output = json!({ + "peer_id": peer_id.to_string(), + "type": "p2p" + }); + output + } + }; + Ok(if arg.pretty { + serde_json::to_string_pretty(&output) + } else { + serde_json::to_string(&output) + }?) +} diff --git a/forc-plugins/forc-crypto/src/main.rs b/forc-plugins/forc-crypto/src/main.rs index a7c1675bc1f..c3f36f2e9a7 100644 --- a/forc-plugins/forc-crypto/src/main.rs +++ b/forc-plugins/forc-crypto/src/main.rs @@ -4,35 +4,41 @@ use anyhow::Result; use clap::Parser; use forc_tracing::{init_tracing_subscriber, println_error}; use std::default::Default; -use strum_macros::EnumString; mod address; -mod content; -mod ecrecover; mod keccak256; +mod keygen; +mod maybe_file; mod sha256; -#[derive(Debug, Clone, PartialEq, EnumString)] -pub enum Algorithm { - #[strum(ascii_case_insensitive)] - Keccak256, - #[strum(ascii_case_insensitive)] - Sha256, - #[strum(ascii_case_insensitive)] - EcRecover, - #[strum(ascii_case_insensitive)] - Address, +#[derive(Debug, clap::Args)] +#[clap(author, version, about = "Hashes the argument or file with this hash")] +pub struct HashArgs { + pub content: maybe_file::Content, } -#[derive(Debug, Clone, Parser)] +#[derive(Debug, clap::Args)] +#[clap( + author, + version, + about = "Converts any valid address to all supported formats" +)] +pub struct AddressArgs { + pub content: String, +} + +#[derive(Debug, Parser)] #[clap( name = "forc-crypto", about = "Forc plugin for hashing arbitrary data.", version )] -pub struct App { - pub algorithm: Algorithm, - pub content: content::Content, +pub enum Command { + Keccak256(HashArgs), + Sha256(HashArgs), + Address(AddressArgs), + NewKey(keygen::new_key::Arg), + ParseSecret(keygen::parse_secret::Arg), } fn main() { @@ -44,12 +50,14 @@ fn main() { } fn run() -> Result<()> { - let app = App::parse(); - let content = match app.algorithm { - Algorithm::Keccak256 => hex::encode(keccak256::hash(app.content)?), - Algorithm::Sha256 => hex::encode(sha256::hash(app.content)?), - Algorithm::EcRecover => hex::encode(ecrecover::ecrecover(app.content)?), - Algorithm::Address => address::dump_address(app.content)?, + let app = Command::parse(); + let content = match app { + Command::Keccak256(arg) => hex::encode(keccak256::hash(arg.content)?), + Command::Sha256(arg) => hex::encode(sha256::hash(arg.content)?), + //Command::E => hex::encode(ecrecover::ecrecover(app.content)?), + Command::Address(arg) => address::dump_address(arg.content)?, + Command::NewKey(arg) => keygen::new_key::handler(arg)?, + Command::ParseSecret(arg) => keygen::parse_secret::handler(arg)?, }; println!("{}", content); diff --git a/forc-plugins/forc-crypto/src/maybe_file.rs b/forc-plugins/forc-crypto/src/maybe_file.rs new file mode 100644 index 00000000000..f690a1d49bf --- /dev/null +++ b/forc-plugins/forc-crypto/src/maybe_file.rs @@ -0,0 +1,65 @@ +use std::str::FromStr; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::{ + fs::read, + io::{self, Read}, +}; + +static STDIN_HAS_BEEN_USED: AtomicBool = AtomicBool::new(false); + +/// Pretty simple parser for a string that can be a path, "-" (to read content +/// from stdin), a hex string or a raw string. +#[derive(Clone, Debug, PartialEq)] +pub enum Content { + Path(std::path::PathBuf, Vec), + Raw(Vec), +} + +impl Content { + pub fn from_hex_or_raw(input: Vec) -> Vec { + if let Ok(text) = String::from_utf8(input.clone()) { + let text = text.trim(); + if let Some(text) = text.strip_prefix("0x") { + if let Ok(bin) = hex::decode(text) { + return bin; + } + } + text.as_bytes().to_vec() + } else { + input + } + } +} + +impl FromStr for Content { + type Err = anyhow::Error; + + fn from_str(s: &str) -> std::result::Result { + if s == "-" { + if STDIN_HAS_BEEN_USED.load(Ordering::Acquire) { + let mut buffer = vec![]; + let _ = io::stdin().lock().read_to_end(&mut buffer)?; + return Ok(Content::Raw(Self::from_hex_or_raw(buffer))); + } + STDIN_HAS_BEEN_USED.store(true, Ordering::SeqCst); + Ok(Content::Raw(vec![])) + } else { + let path = std::path::PathBuf::from(s); + if path.exists() { + let content = read(&path).unwrap(); + Ok(Content::Path(path, Self::from_hex_or_raw(content))) + } else { + Ok(Content::Raw(Self::from_hex_or_raw(s.as_bytes().to_vec()))) + } + } + } +} + +impl AsRef<[u8]> for Content { + fn as_ref(&self) -> &[u8] { + match self { + Content::Path(_, content) => content.as_ref(), + Content::Raw(raw) => raw.as_ref(), + } + } +} diff --git a/forc-plugins/forc-crypto/src/sha256.rs b/forc-plugins/forc-crypto/src/sha256.rs index e40e61a11d5..e200ac0bb16 100644 --- a/forc-plugins/forc-crypto/src/sha256.rs +++ b/forc-plugins/forc-crypto/src/sha256.rs @@ -1,5 +1,6 @@ use sha2::{Digest, Sha256}; +/// Hashes a given data to Sha256 pub fn hash>(data: T) -> anyhow::Result> { let mut hasher = Sha256::new(); hasher.update(data);