Skip to content

Commit

Permalink
nft template fix
Browse files Browse the repository at this point in the history
  • Loading branch information
madMAx43v3r committed Oct 24, 2024
1 parent a3c09b2 commit 2c1c7c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/contract/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function init(creator_)

function add(serial, creator_key, signature) public
{
// TODO: is_uint()
if(serial == 0 || typeof(serial) != 4) {
fail("invalid serial", 1);
}
Expand All @@ -18,7 +19,9 @@ function add(serial, creator_key, signature) public
if(sha256(creator_key) != creator) {
fail("invalid creator", 3);
}
if(!ecdsa_verify(this.user, creator_key, signature)) {
const msg = concat(to_string_bech32(this.address), "/", to_string(serial));

if(!ecdsa_verify(sha256(msg), creator_key, signature)) {
fail("invalid signature", 4);
}
nfts[serial] = this.user;
Expand Down
22 changes: 11 additions & 11 deletions test/vm/test_nft_n.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,45 @@ const template_addr = template.__deploy({
init_args: [creator]
});

const txid = sha256("nft_1");
const signature = to_string_hex(__test.ecdsa_sign(creator_skey, txid));
const msg_0 = concat(to_string_bech32(template_addr), "/", to_string(0));
const signature_0 = to_string_hex(__test.ecdsa_sign(creator_skey, sha256(msg_0)));

// fail due to serial 0
nft_1.__deploy({
__type: "mmx.contract.Executable",
binary: nft_binary,
depends: {template: template_addr},
init_method: "init_n",
init_args: [creator_key_hex, 0, signature, {__test: 1, user: to_string_bech32(txid), assert_fail: true}]
init_args: [creator_key_hex, 0, signature_0, {__test: 1, assert_fail: true}]
});

const msg_1 = concat(to_string_bech32(template_addr), "/", to_string(1));
const signature_1 = to_string_hex(__test.ecdsa_sign(creator_skey, sha256(msg_1)));

const nft_1_addr = nft_1.__deploy({
__type: "mmx.contract.Executable",
binary: nft_binary,
depends: {template: template_addr},
init_method: "init_n",
init_args: [creator_key_hex, 1, signature, {__test: 1, user: to_string_bech32(txid)}]
init_args: [creator_key_hex, 1, signature_1]
});

const txid_test = sha256("nft_test");
const signature_test = to_string_hex(__test.ecdsa_sign(creator_skey, txid_test));

// fail due to duplicate serial
nft_test.__deploy({
__type: "mmx.contract.Executable",
binary: nft_binary,
depends: {template: template_addr},
init_method: "init_n",
init_args: [creator_key_hex, 1, signature_test, {__test: 1, user: to_string_bech32(txid_test), assert_fail: true}]
init_args: [creator_key_hex, 1, signature_1, {__test: 1, assert_fail: true}]
});

const txid_2 = sha256("nft_2");
const signature_2 = to_string_hex(__test.ecdsa_sign(creator_skey, txid_2));
const msg_2 = concat(to_string_bech32(template_addr), "/", to_string(2));
const signature_2 = to_string_hex(__test.ecdsa_sign(creator_skey, sha256(msg_2)));

const nft_2_addr = nft_2.__deploy({
__type: "mmx.contract.Executable",
binary: nft_binary,
depends: {template: template_addr},
init_method: "init_n",
init_args: [creator_key_hex, 2, signature_2, {__test: 1, user: to_string_bech32(txid_2)}]
init_args: [creator_key_hex, 2, signature_2]
});

0 comments on commit 2c1c7c9

Please sign in to comment.