Skip to content

Commit

Permalink
fix compiling errors introduced by new Ed25119PrivateKey sign funct…
Browse files Browse the repository at this point in the history
…ion (#4200)
  • Loading branch information
simonjiao authored Sep 20, 2024
1 parent b688f2a commit ba0b75c
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 53 deletions.
18 changes: 8 additions & 10 deletions account/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ impl Account {
message: SigningMessage,
chain_id: ChainId,
) -> Result<SignedMessage> {
let authenticator = self
.private_key
.as_ref()
.map(|private_key| private_key.sign_message(&message))
.ok_or_else(|| format_err!("Readonly account can not sign message."))?;
let authenticator = match self.private_key.as_ref() {
Some(private_key) => private_key.sign_message(&message)?,
None => return Err(format_err!("Readonly account can not sign message.")),
};
Ok(SignedMessage::new(
self.addr,
message,
Expand All @@ -139,11 +138,10 @@ impl Account {
}

pub fn sign_txn(&self, raw_txn: RawUserTransaction) -> Result<SignedUserTransaction> {
let signature = self
.private_key
.as_ref()
.map(|private_key| private_key.sign(&raw_txn))
.ok_or_else(|| format_err!("Readonly account can not sign txn"))?;
let signature = match self.private_key.as_ref() {
Some(private_key) => private_key.sign(&raw_txn)?,
None => return Err(format_err!("Readonly account can not sign txn.")),
};
Ok(SignedUserTransaction::new(raw_txn, signature))
}

Expand Down
4 changes: 2 additions & 2 deletions chain/force-upgrade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ForceUpgrade {

assert_eq!(package.init_script().unwrap(), &init_script);

Ok(account.sign_txn(RawUserTransaction::new(
account.sign_txn(RawUserTransaction::new(
*account.address(),
sequence_number,
TransactionPayload::Package(package),
Expand All @@ -58,6 +58,6 @@ impl ForceUpgrade {
block_timestamp_in_secs + DEFAULT_EXPIRATION_TIME,
*chain_id,
STC_TOKEN_CODE_STR.to_string(),
)))
))
}
}
2 changes: 1 addition & 1 deletion cmd/airdrop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ async fn main() -> Result<()> {
now + 60 * 60 * 12,
ChainId::new(chain_id),
);
let signature = private_key.sign(&txn);
let signature = private_key.sign(&txn)?;
let signed_txn = SignedUserTransaction::new(txn, signature);

let signed_txn_hex = hex::encode(signed_txn.encode()?);
Expand Down
2 changes: 1 addition & 1 deletion cmd/db-exporter/src/force_deploy_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn deploy_package(
now_time_by_sec + DEFAULT_EXPIRATION_TIME,
chain_id,
STC_TOKEN_CODE_STR.to_string(),
));
))?;
let ret = starcoin_executor::execute_transactions(
statedb,
vec![Transaction::UserTransaction(signed_transaction)],
Expand Down
2 changes: 1 addition & 1 deletion cmd/genesis-nft-miner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async fn main() -> Result<()> {
now + 60 * 60 * 12,
ChainId::new(chain_id),
);
let signature = private_key.sign(&txn);
let signature = private_key.sign(&txn)?;
let signed_txn = SignedUserTransaction::new(txn, signature);
let signed_txn_hex = hex::encode(signed_txn.encode()?);
let txn_hash: HashValue = txpool_client
Expand Down
4 changes: 2 additions & 2 deletions dataformat-generator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn generate() -> Result<(), Error> {
let pri_key = Ed25519PrivateKey::generate_for_testing();
tracer.trace_value(&mut samples, &pri_key)?;
tracer.trace_value(&mut samples, &pri_key.public_key())?;
tracer.trace_value(&mut samples, &pri_key.sign(&DummyObj::default()))?;
tracer.trace_value(&mut samples, &pri_key.sign(&DummyObj::default())?)?;

tracer.trace_value::<AuthenticationKey>(
&mut samples,
Expand All @@ -63,7 +63,7 @@ fn generate() -> Result<(), Error> {
let pri_key = MultiEd25519PrivateKey::generate_for_testing();
tracer.trace_value(&mut samples, &pri_key)?;
tracer.trace_value(&mut samples, &pri_key.public_key())?;
tracer.trace_value(&mut samples, &pri_key.sign(&DummyObj::default()))?;
tracer.trace_value(&mut samples, &pri_key.sign(&DummyObj::default())?)?;
}

tracer.trace_type::<BlockMetadata>(&samples)?;
Expand Down
3 changes: 2 additions & 1 deletion test-helper/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ fn build_signed_txn(
payload: TransactionPayload,
) -> SignedUserTransaction {
let txn = build_raw_txn(user_address, state, payload, None);
let signature = prikey.sign(&txn);
// It's ok to unwrap here, we just build the txn, and this function is only used for testing purpose.
let signature = prikey.sign(&txn).unwrap();
SignedUserTransaction::new(txn, signature)
}

Expand Down
9 changes: 5 additions & 4 deletions types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ impl Account {
expiration_timestamp_secs,
chain_id,
);
let signature = self.private_key.sign(&raw_txn);
// It's ok to unwrap here, because this function is only used `db-exporter` and tests
let signature = self.private_key.sign(&raw_txn).unwrap();
SignedUserTransaction::new(raw_txn, signature)
}

Expand All @@ -225,9 +226,9 @@ impl Account {
)
}

pub fn sign_txn(&self, raw_txn: RawUserTransaction) -> SignedUserTransaction {
let signature = self.private_key.sign(&raw_txn);
SignedUserTransaction::new(raw_txn, signature)
pub fn sign_txn(&self, raw_txn: RawUserTransaction) -> anyhow::Result<SignedUserTransaction> {
let signature = self.private_key.sign(&raw_txn)?;
Ok(SignedUserTransaction::new(raw_txn, signature))
}
}

Expand Down
3 changes: 2 additions & 1 deletion vm/starcoin-transactional-test-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ impl<'a> StarcoinTestAdapter<'a> {
fn sign(&self, raw_txn: RawUserTransaction) -> SignedUserTransaction {
let keypair = genesis_key_pair();
let account_private_key: AccountPrivateKey = keypair.0.into();
let auth = account_private_key.sign(&raw_txn);
// It's ok to unwrap here because this is only used in tests
let auth = account_private_key.sign(&raw_txn).unwrap();
SignedUserTransaction::new(raw_txn, auth)
}

Expand Down
46 changes: 25 additions & 21 deletions vm/transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,29 @@ pub fn peer_to_peer_v2(
amount: u128,
net: &ChainNetwork,
) -> SignedUserTransaction {
sender.sign_txn(RawUserTransaction::new_with_default_gas_token(
*sender.address(),
seq_num,
TransactionPayload::ScriptFunction(ScriptFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("TransferScripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
vec![
bcs_ext::to_bytes(&recipient).unwrap(),
bcs_ext::to_bytes(&amount).unwrap(),
],
)),
10000000,
1,
1000 + 60 * 60,
net.chain_id(),
))
// It's ok to unwrap here, because we know the script exists in the stdlib.
sender
.sign_txn(RawUserTransaction::new_with_default_gas_token(
*sender.address(),
seq_num,
TransactionPayload::ScriptFunction(ScriptFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("TransferScripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
vec![
bcs_ext::to_bytes(&recipient).unwrap(),
bcs_ext::to_bytes(&amount).unwrap(),
],
)),
10000000,
1,
1000 + 60 * 60,
net.chain_id(),
))
.unwrap()
}

//this only work for DEV or TEST
Expand Down Expand Up @@ -1076,6 +1079,7 @@ pub fn build_signed_empty_txn(
expiration_timestamp_secs,
chain_id,
);
let signature = prikey.sign(&txn);
// It's ok to unwrap here, signing an empty txn should never fail.
let signature = prikey.sign(&txn).unwrap();
SignedUserTransaction::new(txn, signature)
}
2 changes: 1 addition & 1 deletion vm/types/src/proptest_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl KeyPairHolder {
pub fn sign_txn(&self, txn: RawUserTransaction) -> Result<SignedUserTransaction> {
Ok(match self {
Self::Ed25519(private_key, public_key) => {
let signature = private_key.sign(&txn);
let signature = private_key.sign(&txn)?;
SignedUserTransaction::ed25519(txn, public_key.clone(), signature)
}
Self::MultiEd25519(private_key, public_key) => {
Expand Down
10 changes: 5 additions & 5 deletions vm/types/src/transaction/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,18 +461,18 @@ impl AccountPrivateKey {
}
}

pub fn sign<T: CryptoHash + Serialize>(&self, message: &T) -> TransactionAuthenticator {
match self {
pub fn sign<T: CryptoHash + Serialize>(&self, message: &T) -> Result<TransactionAuthenticator> {
Ok(match self {
Self::Single(key) => {
TransactionAuthenticator::ed25519(key.public_key(), key.sign(message))
TransactionAuthenticator::ed25519(key.public_key(), key.sign(message)?)
}
Self::Multi(key) => {
TransactionAuthenticator::multi_ed25519(key.public_key(), key.sign(message).into())
}
}
})
}

pub fn sign_message(&self, message: &SigningMessage) -> TransactionAuthenticator {
pub fn sign_message(&self, message: &SigningMessage) -> Result<TransactionAuthenticator> {
self.sign(message)
}
}
Expand Down
2 changes: 1 addition & 1 deletion vm/types/src/transaction/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn create_user_txn<T: TransactionSigner + ?Sized>(

impl TransactionSigner for KeyPair<Ed25519PrivateKey, Ed25519PublicKey> {
fn sign_txn(&self, raw_txn: RawUserTransaction) -> Result<SignedUserTransaction> {
let signature = self.private_key.sign(&raw_txn);
let signature = self.private_key.sign(&raw_txn)?;
Ok(SignedUserTransaction::ed25519(
raw_txn,
self.public_key.clone(),
Expand Down
5 changes: 3 additions & 2 deletions vm/types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl RawUserTransaction {
private_key: &Ed25519PrivateKey,
public_key: Ed25519PublicKey,
) -> Result<SignatureCheckedTransaction> {
let signature = private_key.sign(&self);
let signature = private_key.sign(&self)?;
Ok(SignatureCheckedTransaction(SignedUserTransaction::ed25519(
self, public_key, signature,
)))
Expand Down Expand Up @@ -608,7 +608,8 @@ impl Sample for SignedUserTransaction {
fn sample() -> Self {
let raw_txn = RawUserTransaction::sample();
let (private_key, public_key) = genesis_key_pair();
let signature = private_key.sign(&raw_txn);
// It's ok to unwrap for a sample.
let signature = private_key.sign(&raw_txn).unwrap();
Self::ed25519(raw_txn, public_key, signature)
}
}
Expand Down

0 comments on commit ba0b75c

Please sign in to comment.