Skip to content

Commit

Permalink
fix keypair/private_key discrepancy for sov-cli (#811)
Browse files Browse the repository at this point in the history
* fix keypair/private_key discrepancy

* formatting

* changes to use consistent format

* README changes

* linting fixes

* remove checked in rollup_config.toml

* fix token address in git workflow

---------

Co-authored-by: dubbelosix <[email protected]>
  • Loading branch information
dubbelosix and dubbelosix authored Sep 7, 2023
1 parent bb69393 commit 9efc47a
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ jobs:
SECONDS=0
while ((SECONDS <= 300))
do
if curl -f -s -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"bank_supplyOf","params":["sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"],"id":1}' http://127.0.0.1:12345 | grep -q 1000; then
if curl -f -s -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"bank_supplyOf","params":["sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"],"id":1}' http://127.0.0.1:12345 | grep -q 1000; then
echo "demo-rollup test succeeded"
exit 0
fi
Expand Down
8 changes: 4 additions & 4 deletions examples/demo-rollup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ test-generate-create-token-tx: check-container-running build-sov-cli
$(SOV_CLI_REL_PATH) transactions import from-file bank --path ../test-data/requests/create_token.json

set-rpc-url: build-sov-cli
$(SOV_CLI_REL_PATH) rpc set-url http://localhost:12345
$(SOV_CLI_REL_PATH) rpc set-url http://127.0.0.1:12345

import-keys: build-sov-cli
$(SOV_CLI_REL_PATH) keys import --nickname DANGER__DO_NOT_USE_WITH_REAL_MONEY --path ../test-data/keys/minter_private_key.json
$(SOV_CLI_REL_PATH) keys import --nickname DANGER__DO_NOT_USE_WITH_REAL_MONEY --path ../test-data/keys/token_deployer_private_key.json

test-create-token: set-rpc-url test-generate-create-token-tx import-keys
$(SOV_CLI_REL_PATH) rpc submit-batch
$(SOV_CLI_REL_PATH) rpc submit-batch by-nickname DANGER__DO_NOT_USE_WITH_REAL_MONEY

remove-insecure-keys: build-sov-cli
$(SOV_CLI_REL_PATH) keys remove by-address sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqwr57gc
$(SOV_CLI_REL_PATH) keys remove by-address sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94

clean-rollup-db:
$(eval path := ./$(shell awk -F'=' '/^path/ {print $$2}' rollup_config.toml | tr -d '[:space:]"\n'))
Expand Down
8 changes: 4 additions & 4 deletions examples/demo-rollup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Here's an example of a JSON representing the above call:
"to": "sov1zgfpyysjzgfpyysjzgfpyysjzgfpyysjzgfpyysjzgfpyysjzgfqve8h6h",
"coins": {
"amount": 200,
"token_address": "sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"
"token_address": "sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"
}
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ Adding the following transaction to batch:
"to": "sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94",
"coins": {
"amount": 200,
"token_address": "sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"
"token_address": "sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"
}
}
}
Expand All @@ -286,15 +286,15 @@ You now have a batch with a single transaction in your wallet. If you want to su
batch, you can import them now. Finally, let's submit your transaction to the rollup.

```bash
$ ./target/debug/sov-cli rpc submit-batch by-address sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqwr57gc
$ ./target/debug/sov-cli rpc submit-batch by-address sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94
```

This command will use your default private key

#### 4. Verify the Token Supply

```bash
$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"bank_supplyOf","params":["sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"],"id":1}' http://127.0.0.1:12345
$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"bank_supplyOf","params":["sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"],"id":1}' http://127.0.0.1:12345
{"jsonrpc":"2.0","result":{"amount":1000},"id":1}
```

