Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that assetMetaDataHash is 32 bytes #253

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/makeTxn.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function makeKeyRegistrationTxnWithSuggestedParamsFromObject(o) {
* @param unitName - string units name for this asset
* @param assetName - string name for this asset
* @param assetURL - string URL relating to this asset
* @param assetMetadataHash - string representation of some sort of hash commitment with respect to the asset
* @param assetMetadataHash - Uint8Array or UTF-8 string representation of a hash commitment with respect to the asset. Must be exactly 32 bytes long.
* @param rekeyTo - rekeyTo address, optional
* @Deprecated in version 2.0 this will change to use the "WithSuggestedParams" signature.
* @returns {Transaction}
Expand Down Expand Up @@ -200,7 +200,7 @@ function makeAssetCreateTxn(from, fee, firstRound, lastRound, note, genesisHash,
* @param unitName - string units name for this asset
* @param assetName - string name for this asset
* @param assetURL - string URL relating to this asset
* @param assetMetadataHash - string representation of some sort of hash commitment with respect to the asset
* @param assetMetadataHash - Uint8Array or UTF-8 string representation of a hash commitment with respect to the asset. Must be exactly 32 bytes long.
* @param suggestedParams - a dict holding common-to-all-txns args:
* fee - integer fee per byte, in microAlgos. for a flat fee, set flatFee to true
* flatFee - bool optionally set this to true to specify fee as microalgos-per-txn
Expand Down
13 changes: 13 additions & 0 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const NUM_ADDL_BYTES_AFTER_SIGNING = 75; // NUM_ADDL_BYTES_AFTER_SIGNING is the
const ALGORAND_TRANSACTION_LEASE_LABEL_LENGTH = 5
const ALGORAND_TRANSACTION_ADDRESS_LENGTH = 32;
const ALGORAND_TRANSACTION_REKEY_LABEL_LENGTH = 5;
const ASSET_METADATA_HASH_LENGTH = 32;
/**
* Transaction enables construction of Algorand transactions
* */
Expand Down Expand Up @@ -94,6 +95,18 @@ class Transaction {
if (!Number.isSafeInteger(foreignAssetIndex) || foreignAssetIndex < 0) throw Error("each foreign asset index must be a positive number and smaller than 2^53-1");
});
}
if (assetMetadataHash !== undefined && assetMetadataHash.length !== 0) {
if (typeof(assetMetadataHash) === 'string') {
const encoded = Buffer.from(assetMetadataHash);
if (encoded.byteLength !== ASSET_METADATA_HASH_LENGTH) {
throw Error("assetMetadataHash must be a " + ASSET_METADATA_HASH_LENGTH + " byte Uint8Array or string.");
}
assetMetadataHash = new Uint8Array(encoded);
} else if (assetMetadataHash.constructor !== Uint8Array || assetMetadataHash.byteLength !== ASSET_METADATA_HASH_LENGTH)
throw Error("assetMetadataHash must be a " + ASSET_METADATA_HASH_LENGTH + " byte Uint8Array or string.");
} else {
assetMetadataHash = undefined;
}
if (note !== undefined) {
if (note.constructor !== Uint8Array) throw Error("note must be a Uint8Array.");
}
Expand Down
102 changes: 100 additions & 2 deletions tests/5.Transaction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

const assert = require('assert');
const { Buffer } = require('buffer');
const algosdk = require('../index');
const group = require('../src/group');

Expand Down Expand Up @@ -210,7 +211,7 @@ describe('Sign', function () {
"assetUnitName": "tests",
"assetName": "testcoin",
"assetURL": "testURL",
"assetMetadataHash": "metadatahash",
"assetMetadataHash": new Uint8Array(Buffer.from("ZkFDUE80blJnTzU1ajFuZEFLM1c2U2djNEFQa2N5Rmg=", "base64")),
"assetManager": address,
"assetReserve": address,
"assetFreeze": address,
Expand Down Expand Up @@ -430,7 +431,7 @@ describe('Sign', function () {
let unitName = "tst";
let assetName = "testcoin";
let assetURL = "testURL";
let assetMetadataHash = "testhash";
let assetMetadataHash = new Uint8Array(Buffer.from("dGVzdGhhc2gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", "base64"));
let genesisID = "";
let firstRound = 322575;
let lastRound = 322575;
Expand Down Expand Up @@ -464,6 +465,103 @@ describe('Sign', function () {
assert.deepStrictEqual(expectedTxn, actualTxn);
});

it('should fail to make an asset create transaction with an invalid assetMetadataHash', function() {
let addr = "BH55E5RMBD4GYWXGX5W5PJ5JAHPGM5OXKDQH5DC4O2MGI7NW4H6VOE4CP4";
let fee = 10;
let defaultFrozen = false;
let genesisHash = "SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=";
let total = 100;
let decimals = 0;
let reserve = addr;
let freeze = addr;
let clawback = addr;
let unitName = "tst";
let assetName = "testcoin";
let assetURL = "testURL";
let genesisID = "";
let firstRound = 322575;
let lastRound = 322575;
let note = new Uint8Array([123, 12, 200]);
let rekeyTo = "GAQVB24XEPYOPBQNJQAE4K3OLNYTRYD65ZKR3OEW5TDOOGL7MDKABXHHTM";
let txnTemplate = {
"from": addr,
"fee": fee,
"firstRound": firstRound,
"lastRound": lastRound,
"note": note,
"genesisHash": genesisHash,
"assetTotal": total,
"assetDecimals": decimals,
"assetDefaultFrozen": defaultFrozen,
"assetUnitName": unitName,
"assetName": assetName,
"assetURL": assetURL,
"assetManager": addr,
"assetReserve": reserve,
"assetFreeze": freeze,
"assetClawback": clawback,
"genesisID": genesisID,
"rekeyTo": rekeyTo,
"type": "acfg"
};
assert.doesNotThrow(() => {
let txnParams = {
assetMetadataHash: '',
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.throws(() => {
let txnParams = {
assetMetadataHash: 'abc',
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.doesNotThrow(() => {
let txnParams = {
assetMetadataHash: 'fACPO4nRgO55j1ndAK3W6Sgc4APkcyFh',
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.throws(() => {
let txnParams = {
assetMetadataHash: 'fACPO4nRgO55j1ndAK3W6Sgc4APkcyFh1',
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.doesNotThrow(() => {
let txnParams = {
assetMetadataHash: new Uint8Array(0),
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.throws(() => {
let txnParams = {
assetMetadataHash: new Uint8Array([1, 2, 3]),
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.doesNotThrow(() => {
let txnParams = {
assetMetadataHash: new Uint8Array(32),
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
assert.throws(() => {
let txnParams = {
assetMetadataHash: new Uint8Array(33),
...txnTemplate
};
new algosdk.Transaction(txnParams);
});
});

it('should be able to use helper to make an asset config transaction', function() {
let addr = "BH55E5RMBD4GYWXGX5W5PJ5JAHPGM5OXKDQH5DC4O2MGI7NW4H6VOE4CP4";
let fee = 10;
Expand Down