From f21f01126fd60590b88db722910fa09c127dbf64 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 1 Sep 2023 09:00:49 +0100 Subject: [PATCH] Bump dalek-ed25519 to 2.0.0 (#667) * Bump dalek-ed25519 to 2.0.0 * Fix serde serialization * Fixing lint and comments * Adding key converters and helpers * Remove temporary HexKey * Update key files structure * Fixing test! * Addressing nits --- Cargo.lock | 79 +++++--- examples/demo-prover/Cargo.lock | 169 ++++++----------- examples/demo-prover/methods/guest/Cargo.lock | 88 ++++----- examples/demo-rollup/Cargo.toml | 1 + examples/demo-rollup/src/lib.rs | 26 +-- examples/demo-rollup/src/register_rpc.rs | 16 +- examples/demo-stf/Cargo.toml | 1 + .../test-data/keys/minter_private_key.json | 40 +++- .../test-data/keys/tx_signer_private_key.json | 6 +- module-system/sov-cli/src/wallet_state.rs | 108 ++++++++++- module-system/sov-cli/src/workflows/keys.rs | 8 +- module-system/sov-cli/tests/keys.rs | 5 +- module-system/sov-modules-api/Cargo.toml | 8 +- .../sov-modules-api/src/default_signature.rs | 177 ++++++++---------- 14 files changed, 418 insertions(+), 314 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee6194be9..dcec5ea04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1402,17 +1402,32 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "3.2.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +checksum = "f711ade317dd348950a9910f81c5947e3d8907ebd2b83f76203ff1807e6a2bc2" dependencies = [ - "byteorder", - "digest 0.9.0", - "rand_core 0.5.1", + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "platforms", + "rustc_version 0.4.0", "subtle", "zeroize", ] +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" +dependencies = [ + "proc-macro2 1.0.66", + "quote 1.0.33", + "syn 2.0.28", +] + [[package]] name = "curve25519-dalek-ng" version = "4.1.1" @@ -1534,6 +1549,7 @@ dependencies = [ "borsh", "clap", "const-rollup-config", + "demo-stf", "hex", "jsonrpsee", "rand 0.8.5", @@ -1740,19 +1756,10 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", - "signature 2.1.0", + "signature", "spki", ] -[[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]] name = "ed25519" version = "2.2.1" @@ -1760,7 +1767,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fb04eee5d9d907f29e80ee6b0e78f7e2c82342c63e3580d8c4f69d9d5aad963" dependencies = [ "pkcs8", - "signature 2.1.0", + "serde", + "signature", ] [[package]] @@ -1778,15 +1786,15 @@ dependencies = [ [[package]] name = "ed25519-dalek" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +checksum = "7277392b266383ef8396db7fdeb1e77b6c52fed775f5df15bb24f35b72156980" dependencies = [ "curve25519-dalek", - "ed25519 1.5.3", - "rand 0.7.3", + "ed25519", + "rand_core 0.6.4", "serde", - "sha2 0.9.9", + "sha2 0.10.7", "zeroize", ] @@ -2329,6 +2337,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "fiat-crypto" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" + [[package]] name = "fixed-hash" version = "0.8.0" @@ -3443,7 +3457,7 @@ dependencies = [ "elliptic-curve", "once_cell", "sha2 0.10.7", - "signature 2.1.0", + "signature", ] [[package]] @@ -4393,6 +4407,12 @@ dependencies = [ "crunchy", ] +[[package]] +name = "platforms" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4503fa043bf02cee09a9582e9554b4c6403b2ef55e4612e96561d294419429f8" + [[package]] name = "plotters" version = "0.3.5" @@ -6387,12 +6407,6 @@ 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" @@ -6528,6 +6542,7 @@ dependencies = [ "jmt", "serde", "serde_json", + "sov-attester-incentives", "sov-bank", "sov-chain-state", "sov-modules-api", @@ -6685,6 +6700,7 @@ dependencies = [ "serde_json", "sha2 0.10.7", "sov-bank", + "sov-cli", "sov-db", "sov-demo-rollup", "sov-ethereum", @@ -6793,13 +6809,14 @@ dependencies = [ "ed25519-dalek", "hex", "jsonrpsee", - "rand 0.7.3", + "rand 0.8.5", "risc0-zkvm", "risc0-zkvm-platform", "schemars", "serde", "serde_json", "sha2 0.10.7", + "sov-modules-api", "sov-modules-macros", "sov-rollup-interface", "sov-sequencer", @@ -7257,7 +7274,7 @@ checksum = "3f0a7d05cf78524782337f8edd55cbc578d159a16ad4affe2135c92f7dbac7f0" dependencies = [ "bytes", "digest 0.10.7", - "ed25519 2.2.1", + "ed25519", "ed25519-consensus", "flex-error", "futures", @@ -7270,7 +7287,7 @@ dependencies = [ "serde_json", "serde_repr", "sha2 0.10.7", - "signature 2.1.0", + "signature", "subtle", "subtle-encoding", "tendermint-proto", diff --git a/examples/demo-prover/Cargo.lock b/examples/demo-prover/Cargo.lock index 30644adf4..70c693763 100644 --- a/examples/demo-prover/Cargo.lock +++ b/examples/demo-prover/Cargo.lock @@ -710,7 +710,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf4c2f4e1afd912bc40bfd6fed5d9dc1f288e0ba01bfcc835cc5bc3eb13efe15" dependencies = [ - "rand_core 0.6.4", + "rand_core", "subtle", ] @@ -747,17 +747,32 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "3.2.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +checksum = "f711ade317dd348950a9910f81c5947e3d8907ebd2b83f76203ff1807e6a2bc2" dependencies = [ - "byteorder", - "digest 0.9.0", - "rand_core 0.5.1", + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "platforms", + "rustc_version", "subtle", "zeroize", ] +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.29", +] + [[package]] name = "curve25519-dalek-ng" version = "4.1.1" @@ -766,7 +781,7 @@ checksum = "1c359b7249347e46fb28804470d071c921156ad62b3eef5d34e2ba867533dec8" dependencies = [ "byteorder", "digest 0.9.0", - "rand_core 0.6.4", + "rand_core", "subtle-ng", "zeroize", ] @@ -915,7 +930,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05213e96f184578b5f70105d4d0a644a168e99e12d7bea0b200c15d67b5c182" dependencies = [ "futures", - "rand 0.8.5", + "rand", "reqwest", "thiserror", "tokio", @@ -946,15 +961,6 @@ dependencies = [ "syn 1.0.109", ] -[[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]] name = "ed25519" version = "2.2.1" @@ -962,7 +968,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fb04eee5d9d907f29e80ee6b0e78f7e2c82342c63e3580d8c4f69d9d5aad963" dependencies = [ "pkcs8", - "signature 2.1.0", + "serde", + "signature", ] [[package]] @@ -973,22 +980,22 @@ checksum = "3c8465edc8ee7436ffea81d21a019b16676ee3db267aa8d5a8d729581ecf998b" dependencies = [ "curve25519-dalek-ng", "hex", - "rand_core 0.6.4", + "rand_core", "sha2 0.9.9", "zeroize", ] [[package]] name = "ed25519-dalek" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +checksum = "7277392b266383ef8396db7fdeb1e77b6c52fed775f5df15bb24f35b72156980" dependencies = [ "curve25519-dalek", - "ed25519 1.5.3", - "rand 0.7.3", + "ed25519", + "rand_core", "serde", - "sha2 0.9.9", + "sha2 0.10.7", "zeroize", ] @@ -1102,7 +1109,7 @@ dependencies = [ "bitvec", "byteorder", "ff_derive", - "rand_core 0.6.4", + "rand_core", "subtle", ] @@ -1122,6 +1129,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "fiat-crypto" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" + [[package]] name = "fixedbitset" version = "0.4.2" @@ -1293,17 +1306,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.2.10" @@ -1820,7 +1822,7 @@ dependencies = [ "hyper", "jsonrpsee-types 0.16.2", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "rustc-hash", "serde", "serde_json", @@ -1846,7 +1848,7 @@ dependencies = [ "hyper", "jsonrpsee-types 0.18.2", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "rustc-hash", "serde", "serde_json", @@ -2534,7 +2536,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" dependencies = [ "base64ct", - "rand_core 0.6.4", + "rand_core", "subtle", ] @@ -2636,6 +2638,12 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "platforms" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2787,19 +2795,6 @@ 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" @@ -2807,18 +2802,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "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", + "rand_chacha", + "rand_core", ] [[package]] @@ -2828,16 +2813,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "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", + "rand_core", ] [[package]] @@ -2846,16 +2822,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 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", + "getrandom", ] [[package]] @@ -2910,7 +2877,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" dependencies = [ - "getrandom 0.2.10", + "getrandom", "redox_syscall 0.2.16", "thiserror", ] @@ -3078,7 +3045,7 @@ checksum = "e3066a37173332f502f0bed3ead09fea325a19931c24e728bd844be229ba72c7" dependencies = [ "anyhow", "log", - "rand 0.8.5", + "rand", "rayon", "risc0-circuit-rv32im-sys", "risc0-core", @@ -3107,7 +3074,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ea0e9d6d5845f11157728c494541c42559357fee35afce767b3d3610ef7494b" dependencies = [ "bytemuck", - "rand_core 0.6.4", + "rand_core", ] [[package]] @@ -3138,8 +3105,8 @@ dependencies = [ "log", "ndarray", "paste", - "rand 0.8.5", - "rand_core 0.6.4", + "rand", + "rand_core", "rayon", "risc0-core", "risc0-sys", @@ -3165,14 +3132,14 @@ dependencies = [ "dyn_partial_eq", "elf", "generic-array", - "getrandom 0.2.10", + "getrandom", "hex", "lazy-regex", "libm", "log", "num-derive 0.4.0", "num-traits", - "rand 0.8.5", + "rand", "rayon", "risc0-circuit-rv32im", "risc0-core", @@ -3611,12 +3578,6 @@ 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" @@ -3660,7 +3621,7 @@ dependencies = [ "http", "httparse", "log", - "rand 0.8.5", + "rand", "sha-1", ] @@ -3837,7 +3798,7 @@ dependencies = [ "ed25519-dalek", "hex", "jsonrpsee 0.18.2", - "rand 0.7.3", + "rand", "schemars", "serde", "serde_json", @@ -4085,7 +4046,7 @@ checksum = "3f0a7d05cf78524782337f8edd55cbc578d159a16ad4affe2135c92f7dbac7f0" dependencies = [ "bytes", "digest 0.10.7", - "ed25519 2.2.1", + "ed25519", "ed25519-consensus", "flex-error", "futures", @@ -4098,7 +4059,7 @@ dependencies = [ "serde_json", "serde_repr", "sha2 0.10.7", - "signature 2.1.0", + "signature", "subtle", "subtle-encoding", "tendermint-proto", @@ -4589,12 +4550,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/examples/demo-prover/methods/guest/Cargo.lock b/examples/demo-prover/methods/guest/Cargo.lock index ff8c2de06..26e257604 100644 --- a/examples/demo-prover/methods/guest/Cargo.lock +++ b/examples/demo-prover/methods/guest/Cargo.lock @@ -404,15 +404,29 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "3.2.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +checksum = "f711ade317dd348950a9910f81c5947e3d8907ebd2b83f76203ff1807e6a2bc2" dependencies = [ - "byteorder", - "digest 0.9.0", - "rand_core 0.5.1", + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "platforms", + "rustc_version", "subtle", - "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", ] [[package]] @@ -423,7 +437,7 @@ checksum = "1c359b7249347e46fb28804470d071c921156ad62b3eef5d34e2ba867533dec8" dependencies = [ "byteorder", "digest 0.9.0", - "rand_core 0.6.4", + "rand_core", "subtle-ng", "zeroize", ] @@ -557,15 +571,6 @@ dependencies = [ "syn 1.0.109", ] -[[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]] name = "ed25519" version = "2.2.1" @@ -573,7 +578,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fb04eee5d9d907f29e80ee6b0e78f7e2c82342c63e3580d8c4f69d9d5aad963" dependencies = [ "pkcs8", - "signature 2.1.0", + "signature", ] [[package]] @@ -584,21 +589,20 @@ checksum = "3c8465edc8ee7436ffea81d21a019b16676ee3db267aa8d5a8d729581ecf998b" dependencies = [ "curve25519-dalek-ng", "hex", - "rand_core 0.6.4", + "rand_core", "sha2 0.9.9", "zeroize", ] [[package]] name = "ed25519-dalek" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +checksum = "7277392b266383ef8396db7fdeb1e77b6c52fed775f5df15bb24f35b72156980" dependencies = [ "curve25519-dalek", - "ed25519 1.5.3", - "sha2 0.9.9", - "zeroize", + "ed25519", + "sha2 0.10.6", ] [[package]] @@ -662,6 +666,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "fiat-crypto" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" + [[package]] name = "fixedbitset" version = "0.4.2" @@ -1325,7 +1335,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" dependencies = [ "base64ct", - "rand_core 0.6.4", + "rand_core", "subtle", ] @@ -1401,6 +1411,12 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "platforms" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1506,7 +1522,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core 0.6.4", + "rand_core", ] [[package]] @@ -1516,15 +1532,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core 0.6.4", + "rand_core", ] -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" - [[package]] name = "rand_core" version = "0.6.4" @@ -1673,7 +1683,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ea0e9d6d5845f11157728c494541c42559357fee35afce767b3d3610ef7494b" dependencies = [ "bytemuck", - "rand_core 0.6.4", + "rand_core", ] [[package]] @@ -1689,7 +1699,7 @@ dependencies = [ "hex", "log", "paste", - "rand_core 0.6.4", + "rand_core", "risc0-core", "risc0-zkvm-platform", "serde", @@ -1944,12 +1954,6 @@ dependencies = [ "keccak", ] -[[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" @@ -2246,7 +2250,7 @@ checksum = "3f0a7d05cf78524782337f8edd55cbc578d159a16ad4affe2135c92f7dbac7f0" dependencies = [ "bytes", "digest 0.10.7", - "ed25519 2.2.1", + "ed25519", "ed25519-consensus", "flex-error", "futures", @@ -2259,7 +2263,7 @@ dependencies = [ "serde_json", "serde_repr", "sha2 0.10.6", - "signature 2.1.0", + "signature", "subtle", "subtle-encoding", "tendermint-proto", diff --git a/examples/demo-rollup/Cargo.toml b/examples/demo-rollup/Cargo.toml index ffdd7ad3e..fce0144a3 100644 --- a/examples/demo-rollup/Cargo.toml +++ b/examples/demo-rollup/Cargo.toml @@ -39,6 +39,7 @@ sov-modules-stf-template = { path = "../../module-system/sov-modules-stf-templat sov-modules-api = { path = "../../module-system/sov-modules-api", features = ["native"] } sov-state = { path = "../../module-system/sov-state", features = ["native"] } const-rollup-config = { path = "../const-rollup-config" } +sov-cli = { path = "../../module-system/sov-cli" } [dev-dependencies] sov-evm = { path = "../../module-system/module-implementations/sov-evm", features = ["smart_contracts"] } diff --git a/examples/demo-rollup/src/lib.rs b/examples/demo-rollup/src/lib.rs index 3cb03dee2..e9f9f377b 100644 --- a/examples/demo-rollup/src/lib.rs +++ b/examples/demo-rollup/src/lib.rs @@ -6,10 +6,10 @@ mod rollup; use celestia::types::NamespaceId; use const_rollup_config::ROLLUP_NAMESPACE_RAW; -use demo_stf::app::DefaultPrivateKey; use demo_stf::genesis_config::create_demo_genesis_config; use demo_stf::runtime::GenesisConfig; pub use rollup::{new_rollup_with_celestia_da, Rollup}; +use sov_cli::wallet_state::{HexPrivateAndAddress, PrivateKeyAndAddress}; use sov_db::ledger_db::LedgerDB; use sov_modules_api::default_context::DefaultContext; use sov_rollup_interface::BasicAddress; @@ -23,13 +23,6 @@ pub fn initialize_ledger(path: impl AsRef) -> LedgerDB { LedgerDB::with_path(path).expect("Ledger DB failed to open") } -/// TODO: Remove this when sov-cli is in its own crate. -#[derive(Debug, serde::Serialize, serde::Deserialize)] -pub struct HexKey { - hex_priv_key: String, - address: String, -} - /// Configure our rollup with a centralized sequencer using the SEQUENCER_DA_ADDRESS /// address constant. Since the centralize sequencer's address is consensus critical, /// it has to be hardcoded as a constant, rather than read from the config at runtime. @@ -43,21 +36,22 @@ pub struct HexKey { pub fn get_genesis_config( sequencer_da_address: A, ) -> GenesisConfig { - let hex_key: HexKey = serde_json::from_slice(include_bytes!( + let hex_key: HexPrivateAndAddress = serde_json::from_slice(include_bytes!( "../../test-data/keys/token_deployer_private_key.json" )) .expect("Broken key data file"); - let sequencer_private_key = DefaultPrivateKey::from_hex(&hex_key.hex_priv_key).unwrap(); - assert_eq!( - sequencer_private_key.default_address().to_string(), - hex_key.address, - "Inconsistent key data", + let key_and_address: PrivateKeyAndAddress = hex_key + .try_into() + .expect("Failed to parse sequencer private key and address"); + assert!( + key_and_address.is_matching_to_default(), + "Inconsistent key data" ); create_demo_genesis_config( 100000000, - sequencer_private_key.default_address(), + key_and_address.address, sequencer_da_address.as_ref().to_vec(), - &sequencer_private_key, + &key_and_address.private_key, ) } diff --git a/examples/demo-rollup/src/register_rpc.rs b/examples/demo-rollup/src/register_rpc.rs index 3d64bb0f5..c7d89a55b 100644 --- a/examples/demo-rollup/src/register_rpc.rs +++ b/examples/demo-rollup/src/register_rpc.rs @@ -51,16 +51,16 @@ pub fn register_ethereum( let data = fs::read_to_string(TX_SIGNER_PRIV_KEY_PATH).context("Unable to read file")?; - let hex_key: crate::HexKey = - serde_json::from_str(&data).context("JSON does not have correct format.")?; + let hex_key: sov_cli::wallet_state::HexPrivateAndAddress = + serde_json::from_str(&data).context("JSON does not have a correct format.")?; - let tx_signer_private_key = - sov_modules_api::default_signature::private_key::DefaultPrivateKey::from_hex( - &hex_key.hex_priv_key, - ) - .unwrap(); + let key_and_address: sov_cli::wallet_state::PrivateKeyAndAddress< + sov_modules_api::default_context::DefaultContext, + > = hex_key + .try_into() + .expect("Failed to parse sequencer private key and address"); - let ethereum_rpc = sov_ethereum::get_ethereum_rpc(da_service, tx_signer_private_key); + let ethereum_rpc = sov_ethereum::get_ethereum_rpc(da_service, key_and_address.private_key); methods .merge(ethereum_rpc) .context("Failed to merge Ethereum RPC modules") diff --git a/examples/demo-stf/Cargo.toml b/examples/demo-stf/Cargo.toml index e17952125..f2e4559b6 100644 --- a/examples/demo-stf/Cargo.toml +++ b/examples/demo-stf/Cargo.toml @@ -45,6 +45,7 @@ sov-evm = { path = "../../module-system/module-implementations/sov-evm", optiona [dev-dependencies] sov-rollup-interface = { path = "../../rollup-interface", features = ["mocks"] } sov-data-generators = { path = "../../module-system/utils/sov-data-generators" } +demo-stf = { path = ".", features = ["native"] } tempfile = { workspace = true } rand = "0.8" diff --git a/examples/test-data/keys/minter_private_key.json b/examples/test-data/keys/minter_private_key.json index bd50823b0..117c31efb 100644 --- a/examples/test-data/keys/minter_private_key.json +++ b/examples/test-data/keys/minter_private_key.json @@ -1 +1,39 @@ -[35, 110, 128, 203, 34, 44, 78, 208, 67, 27, 9, 59, 58, 197, 62, 106, 167, 162, 39, 63, 225, 244, 53, 28, 211, 84, 152, 154, 130, 52, 50, 162, 123, 117, 139, 242, 231, 103, 15, 175, 175, 107, 240, 1, 92, 224, 255, 90, 168, 2, 48, 111, 199, 227, 244, 87, 98, 133, 63, 252, 55, 24, 15, 230] \ No newline at end of file +{ + "private_key": { + "key_pair": [ + 35, + 110, + 128, + 203, + 34, + 44, + 78, + 208, + 67, + 27, + 9, + 59, + 58, + 197, + 62, + 106, + 167, + 162, + 39, + 63, + 225, + 244, + 53, + 28, + 211, + 84, + 152, + 154, + 130, + 52, + 50, + 162 + ] + }, + "address": "sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqwr57gc" +} \ No newline at end of file diff --git a/examples/test-data/keys/tx_signer_private_key.json b/examples/test-data/keys/tx_signer_private_key.json index f01dde9ad..b237947bc 100644 --- a/examples/test-data/keys/tx_signer_private_key.json +++ b/examples/test-data/keys/tx_signer_private_key.json @@ -1,4 +1,4 @@ { - "hex_priv_key": "27c3774d52e71ea266a9c5256cd98b9ae67e62f2ae5ed34a668db8eaa83e1bac61fcf0f466bc20ca3882d46ae07d65227e31cfaefb852bc8f579415247565dd4", - "address": "sov1dnhqk4mdsj2kwv4xymt8a624xuahfx8906j9usdkx7ensfghndkq8p33f7" - } \ No newline at end of file + "hex_priv_key": "27c3774d52e71ea266a9c5256cd98b9ae67e62f2ae5ed34a668db8eaa83e1bac61fcf0f466bc20ca3882d46ae07d65227e31cfaefb852bc8f579415247565dd4", + "address": "sov1dnhqk4mdsj2kwv4xymt8a624xuahfx8906j9usdkx7ensfghndkq8p33f7" +} \ No newline at end of file diff --git a/module-system/sov-cli/src/wallet_state.rs b/module-system/sov-cli/src/wallet_state.rs index dce038145..08b8691ef 100644 --- a/module-system/sov-cli/src/wallet_state.rs +++ b/module-system/sov-cli/src/wallet_state.rs @@ -1,9 +1,10 @@ use std::fs; use std::path::{Path, PathBuf}; +use std::str::FromStr; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; -use sov_modules_api::clap; +use sov_modules_api::{clap, PrivateKey}; /// A struct representing the current state of the CLI wallet #[derive(Debug, Serialize, Deserialize)] @@ -52,6 +53,67 @@ impl WalletStat } } +/// A struct representing private key and associated address +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(bound = "Ctx::Address: Serialize + DeserializeOwned")] +pub struct PrivateKeyAndAddress { + /// Private key of the address + pub private_key: Ctx::PrivateKey, + /// Address associated from the private key + pub address: Ctx::Address, +} + +impl PrivateKeyAndAddress { + /// Returns boolean if the private key matches default address + pub fn is_matching_to_default(&self) -> bool { + self.private_key.to_address::() == self.address + } + + /// Randomly generates a new private key and address + pub fn generate() -> Self { + let private_key = Ctx::PrivateKey::generate(); + let address = private_key.to_address::(); + Self { + private_key, + address, + } + } + + /// Generates valid private key and address from given private key + pub fn from_key(private_key: Ctx::PrivateKey) -> Self { + let address = private_key.to_address::(); + Self { + private_key, + address, + } + } +} + +/// A simplified struct representing private key and associated address +/// where the private key is represented as a hex string and address as canonical string +/// TODO: Remove it https://github.com/Sovereign-Labs/sovereign-sdk/issues/766 +#[derive(Debug, serde::Serialize, serde::Deserialize)] +pub struct HexPrivateAndAddress { + /// Private key is hex encoded bytes, without leading 0x + pub hex_priv_key: String, + /// Address is in canonical string format + pub address: String, +} + +impl TryFrom for PrivateKeyAndAddress { + type Error = anyhow::Error; + + fn try_from(value: HexPrivateAndAddress) -> Result { + let private_key_bytes = hex::decode(value.hex_priv_key)?; + let private_key = Ctx::PrivateKey::try_from(&private_key_bytes)?; + let address = Ctx::Address::from_str(&value.address)?; + Ok(PrivateKeyAndAddress { + private_key, + address, + }) + } +} + /// A list of addresses associated with this wallet #[derive(Debug, Serialize, Deserialize)] #[serde(bound = "Ctx::Address: Serialize + DeserializeOwned")] @@ -227,3 +289,47 @@ mod pubkey_hex { deserializer.deserialize_str(HexPubkeyVisitor(PhantomData::)) } } + +#[cfg(test)] +mod tests { + use sov_modules_api::default_context::DefaultContext; + + use super::*; + + type C = DefaultContext; + #[test] + fn test_private_key_and_address() { + let private_key_and_address = PrivateKeyAndAddress::::generate(); + + let json = serde_json::to_string_pretty(&private_key_and_address).unwrap(); + + let decoded: PrivateKeyAndAddress = serde_json::from_str(&json).unwrap(); + + assert_eq!( + private_key_and_address.private_key.pub_key(), + decoded.private_key.pub_key() + ); + assert_eq!(private_key_and_address.address, decoded.address); + } + + #[test] + fn test_hex_private_key_conversion() { + let private_key_and_address = PrivateKeyAndAddress::::generate(); + + let hex_private_key = private_key_and_address.private_key.as_hex(); + let address_string = private_key_and_address.address.to_string(); + + let hex_private_key_and_address = HexPrivateAndAddress { + hex_priv_key: hex_private_key, + address: address_string, + }; + + let converted = PrivateKeyAndAddress::::try_from(hex_private_key_and_address).unwrap(); + + assert_eq!( + private_key_and_address.private_key.pub_key(), + converted.private_key.pub_key() + ); + assert_eq!(private_key_and_address.address, converted.address); + } +} diff --git a/module-system/sov-cli/src/workflows/keys.rs b/module-system/sov-cli/src/workflows/keys.rs index e40c072df..cc5b67629 100644 --- a/module-system/sov-cli/src/workflows/keys.rs +++ b/module-system/sov-cli/src/workflows/keys.rs @@ -5,7 +5,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use sov_modules_api::{clap, PrivateKey, PublicKey, Spec}; -use crate::wallet_state::{KeyIdentifier, WalletState}; +use crate::wallet_state::{KeyIdentifier, PrivateKeyAndAddress, WalletState}; #[derive(clap::Subcommand)] /// View and manage keys associated with this wallet @@ -111,9 +111,9 @@ impl KeyWorkflow { pub fn load_key( path: impl AsRef, ) -> Result { - let data = std::fs::read(path)?; - let key = serde_json::from_slice(data.as_slice())?; - Ok(key) + let data = std::fs::read_to_string(path)?; + let key_and_address: PrivateKeyAndAddress = serde_json::from_str(&data)?; + Ok(key_and_address.private_key) } /// Generate a new key pair and save it to the wallet diff --git a/module-system/sov-cli/tests/keys.rs b/module-system/sov-cli/tests/keys.rs index 75df62692..82bba7383 100644 --- a/module-system/sov-cli/tests/keys.rs +++ b/module-system/sov-cli/tests/keys.rs @@ -1,5 +1,5 @@ use demo_stf::runtime::RuntimeCall; -use sov_cli::wallet_state::{KeyIdentifier, WalletState}; +use sov_cli::wallet_state::{KeyIdentifier, PrivateKeyAndAddress, WalletState}; use sov_cli::workflows::keys::KeyWorkflow; use sov_modules_api::default_context::DefaultContext; use sov_modules_api::{PrivateKey, PublicKey, Spec}; @@ -20,7 +20,8 @@ fn test_key_import() { // Generate a key and write it to a file let generated_key = ::PrivateKey::generate(); let key_path = app_dir.path().join("test_key"); - std::fs::write(&key_path, serde_json::to_string(&generated_key).unwrap()) + let key_and_address = PrivateKeyAndAddress::::from_key(generated_key.clone()); + std::fs::write(&key_path, serde_json::to_string(&key_and_address).unwrap()) .expect("Failed to write key to tempdir"); // Initialize an empty wallet diff --git a/module-system/sov-modules-api/Cargo.toml b/module-system/sov-modules-api/Cargo.toml index 6e74c1893..69fb4168b 100644 --- a/module-system/sov-modules-api/Cargo.toml +++ b/module-system/sov-modules-api/Cargo.toml @@ -30,9 +30,8 @@ hex = { workspace = true, optional = true } clap = { workspace = true, optional = true } schemars = { workspace = true, optional = true, features = [] } -# TODO: try update 2.x -ed25519-dalek = { version = "1.0.1", default-features = false, features = ["u64_backend"] } -rand = { version = "0.7", optional = true } +ed25519-dalek = { version = "2.0.0", default-features = false } +rand = { version = "0.8", optional = true } zk-cycle-macros = { path = "../../utils/zk-cycle-macros", optional = true } risc0-zkvm = { version = "0.16", default-features = false, features = ["std"], optional = true } @@ -40,9 +39,10 @@ risc0-zkvm-platform = { version = "0.16", optional = true } [dev-dependencies] bincode = { workspace = true } +sov-modules-api = { path = ".", features = ["native"] } [features] bench = ["zk-cycle-macros", "risc0-zkvm", "risc0-zkvm-platform"] default = ["macros"] -native = ["serde_json", "rand", "hex", "schemars", "ed25519-dalek/default", "clap", "jsonrpsee", "macros", "sov-modules-macros/native", "sov-state/native", "sov-sequencer"] +native = ["serde_json", "rand", "hex", "schemars", "ed25519-dalek/default", "ed25519-dalek/serde", "ed25519-dalek/rand_core", "clap", "jsonrpsee", "macros", "sov-modules-macros/native", "sov-state/native", "sov-sequencer"] macros = ["sov-modules-macros"] diff --git a/module-system/sov-modules-api/src/default_signature.rs b/module-system/sov-modules-api/src/default_signature.rs index 5b2cc54a1..ab7be5a2b 100644 --- a/module-system/sov-modules-api/src/default_signature.rs +++ b/module-system/sov-modules-api/src/default_signature.rs @@ -3,19 +3,15 @@ use std::hash::Hash; use std::str::FromStr; use borsh::{BorshDeserialize, BorshSerialize}; -use ed25519_dalek::ed25519::signature::Signature as DalekSignatureTrait; use ed25519_dalek::{ - PublicKey as DalekPublicKey, Signature as DalekSignature, PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH, + Signature as DalekSignature, VerifyingKey as DalekPublicKey, PUBLIC_KEY_LENGTH, }; -use serde::de::Error; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::{SigVerificationError, Signature}; #[cfg(feature = "native")] pub mod private_key { - - use ed25519_dalek::{Keypair, SignatureError, Signer}; + use ed25519_dalek::{Signer, SigningKey, KEYPAIR_LENGTH, SECRET_KEY_LENGTH}; use rand::rngs::OsRng; use thiserror::Error; @@ -23,40 +19,52 @@ pub mod private_key { use crate::{Address, PrivateKey, PublicKey}; #[derive(Error, Debug)] - pub enum DefaultPrivateKeyHexDeserializationError { + pub enum DefaultPrivateKeyDeserializationError { #[error("Hex deserialization error")] FromHexError(#[from] hex::FromHexError), - #[error("PrivateKey deserialization error")] - PrivateKeyError(#[from] SignatureError), + #[error("KeyPairError deserialization error")] + KeyPairError(#[from] ed25519_dalek::SignatureError), + #[error("Invalid private key length: {actual}, expected {expected_1} or {expected_2}")] + InvalidPrivateKeyLength { + expected_1: usize, + expected_2: usize, + actual: usize, + }, } /// A private key for the default signature scheme. /// This struct also stores the corresponding public key. + #[derive(Clone, serde::Serialize, serde::Deserialize)] pub struct DefaultPrivateKey { - key_pair: Keypair, + key_pair: SigningKey, } - impl core::fmt::Debug for DefaultPrivateKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("DefaultPrivateKey") - .field("public_key", &self.key_pair.public) - .field("private_key", &"***REDACTED***") - .finish() + impl DefaultPrivateKey { + // This is private method and panics if input slice has incorrect length + fn try_from_keypair(value: &[u8]) -> Result { + let value: [u8; KEYPAIR_LENGTH] = value + .try_into() + .expect("incorrect usage of `try_from_keypair`, check input length"); + let key_pair = SigningKey::from_keypair_bytes(&value)?; + Ok(Self { key_pair }) } - } - impl serde::Serialize for DefaultPrivateKey { - fn serialize(&self, serializer: S) -> Result { - serializer.serialize_bytes(&self.key_pair.to_bytes()) + // This is private method and panics if input slice has incorrect length + fn try_from_private_key(value: &[u8]) -> Self { + let value: [u8; SECRET_KEY_LENGTH] = value + .try_into() + .expect("incorrect usage of `try_from_private_key`, check input length"); + let key_pair = SigningKey::from_bytes(&value); + Self { key_pair } } } - impl<'de> serde::Deserialize<'de> for DefaultPrivateKey { - fn deserialize>(deserializer: D) -> Result { - use serde::de::Error; - let bytes = Vec::::deserialize(deserializer)?; - let key_pair = Keypair::from_bytes(&bytes).map_err(D::Error::custom)?; - Ok(Self { key_pair }) + impl core::fmt::Debug for DefaultPrivateKey { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DefaultPrivateKey") + .field("public_key", &self.key_pair.verifying_key()) + .field("private_key", &"***REDACTED***") + .finish() } } @@ -64,9 +72,20 @@ pub mod private_key { type Error = anyhow::Error; fn try_from(value: &[u8]) -> Result { - let key_pair = Keypair::from_bytes(value)?; - key_pair.secret.to_bytes(); - Ok(Self { key_pair }) + if value.len() == KEYPAIR_LENGTH { + Self::try_from_keypair(value).map_err(|e| e.into()) + } else if value.len() == SECRET_KEY_LENGTH { + Ok(Self::try_from_private_key(value)) + } else { + let err = Err( + DefaultPrivateKeyDeserializationError::InvalidPrivateKeyLength { + expected_1: SECRET_KEY_LENGTH, + expected_2: KEYPAIR_LENGTH, + actual: value.len(), + }, + ); + err.map_err(|e| e.into()) + } } } @@ -79,13 +98,13 @@ pub mod private_key { let mut csprng = OsRng; Self { - key_pair: Keypair::generate(&mut csprng), + key_pair: SigningKey::generate(&mut csprng), } } fn pub_key(&self) -> Self::PublicKey { DefaultPublicKey { - pub_key: self.key_pair.public, + pub_key: self.key_pair.verifying_key(), } } @@ -101,11 +120,9 @@ pub mod private_key { hex::encode(self.key_pair.to_bytes()) } - pub fn from_hex(hex: &str) -> Result { + pub fn from_hex(hex: &str) -> anyhow::Result { let bytes = hex::decode(hex)?; - Ok(Self { - key_pair: Keypair::from_bytes(&bytes)?, - }) + Self::try_from(&bytes[..]) } pub fn default_address(&self) -> Address { @@ -120,11 +137,11 @@ pub mod private_key { use rand::SeedableRng; // it is important to generate the secret deterministically from the arbitrary argument - // so keys and signatures will be reproductible for a given seed. this unlocks fuzzy - // replay + // so keys and signatures will be reproducible for a given seed. + // this unlocks fuzzy replay let seed = <[u8; 32]>::arbitrary(u)?; let rng = &mut StdRng::from_seed(seed); - let key_pair = Keypair::generate(rng); + let key_pair = SigningKey::generate(rng); Ok(Self { key_pair }) } @@ -149,7 +166,10 @@ pub mod private_key { } } -#[cfg_attr(feature = "native", derive(schemars::JsonSchema))] +#[cfg_attr( + feature = "native", + derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema) +)] #[derive(PartialEq, Eq, Clone, Debug)] pub struct DefaultPublicKey { #[cfg_attr( @@ -165,29 +185,6 @@ impl Hash for DefaultPublicKey { } } -impl Serialize for DefaultPublicKey { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = self.pub_key.as_bytes(); - serializer.serialize_bytes(s) - } -} - -impl<'de> Deserialize<'de> for DefaultPublicKey { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let bytes = as serde::Deserialize>::deserialize(deserializer)?; - let dpk = DalekPublicKey::from_bytes(&bytes).or(Err(D::Error::custom( - "Couldn't convert bytes to ed25519 public key", - )))?; - Ok(DefaultPublicKey { pub_key: dpk }) - } -} - impl BorshDeserialize for DefaultPublicKey { fn deserialize_reader(reader: &mut R) -> std::io::Result { let mut buffer = [0; PUBLIC_KEY_LENGTH]; @@ -205,7 +202,10 @@ impl BorshSerialize for DefaultPublicKey { } } -#[cfg_attr(feature = "native", derive(schemars::JsonSchema))] +#[cfg_attr( + feature = "native", + derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema) +)] #[derive(PartialEq, Eq, Debug, Clone)] pub struct DefaultSignature { #[cfg_attr( @@ -215,43 +215,20 @@ pub struct DefaultSignature { pub msg_sig: DalekSignature, } -impl Serialize for DefaultSignature { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let s = self.msg_sig.as_bytes(); - serializer.serialize_bytes(s) - } -} - -impl<'de> Deserialize<'de> for DefaultSignature { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let bytes = as serde::Deserialize>::deserialize(deserializer)?; - let dsig = DalekSignature::from_bytes(&bytes).or(Err(D::Error::custom( - "Couldn't convert bytes to ed25519 signature", - )))?; - Ok(DefaultSignature { msg_sig: dsig }) - } -} - impl BorshDeserialize for DefaultSignature { fn deserialize_reader(reader: &mut R) -> std::io::Result { - let mut buffer = [0; SIGNATURE_LENGTH]; + let mut buffer = [0; DalekSignature::BYTE_SIZE]; reader.read_exact(&mut buffer)?; - let msg_sig = DalekSignature::from_bytes(&buffer).map_err(map_error)?; - - Ok(Self { msg_sig }) + Ok(Self { + msg_sig: DalekSignature::from_bytes(&buffer), + }) } } impl BorshSerialize for DefaultSignature { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_all(self.msg_sig.as_bytes()) + writer.write_all(&self.msg_sig.to_bytes()) } } @@ -281,6 +258,11 @@ impl FromStr for DefaultPublicKey { fn from_str(s: &str) -> Result { let bytes = hex::decode(s)?; + + let bytes: [u8; PUBLIC_KEY_LENGTH] = bytes + .try_into() + .map_err(|_| anyhow::anyhow!("Invalid public key size"))?; + let pub_key = DalekPublicKey::from_bytes(&bytes) .map_err(|_| anyhow::anyhow!("Invalid public key"))?; Ok(DefaultPublicKey { pub_key }) @@ -293,9 +275,14 @@ impl FromStr for DefaultSignature { fn from_str(s: &str) -> Result { let bytes = hex::decode(s)?; - let msg_sig = - DalekSignature::from_bytes(&bytes).map_err(|_| anyhow::anyhow!("Invalid signature"))?; - Ok(DefaultSignature { msg_sig }) + + let bytes: ed25519_dalek::ed25519::SignatureBytes = bytes + .try_into() + .map_err(|_| anyhow::anyhow!("Invalid signature"))?; + + Ok(DefaultSignature { + msg_sig: DalekSignature::from_bytes(&bytes), + }) } } @@ -308,7 +295,7 @@ fn test_privatekey_serde_bincode() { let key_pair = DefaultPrivateKey::generate(); let serialized = bincode::serialize(&key_pair).expect("Serialization to vec is infallible"); let output = bincode::deserialize::(&serialized) - .expect("Keypair is serialized correctly"); + .expect("SigningKey is serialized correctly"); assert_eq!(key_pair.as_hex(), output.as_hex()); }