JavaScript SDK Asset creation

Hi team,

When I follow the instruction of Assets creation inside https://github.com/algorand/js-algorand-sdk, using txn=algosdk.makeAssetCreateTxn(all parameters),

I got this error “Error: address seems to be malformed”. I inspect txn object it seems all address (from, assetManager, etc) are all in decoded format (public key + checksum), which is not the address in String when I construct txn for sending Algos. I can modify the txn object after the makeAssetCreateTxn with the address in String. Then this error is gone.

Is it right to have these address in decoded format (public key + checksum) or should they remain the address in String format?

Thanks in advance.

kc

Welcome back @kctam great to see you here building. Please confirm your SDK version and how you are instantiating the client. From SDK release 1.6.1 Algodv2 is the recommended client:

let algodClient = new algosdk.Algodv2(algod_token, algod_server, algod_port);

Reference:

Thanks Ryan. Yes I’m using v2 right now. I can use v2 to send Algos. I’m just start working on the ASA creation. I’m using SDK 1.7.2.

Some further input.

When I build txn object with simply object definition, i.e., let txn = {...}, the txn.from is the address in String format.

When I build txn object with algosdk.makeAssetCreateTxn(), the txn.from becomes a decoded format, which is {public key, checksum}. Here I encounter the “malformed address” error message.

Just would like to know if it is why I see the “malformed address” problem.

Thanks.

Help me understand more about your signing account. Never post your mnemonic passphrase!

Here is an article for Working with ASA Using JavaScript. Below is a bit of the code from the tutorial related to getting from the mnemonic to the signing key for use in creating the ASA. Note: the tutorial uses the makeAssetCreateTxnWithSuggestedParams() method.

let algodclient = new algosdk.Algodv2(token, server, port);

var account1_mnemonic = "PASTE your phrase for account 1";
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
console.log("Account One: " + recoveredAccount1.addr);
let addr = recoveredAccount1.addr;

// signing and sending "txn" allows "addr" to create an asset
let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(addr, note,
         totalIssuance, decimals, defaultFrozen, manager, reserve, freeze,
         clawback, unitName, assetName, assetURL, assetMetadataHash, params);

Share some of your code leading up to the malformed address.

Hi Ryan,
Here is mine:


var account1_mnemonic = "25-WORD-MNEMONIC";
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
console.log("Account One: " + recoveredAccount1.addr);
let addr = recoveredAccount1.addr;
( async() => {
    let params = await algodclient.getTransactionParams().do();

    let defaultFrozen = false;
    let totalIssuance = 1000; 
    let decimals = 0; 
    let reserve = addr;
    let freeze = addr; 
    let clawback = addr; 
    let manager = addr; 
    let unitName = "DC1";
    let assetName = "dumbcoin001";
    let note = new Uint8Array(0);
    let assetURL = "http://someurl";
    let assetMetadataHash = "";

    let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(addr, note,
         totalIssuance, decimals, defaultFrozen, manager, reserve, freeze,
         clawback, unitName, assetName, assetURL, assetMetadataHash, params);
    console.log(txn);
    let signedTxn = algosdk.signTransaction(txn, recoveredAccount1.sk);
    console.log(signedTxn);
})().catch( e=> {
    console.log(e)
})

From console log I saw a complete txn is shown. Here is the first several lines and you can see the txn.from is not address in String but a decoded format.

Account One: 5SL7MUMPYFNDBHUD4L7J4LJRQCQWXQUD3RDK7SBGIDWPH3ZFK47BTH43HE
Transaction {
  name: 'Transaction',
  tag: <Buffer 54 58>,
  from:
   { publicKey:
      Uint8Array [
        236,
        151,
<<truncated>>

and it seems the line signedTxn causing the malformed address error:
Error message: Error: address seems to be malformed at Object.<anonymous> (/Users/kctam/playground/testAlgorand/node_modules/algosdk/src/encoding/address.js:13:33)

Thanks for your help in advance!

The raw signed tx looks a little different then in my example. Mine uses txn.signTxn(recoveredAccount1.sk) instead of what yours uses, algosdk.signTransaction(txn, recoveredAccount1.sk)

try this and see if it works.

    let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(addr, note,
         totalIssuance, decimals, defaultFrozen, manager, reserve, freeze,
        clawback, unitName, assetName, assetURL, assetMetadataHash, params);

    let rawSignedTxn = txn.signTxn(recoveredAccount1.sk)
    let tx = (await algodclient.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);

Complete source available here: https://github.com/algorand/docs/blob/master/examples/assets/v2/javascript/AssetExample.js

The js sdk source code confirms the expected value of the creator account is a string representation of Algorand address of the sender

Here is my source on that… https://github.com/algorand/js-algorand-sdk/blob/develop/src/makeTxn.js

Note: I was able to recreate the error with your code, thank you for providing. The code I provided above works for me instead.

Thanks @rfustino. It seems the object txn created with makeAssetCreateTxnWithSuggestedParams comes with the method signTxn, which is different from if txn is simple JS object using algodsdk.signTransaction, I use the latter when sending Algos to other account before.

I will test it again.

Once again, thanks a lot.

kc

Hi Team,

I finally can create ASA through the JS SDK. Lessons learned:
(1) use signTxn instead of algodsdk.signTransaction when handling Txn object created by makeAssetCreateTxnWithSuggestedParams.
(2) Although assetMetadataHash is optional, it must contain 32-char data. I make it empty and the transaction fails.

Special thanks to @rfustino and @ryanRfox for your prompt help.

cheers,
kc

1 Like