Skip to content

Commit

Permalink
fix tests and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 19, 2024
1 parent ad551c9 commit eb6d644
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
16 changes: 15 additions & 1 deletion precompile/modules/initia_stdlib/sources/crypto/secp256k1.move
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module initia_std::secp256k1 {
sig.bytes
}

/// Returns `true` only the signature can verify the public key on the message
/// Returns `true` if the signature can verify the public key on the message
public fun verify(
message: vector<u8>,
public_key: &ECDSACompressedPublicKey,
Expand Down Expand Up @@ -171,6 +171,10 @@ module initia_std::secp256k1 {

/// Returns `true` if `signature` verifies on `public_key` and `message`
/// and returns `false` otherwise.
///
/// - `message`: A 32-byte hashed message.
/// - `public_key`: A compressed public key in bytes.
/// - `signature`: A 64-byte ECDSA signature.
native fun verify_internal(
message: vector<u8>,
public_key: vector<u8>,
Expand Down Expand Up @@ -209,6 +213,16 @@ module initia_std::secp256k1 {
let (_rid, sig_bytes) = sign(msg, sk);
let sig = ecdsa_signature_from_bytes(sig_bytes);
assert!(verify(msg, &pk, &sig), 1);

// Test with an incorrect message
let wrong_msg: vector<u8> = hash::sha2_256(b"wrong message");
assert!(!verify(wrong_msg, &pk, &sig), 2);

// Test with an incorrect signature
let invalid_sig_bytes = sig_bytes;
*std::vector::borrow_mut(&mut invalid_sig_bytes, 0) = 0xFF; // Corrupt the signature
let invalid_sig = ecdsa_signature_from_bytes(invalid_sig_bytes);
assert!(!verify(msg, &pk, &invalid_sig), 3);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions precompile/modules/initia_stdlib/sources/object.move
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ module initia_std::object {
}

/// Create a new object to represent an NFT and return the ConstructorRef.
/// Nft objects can be queried globally by knowing the user generated seed used to create them
/// and the creator's address. NFT objects can be deleted.
public(friend) fun create_nft_object(
owner: address, creator: address, seed: vector<u8>
): ConstructorRef acquires Tombstone {
Expand Down
19 changes: 9 additions & 10 deletions precompile/modules/initia_stdlib/sources/token/collection.move
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,8 @@ module initia_std::collection {
assert!(count(collection) == option::some(0), 0);
}

#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x10007, location = Self)]
fun test_create_collection_with_invalid_name(creator: &signer) {
create_collection_helper(creator, string::utf8(b"collection::hello"));
}

#[test(creator = @0x123, trader = @0x456)]
#[expected_failure(abort_code = 0x50003, location = initia_std::object)]
entry fun test_create_and_transfer(creator: &signer, trader: &signer) {
#[test(creator = @0x123, receipient = @0x456)]
entry fun test_create_and_transfer(creator: &signer, receipient: &signer) {
let creator_address = signer::address_of(creator);
let collection_name = string::utf8(b"collection name");
create_collection_helper(creator, collection_name);
Expand All @@ -558,10 +551,16 @@ module initia_std::collection {
object::transfer(
creator,
collection,
signer::address_of(trader)
signer::address_of(receipient)
);
}

#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x10007, location = Self)]
fun test_create_collection_with_invalid_name(creator: &signer) {
create_collection_helper(creator, string::utf8(b"collection::hello"));
}

#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x80001, location = initia_std::object)]
entry fun test_duplicate_collection(creator: &signer) {
Expand Down
16 changes: 15 additions & 1 deletion precompile/modules/minitia_stdlib/sources/crypto/secp256k1.move
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module minitia_std::secp256k1 {
sig.bytes
}

/// Returns `true` only the signature can verify the public key on the message
/// Returns `true` if the signature can verify the public key on the message
public fun verify(
message: vector<u8>,
public_key: &ECDSACompressedPublicKey,
Expand Down Expand Up @@ -171,6 +171,10 @@ module minitia_std::secp256k1 {

/// Returns `true` if `signature` verifies on `public_key` and `message`
/// and returns `false` otherwise.
///
/// - `message`: A 32-byte hashed message.
/// - `public_key`: A compressed public key in bytes.
/// - `signature`: A 64-byte ECDSA signature.
native fun verify_internal(
message: vector<u8>,
public_key: vector<u8>,
Expand Down Expand Up @@ -209,6 +213,16 @@ module minitia_std::secp256k1 {
let (_rid, sig_bytes) = sign(msg, sk);
let sig = ecdsa_signature_from_bytes(sig_bytes);
assert!(verify(msg, &pk, &sig), 1);

// Test with an incorrect message
let wrong_msg: vector<u8> = hash::sha2_256(b"wrong message");
assert!(!verify(wrong_msg, &pk, &sig), 2);

// Test with an incorrect signature
let invalid_sig_bytes = sig_bytes;
*std::vector::borrow_mut(&mut invalid_sig_bytes, 0) = 0xFF; // Corrupt the signature
let invalid_sig = ecdsa_signature_from_bytes(invalid_sig_bytes);
assert!(!verify(msg, &pk, &invalid_sig), 3);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions precompile/modules/minitia_stdlib/sources/object.move
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ module minitia_std::object {
}

/// Create a new object to represent an NFT and return the ConstructorRef.
/// Nft objects can be queried globally by knowing the user generated seed used to create them
/// and the creator's address. NFT objects can be deleted.
public(friend) fun create_nft_object(
owner: address, creator: address, seed: vector<u8>
): ConstructorRef acquires Tombstone {
Expand Down
20 changes: 10 additions & 10 deletions precompile/modules/minitia_stdlib/sources/token/collection.move
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,8 @@ module minitia_std::collection {
assert!(count(collection) == option::some(0), 0);
}

#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x10007, location = Self)]
fun test_create_collection_with_invalid_name(creator: &signer) {
create_collection_helper(creator, string::utf8(b"collection::hello"));
}

#[test(creator = @0x123, trader = @0x456)]
#[expected_failure(abort_code = 0x50003, location = minitia_std::object)]
entry fun test_create_and_transfer(creator: &signer, trader: &signer) {
#[test(creator = @0x123, receipient = @0x456)]
entry fun test_create_and_transfer(creator: &signer, receipient: &signer) {
let creator_address = signer::address_of(creator);
let collection_name = string::utf8(b"collection name");
create_collection_helper(creator, collection_name);
Expand All @@ -558,10 +551,17 @@ module minitia_std::collection {
object::transfer(
creator,
collection,
signer::address_of(trader)
signer::address_of(receipient)
);
}

#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x10007, location = Self)]
fun test_create_collection_with_invalid_name(creator: &signer) {
create_collection_helper(creator, string::utf8(b"collection::hello"));
}


#[test(creator = @0x123)]
#[expected_failure(abort_code = 0x80001, location = minitia_std::object)]
entry fun test_duplicate_collection(creator: &signer) {
Expand Down

0 comments on commit eb6d644

Please sign in to comment.