Skip to content

Commit

Permalink
Add test only authority can change metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Aug 30, 2024
1 parent 527ce97 commit 3a767ce
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions chainstate/test-suite/src/tests/fungible_tokens_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5884,3 +5884,138 @@ fn test_change_metadata_uri_activation(#[case] seed: Seed) {
.unwrap();
});
}

#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn only_authority_can_change_metadata_uri(#[case] seed: Seed) {
utils::concurrency::model(move || {
let mut rng = make_seedable_rng(seed);
let mut tf = TestFramework::builder(&mut rng).build();
let genesis_block_id = tf.genesis().get_id();

let (original_sk, original_pk) =
PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);

let issuance = TokenIssuance::V1(TokenIssuanceV1 {
token_ticker: random_ascii_alphanumeric_string(&mut rng, 1..5).as_bytes().to_vec(),
number_of_decimals: rng.gen_range(1..18),
metadata_uri: random_ascii_alphanumeric_string(&mut rng, 1..1024).as_bytes().to_vec(),
total_supply: TokenTotalSupply::Lockable,
authority: Destination::PublicKey(original_pk.clone()),
is_freezable: IsTokenFreezable::No,
});

let (token_id, _, utxo_with_change) = issue_token_from_block(
&mut rng,
&mut tf,
genesis_block_id.into(),
UtxoOutPoint::new(genesis_block_id.into(), 0),
issuance,
);

let new_metadata_uri =
random_ascii_alphanumeric_string(&mut rng, 1..1024).as_bytes().to_vec();

// Try to change metadata without a signature
let tx_1_no_signatures = TransactionBuilder::new()
.add_input(
TxInput::from_command(
AccountNonce::new(0),
AccountCommand::ChangeTokenMetadataUri(token_id, new_metadata_uri),
),
InputWitness::NoSignature(None),
)
.add_input(
utxo_with_change.clone().into(),
InputWitness::NoSignature(None),
)
.add_output(TxOutput::Transfer(
OutputValue::Coin(
tf.chain_config().token_change_authority_fee(BlockHeight::zero()),
),
Destination::AnyoneCanSpend,
))
.build();

let result = tf
.make_block_builder()
.add_transaction(tx_1_no_signatures.clone())
.build_and_process(&mut rng);

assert_eq!(
result.unwrap_err(),
ChainstateError::ProcessBlockError(BlockError::StateUpdateFailed(
ConnectTransactionError::InputCheck(InputCheckError::new(
0,
ScriptError::Signature(DestinationSigError::SignatureNotFound)
))
))
);

let inputs_utxos = vec![
None,
tf.chainstate.utxo(&utxo_with_change).unwrap().map(|utxo| utxo.output().clone()),
];
let inputs_utxos_refs = inputs_utxos.iter().map(|utxo| utxo.as_ref()).collect::<Vec<_>>();

// Try to change metadata with wrong signature
let tx = {
let tx = tx_1_no_signatures.transaction().clone();

let (some_sk, some_pk) = PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr);
let account_sig = StandardInputSignature::produce_uniparty_signature_for_input(
&some_sk,
Default::default(),
Destination::PublicKey(some_pk),
&tx,
&inputs_utxos_refs,
0,
&mut rng,
)
.unwrap();

SignedTransaction::new(
tx,
vec![InputWitness::Standard(account_sig), InputWitness::NoSignature(None)],
)
.unwrap()
};

let result = tf.make_block_builder().add_transaction(tx).build_and_process(&mut rng);

assert_eq!(
result.unwrap_err(),
ChainstateError::ProcessBlockError(BlockError::StateUpdateFailed(
ConnectTransactionError::InputCheck(InputCheckError::new(
0,
ScriptError::Signature(DestinationSigError::SignatureVerificationFailed)
))
))
);

// Change metadata with proper keys
let tx = {
let tx = tx_1_no_signatures.transaction().clone();

let account_sig = StandardInputSignature::produce_uniparty_signature_for_input(
&original_sk,
Default::default(),
Destination::PublicKey(original_pk.clone()),
&tx,
&inputs_utxos_refs,
0,
&mut rng,
)
.unwrap();

SignedTransaction::new(
tx,
vec![InputWitness::Standard(account_sig), InputWitness::NoSignature(None)],
)
.unwrap()
};

tf.make_block_builder().add_transaction(tx).build_and_process(&mut rng).unwrap();
});
}

0 comments on commit 3a767ce

Please sign in to comment.