Expand Down
13 changes: 5 additions & 8 deletions examples/demo-rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use rollup::{
new_rollup_with_celestia_da, new_rollup_with_mock_da, new_rollup_with_mock_da_from_config,
Rollup,
};
use sov_cli::wallet_state::{HexPrivateAndAddress, PrivateKeyAndAddress};
use sov_cli::wallet_state::PrivateKeyAndAddress;
use sov_db::ledger_db::LedgerDB;
use sov_modules_api::default_context::DefaultContext;
use sov_rollup_interface::da::{BlobReaderTrait, DaSpec};
Expand All @@ -41,13 +41,10 @@ pub fn initialize_ledger(path: impl AsRef<std::path::Path>) -> LedgerDB {
pub fn get_genesis_config<Da: DaSpec>(
sequencer_da_address: <<Da as DaSpec>::BlobTransaction as BlobReaderTrait>::Address,
) -> GenesisConfig<DefaultContext, Da> {
let hex_key: HexPrivateAndAddress = serde_json::from_slice(include_bytes!(
"../../test-data/keys/token_deployer_private_key.json"
))
.expect("Broken key data file");
let key_and_address: PrivateKeyAndAddress<DefaultContext> = hex_key
.try_into()
.expect("Failed to parse sequencer private key and address");
let data = std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");
let key_and_address: PrivateKeyAndAddress<DefaultContext> = serde_json::from_str(&data)
.unwrap_or_else(|_| panic!("Unable to convert data {} to PrivateKeyAndAddress", &data));
assert!(
key_and_address.is_matching_to_default(),
"Inconsistent key data"
Expand Down
12 changes: 5 additions & 7 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use demo_stf::app::DefaultPrivateKey;
use demo_stf::app::{App, DefaultContext};
use demo_stf::runtime::{get_rpc_methods, GenesisConfig};
use risc0_adapter::host::Risc0Verifier;
#[cfg(feature = "experimental")]
use sov_cli::wallet_state::PrivateKeyAndAddress;
use sov_db::ledger_db::LedgerDB;
#[cfg(feature = "experimental")]
use sov_ethereum::experimental::EthRpcConfig;
Expand Down Expand Up @@ -129,14 +131,10 @@ pub fn new_rollup_with_mock_da_from_config(
pub fn read_tx_signer_priv_key() -> Result<DefaultPrivateKey, anyhow::Error> {
let data = std::fs::read_to_string(TX_SIGNER_PRIV_KEY_PATH).context("Unable to read file")?;

let hex_key: crate::HexPrivateAndAddress =
serde_json::from_str(&data).context("JSON does not have correct format.")?;

let priv_key = sov_modules_api::default_signature::private_key::DefaultPrivateKey::from_hex(
&hex_key.hex_priv_key,
)?;
let key_and_address: PrivateKeyAndAddress<DefaultContext> = serde_json::from_str(&data)
.unwrap_or_else(|_| panic!("Unable to convert data {} to PrivateKeyAndAddress", &data));

Ok(priv_key)
Ok(key_and_address.private_key)
}

impl<Vm: Zkvm, Da: DaService<Error = anyhow::Error> + Clone> Rollup<Vm, Da> {
Expand Down
4 changes: 3 additions & 1 deletion examples/test-data/keys/token_deployer_private_key.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"hex_priv_key": "75fbf8d98746c2692e502942b938c82379fd09ea9f5b60d4d39e87e1b42468fdf8ad2437a279e1c8932c07358c91dc4fe34864a98c6c25f298e2a0199c1509ff",
"private_key": {
"key_pair": [117, 251, 248, 217, 135, 70, 194, 105, 46, 80, 41, 66, 185, 56, 200, 35, 121, 253, 9, 234, 159, 91, 96, 212, 211, 158, 135, 225, 180, 36, 104, 253]
},
"address": "sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94"
}
4 changes: 3 additions & 1 deletion examples/test-data/keys/tx_signer_private_key.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"hex_priv_key": "27c3774d52e71ea266a9c5256cd98b9ae67e62f2ae5ed34a668db8eaa83e1bac61fcf0f466bc20ca3882d46ae07d65227e31cfaefb852bc8f579415247565dd4",
"private_key": {
"key_pair": [39, 195, 119, 77, 82, 231, 30, 162, 102, 169, 197, 37, 108, 217, 139, 154, 230, 126, 98, 242, 174, 94, 211, 74, 102, 141, 184, 234, 168, 62, 27, 172]
},
"address": "sov1dnhqk4mdsj2kwv4xymt8a624xuahfx8906j9usdkx7ensfghndkq8p33f7"
}
2 changes: 1 addition & 1 deletion examples/test-data/requests/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Mint": {
"coins": {
"amount": 3000,
"token_address": "sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"
"token_address": "sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"
},
"minter_address": "sov15vspj48hpttzyvxu8kzq5klhvaczcpyxn6z6k0hwpwtzs4a6wkvqwr57gc"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/test-data/requests/transfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"to": "sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqklh0qh",
"coins": {
"amount": 200,
"token_address": "sov16m8fxq0x5wc5aw75fx9rus2p7g2l22zf4re72c3m058g77cdjemsavg2ft"
"token_address": "sov1zdwj8thgev2u3yyrrlekmvtsz4av4tp3m7dm5mx5peejnesga27svq9m72"
}
}
}
3 changes: 2 additions & 1 deletion module-system/module-implementations/sov-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod genesis;
#[cfg(feature = "native")]
pub mod query;
mod token;
mod utils;
/// Util functions for bank
pub mod utils;

/// Specifies the call methods using in that module.
pub use call::CallMessage;
Expand Down
7 changes: 4 additions & 3 deletions module-system/sov-cli/src/workflows/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,16 @@ pub fn generate_and_save_key<Tx, C: sov_modules_api::Context>(
wallet_state: &mut WalletState<Tx, C>,
) -> Result<(), anyhow::Error> {
let keys = <C as Spec>::PrivateKey::generate();
let public_key = keys.pub_key();
let address = keys.pub_key().to_address::<<C as Spec>::Address>();
let key_and_address = PrivateKeyAndAddress::<C>::from_key(keys);
let public_key = key_and_address.private_key.pub_key();
let address = key_and_address.address.clone();
let key_path = app_dir.as_ref().join(format!("{}.json", address));
println!(
"Generated key pair with address: {}. Saving to {}",
address,
key_path.display()
);
std::fs::write(&key_path, serde_json::to_string(&keys)?)?;
std::fs::write(&key_path, serde_json::to_string(&key_and_address)?)?;
wallet_state
.addresses
.add(address, nickname, public_key, key_path);
Expand Down

0 comments on commit 9efc47a

Please sign in to comment.