Skip to content

Commit

Permalink
[address] Merge AccountAddress and ReceiptIdentifier, now, AccountAdd…
Browse files Browse the repository at this point in the history
…ress == ReceiptIdentifier (starcoinorg#2627)

* [address] Merge AccountAddress and ReceiptIdentifier, now, AccountAddress == ReceiptIdentifier

* [document] Update document about address and receipt identifier.
  • Loading branch information
jolestar authored and naughtyvenom committed Jul 19, 2021
1 parent ab20d63 commit 1af4100
Show file tree
Hide file tree
Showing 34 changed files with 148 additions and 331 deletions.
87 changes: 44 additions & 43 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions account/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use starcoin_types::{
transaction::authenticator::AuthenticationKey,
};

use starcoin_types::receipt_identifier::ReceiptIdentifier;
pub use starcoin_types::transaction::authenticator::{
AccountPrivateKey, AccountPublicKey, AccountSignature,
};
Expand All @@ -21,7 +20,7 @@ pub struct AccountInfo {
pub is_default: bool,
pub is_readonly: bool,
pub public_key: AccountPublicKey,
pub receipt_identifier: ReceiptIdentifier,
pub receipt_identifier: String,
}

impl AccountInfo {
Expand All @@ -31,13 +30,12 @@ impl AccountInfo {
is_default: bool,
is_readonly: bool,
) -> Self {
let auth_key = public_key.authentication_key();
Self {
address,
public_key,
is_default,
is_readonly,
receipt_identifier: ReceiptIdentifier::V1(address, Some(auth_key)),
receipt_identifier: address.to_bech32(),
}
}

Expand All @@ -54,13 +52,12 @@ impl AccountInfo {
let (_private_key, public_key) = key_gen.generate_keypair();
let address = account_address::from_public_key(&public_key);
let account_public_key = AccountPublicKey::Single(public_key);
let auth_key = account_public_key.authentication_key();
AccountInfo {
address,
is_default: false,
is_readonly: false,
public_key: account_public_key,
receipt_identifier: ReceiptIdentifier::V1(address, Some(auth_key)),
receipt_identifier: address.to_bech32(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/starcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ starcoin-genesis = { path = "../../genesis" }
starcoin-resource-viewer = { path = "../../vm/resource-viewer" }
starcoin-service-registry = { path = "../../commons/service-registry" }
starcoin-move-explain = { path = "../../vm/move-explain" }
errmapgen = { git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0" }
errmapgen = { git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21" }
network-api = {path = "../../network/api", package="network-api"}
starcoin-network-rpc-api = {path = "../../network-rpc/api"}
short-hex-str = { git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0" }
short-hex-str = { git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21" }


[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions cmd/starcoin/src/account/derive_account_address_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use serde::Serialize;
use starcoin_crypto::ed25519::Ed25519PublicKey;
use starcoin_crypto::ValidCryptoMaterialStringExt;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::receipt_identifier::ReceiptIdentifier;
use starcoin_types::transaction::authenticator::AuthenticationKey;
use starcoin_vm_types::transaction::authenticator::AccountPublicKey;
use structopt::StructOpt;
Expand Down Expand Up @@ -65,11 +64,12 @@ impl CommandAction for DeriveAddressCommand {
.collect::<Vec<_>>();
AccountPublicKey::multi(pubkeys, threshold)?
};

let address = account_key.derived_address();
let receipt_identifier = address.to_bech32();
Ok(DerivedAddressData {
address: account_key.derived_address(),
address,
auth_key: account_key.authentication_key(),
receipt_identifier: account_key.receipt_identifier(),
receipt_identifier,
public_key: account_key,
})
}
Expand All @@ -79,6 +79,6 @@ impl CommandAction for DeriveAddressCommand {
pub struct DerivedAddressData {
pub address: AccountAddress,
pub auth_key: AuthenticationKey,
pub receipt_identifier: ReceiptIdentifier,
pub receipt_identifier: String,
pub public_key: AccountPublicKey,
}
10 changes: 6 additions & 4 deletions cmd/starcoin/src/account/generate_keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use serde::Serialize;
use starcoin_crypto::keygen::KeyGen;
use starcoin_crypto::ValidCryptoMaterialStringExt;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::receipt_identifier::ReceiptIdentifier;
use starcoin_types::transaction::authenticator::AuthenticationKey;
use starcoin_vm_types::transaction::authenticator::{AccountPrivateKey, AccountPublicKey};
use std::convert::TryInto;
Expand Down Expand Up @@ -65,10 +64,13 @@ impl CommandAction for GenerateKeypairCommand {
let (private_key, public_key) = key_gen.generate_keypair();
let account_public_key = AccountPublicKey::single(public_key);
let account_private_key = AccountPrivateKey::Single(private_key);

let address = account_public_key.derived_address();
let receipt_identifier = address.to_bech32();
GenerateKeypairData {
address: account_public_key.derived_address(),
address,
auth_key: account_public_key.authentication_key(),
receipt_identifier: account_public_key.receipt_identifier(),
receipt_identifier,
public_key: account_public_key,
private_key: account_private_key
.to_encoded_string()
Expand All @@ -85,7 +87,7 @@ impl CommandAction for GenerateKeypairCommand {
pub struct GenerateKeypairData {
pub address: AccountAddress,
pub auth_key: AuthenticationKey,
pub receipt_identifier: ReceiptIdentifier,
pub receipt_identifier: String,
pub public_key: AccountPublicKey,
pub private_key: String,
}
26 changes: 8 additions & 18 deletions cmd/starcoin/src/account/receipt_identifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::view::AddressOrReceipt;
use crate::StarcoinOpt;
use anyhow::Result;
use scmd::{CommandAction, ExecContext};
use serde::Deserialize;
use serde::Serialize;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::receipt_identifier::ReceiptIdentifier;
use structopt::StructOpt;

/// Encode or decode the receipt_identifier
#[derive(Debug, StructOpt)]
#[structopt(name = "receipt-identifier")]
pub struct ReceiptIdentifierOpt {
#[structopt(name = "address_or_receipt")]
address_or_receipt: AddressOrReceipt,
address_or_receipt: AccountAddress,
}

pub struct ReceiptIdentifierCommand;
Expand All @@ -33,24 +31,16 @@ impl CommandAction for ReceiptIdentifierCommand {
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
match opt.address_or_receipt {
AddressOrReceipt::Address(address) => {
let receipt_identifier = ReceiptIdentifier::v1(address);
Ok(ReceiptIdentifierData {
address,
receipt_identifier,
})
}
AddressOrReceipt::Receipt(receipt_identifier) => Ok(ReceiptIdentifierData {
address: receipt_identifier.address(),
receipt_identifier,
}),
}

Ok(ReceiptIdentifierData {
address: opt.address_or_receipt.to_hex(),
receipt_identifier: opt.address_or_receipt.to_bech32(),
})
}
}

#[derive(Debug, Clone, Hash, Serialize, Deserialize)]
pub struct ReceiptIdentifierData {
pub address: AccountAddress,
pub receipt_identifier: ReceiptIdentifier,
pub address: String,
pub receipt_identifier: String,
}
9 changes: 5 additions & 4 deletions cmd/starcoin/src/account/show_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::view::{AccountWithStateView, AddressOrReceipt};
use crate::view::AccountWithStateView;
use crate::StarcoinOpt;
use anyhow::{format_err, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::{HashValue, ValidCryptoMaterialStringExt};
use starcoin_rpc_client::RemoteStateReader;
use starcoin_state_api::AccountStateReader;
use starcoin_vm_types::account_address::AccountAddress;
use std::collections::HashMap;
use structopt::StructOpt;

Expand All @@ -17,8 +18,8 @@ use structopt::StructOpt;
#[structopt(name = "show")]
pub struct ShowOpt {
#[structopt(name = "address_or_receipt")]
/// The account's address or receipt to show, if absent, show the default account.
address_or_receipt: Option<AddressOrReceipt>,
/// The account's address to show, if absent, show the default account.
address_or_receipt: Option<AccountAddress>,

#[structopt(name = "block_id", short = "b")]
block_id: Option<HashValue>,
Expand All @@ -39,7 +40,7 @@ impl CommandAction for ShowCommand {
let client = ctx.state().client();
let opt = ctx.opt();
let account_address = if let Some(address_or_receipt) = opt.address_or_receipt {
address_or_receipt.address()
address_or_receipt
} else {
let default_account = client
.account_default()?
Expand Down
9 changes: 5 additions & 4 deletions cmd/starcoin/src/account/transfer_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::view::{AddressOrReceipt, ExecuteResultView, TransactionOptions};
use crate::view::{ExecuteResultView, TransactionOptions};
use crate::StarcoinOpt;
use anyhow::Result;
use scmd::{CommandAction, ExecContext};
use starcoin_types::account_address::AccountAddress;
use starcoin_vm_types::token::stc::STC_TOKEN_CODE;
use starcoin_vm_types::token::token_code::TokenCode;
use starcoin_vm_types::transaction::TransactionPayload;
Expand All @@ -16,8 +17,8 @@ use structopt::StructOpt;
#[structopt(name = "transfer")]
pub struct TransferOpt {
#[structopt(short = "r", long = "receiver", alias = "receipt")]
/// transfer to, accept address (start with 0x) or receipt_identifier (start with stc1)
receiver: AddressOrReceipt,
/// transfer to, accept address (start with 0x) or receipt_identifier (start with stc)
receiver: AccountAddress,

#[structopt(short = "k", name = "public-key", long = "public-key")]
/// this option is deprecated
Expand Down Expand Up @@ -51,7 +52,7 @@ impl CommandAction for TransferCommand {
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
let receiver_address = opt.receiver.address();
let receiver_address = opt.receiver;

let token_code = opt
.token_code
Expand Down
8 changes: 3 additions & 5 deletions cmd/starcoin/src/dev/get_coin_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::view::AddressOrReceipt;
use crate::StarcoinOpt;
use anyhow::{bail, format_err, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_executor::{DEFAULT_EXPIRATION_TIME, DEFAULT_MAX_GAS_AMOUNT};
use starcoin_rpc_api::types::SignedUserTransactionView;
use starcoin_rpc_client::RemoteStateReader;
use starcoin_state_api::AccountStateReader;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config;
use std::convert::TryInto;
use structopt::StructOpt;
Expand All @@ -32,7 +32,7 @@ pub struct GetCoinOpt {

#[structopt(name = "address_or_receipt")]
/// The account's address or receipt to send coin, if absent, send to the default account.
address_or_receipt: Option<AddressOrReceipt>,
address_or_receipt: Option<AccountAddress>,
}

pub struct GetCoinCommand;
Expand All @@ -57,9 +57,7 @@ impl CommandAction for GetCoinCommand {
}
let client = ctx.state().client();
let node_info = client.node_info()?;
let to = ctx
.state()
.get_account_or_default(opt.address_or_receipt.map(|add| add.address()))?;
let to = ctx.state().get_account_or_default(opt.address_or_receipt)?;

let association_address = account_config::association_address();
let chain_state_reader = RemoteStateReader::new(client)?;
Expand Down
44 changes: 0 additions & 44 deletions cmd/starcoin/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,14 @@ use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config::{DepositEvent, MintEvent, WithdrawEvent};
use starcoin_types::contract_event::ContractEvent;
use starcoin_types::language_storage::TypeTag;
use starcoin_types::receipt_identifier::ReceiptIdentifier;
use starcoin_vm_types::account_config::events::accept_token_payment::AcceptTokenEvent;
use starcoin_vm_types::account_config::{BlockRewardEvent, ProposalCreatedEvent, VoteChangedEvent};
use starcoin_vm_types::event::EventKey;
use starcoin_vm_types::move_resource::MoveResource;
use starcoin_vm_types::vm_status::VMStatus;
use std::collections::HashMap;
use std::str::FromStr;
use structopt::StructOpt;

#[derive(Clone, Copy, Debug)]
pub enum AddressOrReceipt {
Address(AccountAddress),
Receipt(ReceiptIdentifier),
}

impl AddressOrReceipt {
pub fn is_address(&self) -> bool {
matches!(self, AddressOrReceipt::Address(_))
}

pub fn address(&self) -> AccountAddress {
match self {
AddressOrReceipt::Address(address) => *address,
AddressOrReceipt::Receipt(receipt) => receipt.address(),
}
}

pub fn is_receipt(&self) -> bool {
matches!(self, AddressOrReceipt::Receipt(_))
}

pub fn as_receipt(&self) -> Option<ReceiptIdentifier> {
match self {
AddressOrReceipt::Receipt(receipt) => Some(*receipt),
_ => None,
}
}
}

impl FromStr for AddressOrReceipt {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if s.starts_with("stc") {
AddressOrReceipt::Receipt(ReceiptIdentifier::decode(s)?)
} else {
AddressOrReceipt::Address(AccountAddress::from_hex_literal(s)?)
})
}
}

#[derive(Debug, Clone, StructOpt, Default)]
pub struct TransactionOptions {
#[structopt(short = "s", long)]
Expand Down
4 changes: 2 additions & 2 deletions commons/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ serde = { version = "1.0.126" }
serde_bytes = "0.11.5"
hex = "0.4.3"
anyhow = "1.0"
diem-crypto = { package="diem-crypto", git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0", features = ["fuzzing"] }
diem-crypto-derive = { package="diem-crypto-derive", git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0" }
diem-crypto = { package="diem-crypto", git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21", features = ["fuzzing"] }
diem-crypto-derive = { package="diem-crypto-derive", git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21" }
bcs-ext = { package="bcs-ext", path = "../bcs_ext" }
crypto-macro = { package="starcoin-crypto-macro", path = "./crypto-macro"}
rand = "0.8.4"
Expand Down
2 changes: 1 addition & 1 deletion commons/proptest-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
crossbeam = "0.7.3"
diem-proptest-helpers = { package="diem-proptest-helpers", git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0" }
diem-proptest-helpers = { package="diem-proptest-helpers", git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21" }

proptest = "1.0.0"
proptest-derive = "0.3.0"
2 changes: 1 addition & 1 deletion config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ starcoin-vm-types = { path = "../vm/types" }
starcoin-uint = { path = "../types/uint" }
network-p2p-types = { path = "../network-p2p/types"}
starcoin-logger = {path = "../commons/logger", package="starcoin-logger"}
diem-temppath = { git = "https://github.com/starcoinorg/diem", rev="b5d4b28697d26b330e5f77296f92080214b601c0" }
diem-temppath = { git = "https://github.com/starcoinorg/diem", rev="11dc6d890b48af4f85c1a75bbe690501e8994e21" }
starcoin-system = {path = "../commons/system", package="starcoin-system"}
network-api = {path = "../network/api", package="network-api"}
stdlib = { path = "../vm/stdlib"}
2 changes: 1 addition & 1 deletion developer.starcoin.org/content/cli/account_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ starcoin% account create -p my-pass
+--------------------+------------------------------------------------------------------------------------------+
| public_key | 0x36012395c6ddf99cc4e6d60f35ac24b59c3a930d5e2611ac39d8bdfac2bfecf4 |
+--------------------+------------------------------------------------------------------------------------------+
| receipt_identifier | stc1psy4p48y0qwsq37t2usft4f5mapaeav0uh6uzu37raz2c7nz692tcz2s6nj8s8gqgl94wgy4656d7szgl0sw |
| receipt_identifier | stc1psy4p48y0qwsq37t2usft4f5maq7nrgfm |
+--------------------+------------------------------------------------------------------------------------------+
```

Expand Down
Loading

0 comments on commit 1af4100

Please sign in to comment.