7

I'm converting IPFS multi hash to hex, and after throwing away its prefix I want to attach that hash to a stellar transaction (something like here). I build it this way:

let transaction = new StellarSdk.TransactionBuilder(account)
    .addOperation(StellarSdk.Operation.payment({
        destination: supply,
        asset: StellarSdk.Asset.native(),
        amount: MIN,
    }))
    .addMemo(StellarSdk.Memo.hash(hash))
    .build();
transaction.sign(pair);

At runtime let's say the hash value is:

8c339e15bd89e7f8c7d6c754b8016a202bba12a8cdb4aabe39c6c9e30507a8f3

But after consensus has been reached, I retrieve the same transaction and it shows a different value for the memo hash:

memo: "jDOeFb2J5/jH1sdUuAFqICu6EqjNtKq+OcbJ4wUHqPM="
memo_type: "hash"

Why?

1

1 Answer 1

11

While I'm not sure why this happens, this is not a different hash, its just encoded in base64

   >>> import base64
   >>> base64.b64decode('jDOeFb2J5/jH1sdUuAFqICu6EqjNtKq+OcbJ4wUHqPM=').hex()
   '8c339e15bd89e7f8c7d6c754b8016a202bba12a8cdb4aabe39c6c9e30507a8f3'
6
  • I'm doing it like this bs58.decode(multiHash).slice(2).toString('hex'); and get the hex string, this is strange Commented Jul 21, 2018 at 17:05
  • Base64 encoding is what is used to store the data on the blockchain as it is more compact. In most cases, in js-stellar-sdk, the values are stored as buffer. You can get back to hex using something like buffer.toString('hex') Commented Jul 21, 2018 at 18:40
  • 1
    @MisterTicot What's stored on the blockchain is the actual 256-bit binary string. Base64 encoding is just used as a representation to get printable characters. Commented Jul 22, 2018 at 1:36
  • 3
    Ok after retrieving the memo hash it is sufficient to do this let buffer = Buffer.from(txs[0].memo, 'base64'); let string = buffer.toString('hex'); Commented Jul 22, 2018 at 9:09
  • 1
    @JohanStén Of course you're right. Thank you for the correction. Commented Jul 23, 2018 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.