0

How to create Stellar Muxed accounts using java-stellar-sdk-0.31.0.jar, I want to create Muxed account with id 1 from custodian account GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE

attached my code, I get result but seems very long muxed account, can you please help on this?


 public static String createMuxedAccount(String custacc) throws Exception {

        MuxedAccount.MuxedAccountMed25519 accountMed25519 = new MuxedAccount.MuxedAccountMed25519();
        accountMed25519.setEd25519(new Uint256(custacc.getBytes()));
        accountMed25519.setId(new Uint64(1L));

        MuxedAccount muxedAccount = new MuxedAccount();
        muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519);
        muxedAccount.setMed25519(accountMed25519);

        AccountConverter accountConverter = AccountConverter.enableMuxed();
        String muxedAccountrep = accountConverter.decode(muxedAccount);
        System.out.println(muxedAccountrep);
         return muxedAccountrep;

    }
        String destination = createMuxedAccount("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE");

MBDUETKIKRMDMVSPJFEVGNCGJZIVGTKUKNLVMSCDJVLEGSSYKRKE4WCUJFMTGU2CLBETKSZUJZGEIQ2UKY2UGSSMLJCQAAAAAAAAAAABNKZA

when I use that generated Muxed account the transaction fail:

tx_failed
[op_no_destination]

package com.example.DemoGraphQL.services;


import org.springframework.stereotype.Service;
import org.stellar.sdk.*;
import org.stellar.sdk.Memo;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.SubmitTransactionResponse;
import org.stellar.sdk.xdr.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;


@Service
public class StellarServices {


    public static void main(String[] args) throws Exception {
        sendTransaction();
    }

 

    public static String createMuxedAccount(String custacc) throws Exception {

        MuxedAccount.MuxedAccountMed25519 accountMed25519 = new MuxedAccount.MuxedAccountMed25519();
        accountMed25519.setEd25519(new Uint256(custacc.getBytes()));
        accountMed25519.setId(new Uint64(1L));


        MuxedAccount muxedAccount = new MuxedAccount();
        muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519);
        muxedAccount.setMed25519(accountMed25519);

        AccountConverter accountConverter = AccountConverter.enableMuxed();
        String muxedAccountrep = accountConverter.decode(muxedAccount);

        return muxedAccountrep;

    }

    public static void sendTransaction() throws Exception {
        Server server = new Server("https://horizon-testnet.stellar.org");

        KeyPair source = KeyPair.fromSecretSeed("SC3TVXXRGKXX5ATKLNLZZGHS642MAKFMSU7CYTY55XE3NNBFLJS45TXY");
        String destination = createMuxedAccount("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE");


        AccountResponse sourceAccount = server.accounts().account(source.getAccountId());

// Start building the transaction.
        Transaction transaction = new Transaction.Builder( sourceAccount, Network.TESTNET)
                .addOperation(new PaymentOperation.Builder(destination, new AssetTypeNative(), "10").build())
                // A memo allows you to add your own metadata to a transaction. It's
                // optional and does not affect how Stellar treats the transaction.
                .addMemo(Memo.text("Test Transaction"))
                // Wait a maximum of three minutes for the transaction
                .setTimeout(180)
                // Set the amount of lumens you're willing to pay per operation to submit your transaction
                .setBaseFee(Transaction.MIN_BASE_FEE)
                .build();
// Sign the transaction to prove you are actually the person sending it.
        transaction.sign(source);

// And finally, send it off to Stellar!
        try {
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            System.out.println(response.getHash());
            System.out.println(response.getExtras().getResultCodes().getTransactionResultCode());
            System.out.println(response.getExtras().getResultCodes().getOperationsResultCodes());
            System.out.println(response.getExtras().getResultXdr());
        } catch (Exception e) {
            System.out.println("Something went wrong!");
            System.out.println(e.getMessage());

        }
    }
}



 

There is no clear Java example , like in javascript it's very easy like following

 new sdk.MuxedAccount(custodianAcc, "1")

or

   const StellarBase = require("stellar-base");

  let baseAccount = new StellarBase.Account("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE", '1');
  const mux = new StellarBase.MuxedAccount(baseAccount, '0');

  console.log(JSON.stringify(mux))

and response is :

{"account":{"_accountId":"GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE","sequence":"1"},"_muxedXdr":{"_switch":{"name":"keyTypeMuxedEd25519","value":256},"_arm":"med25519","_value":{"_attributes":{"id":{"low":0,"high":0,"unsigned":true},"ed25519":{"type":"Buffer","data":[88,121,223,213,114,17,46,21,176,147,39,43,84,226,101,68,155,206,109,188,209,141,200,55,71,85,198,172,98,157,122,36]}}}},"_mAddress":"MBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CIAAAAAAAAAAAAASPS","_id":"0"}

in Python it's also clear

account_id = "GAQAA5L65LSYH7CQ3VTJ7F3HHLGCL3DSLAR2Y47263D56MNNGHSQSTVY"
account_muxed_id = 1234
account_muxed = "MAQAA5L65LSYH7CQ3VTJ7F3HHLGCL3DSLAR2Y47263D56MNNGHSQSAAAAAAAAAAE2LP26"
# generate account_muxed
muxed = MuxedAccount(account=account_id, account_muxed_id=1234)

I tried to follow js & the java sdk unit tests documentation: here : 1- https://developers.stellar.org/docs/glossary/muxed-accounts/#what-happens-if-i-get-errors-when-using-muxed-accounts

2- https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md

3- https://github.com/stellar/java-stellar-sdk/blob/master/src/test/java/org/stellar/sdk/OperationTest.java

1 Answer 1

0

I was able to get the Muxed account ID with the length that looks normal MBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CIAAAAAAAAAAAAEC6S

Solved by: changing accountMed25519.setEd25519(new Uint256(custacc.getBytes())); to

accountMed25519.setEd25519(new Uint256(KeyPair.fromAccountId(custacc).getPublicKey())); on the method createMuxedAccount ;

here is the new method body:

 public static String createMuxedAccount(String custacc) throws Exception {

        MuxedAccount.MuxedAccountMed25519 accountMed25519 = new MuxedAccount.MuxedAccountMed25519();

        accountMed25519.setEd25519(new Uint256(KeyPair.fromAccountId(custacc).getPublicKey()));
        accountMed25519.setId(new Uint64(1L));


        MuxedAccount muxedAccount = new MuxedAccount();
        muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519);
        muxedAccount.setMed25519(accountMed25519);

        AccountConverter accountConverter = AccountConverter.enableMuxed();
        String muxedAccountrep = accountConverter.decode(muxedAccount);

        System.out.println(muxedAccountrep);

        return muxedAccountrep;

    }

I found there are many ways to generate muxed accounts based on what you want to use: from secret, public key string, KeyPair etc..


   KeyPair keyPairFromSecret = KeyPair.fromSecretSeed("SC3TVXXRGKXX5ATKLNLZZGHS642MAKFMSU7CYTY55XE3NNBFLJS45TXY");
        KeyPair keyPairFromAccountID = KeyPair.fromAccountId("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE");
        Uint256 uint256 = new Uint256();
        uint256.setUint256(KeyPair.fromAccountId("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE").getPublicKey());
        // need to copy StrKey.java from stellar SDK + Google  Juava
        Uint256 uint2561FromAccountid = StrKey.encodeToXDRMuxedAccount("GBMHTX6VOIIS4FNQSMTSWVHCMVCJXTTNXTIY3SBXI5K4NLDCTV5CJLZE").getEd25519();

        MuxedAccount muxedAccount = new MuxedAccount();
        muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519);
        MuxedAccount.MuxedAccountMed25519 med = new MuxedAccount.MuxedAccountMed25519();

// all of these are working
        med.setEd25519(uint2561FromAccountid); 
        med.setEd25519(uint256);
        med.setEd25519(keyPairFromAccountID.getXdrPublicKey().getEd25519());
        med.setEd25519(keyPairFromSecret.getXdrPublicKey().getEd25519());

        med.setId(new Uint64(1l));
        muxedAccount.setMed25519(med);
        AccountConverter accountConverter = AccountConverter.enableMuxed();
        String muxedAccountrep = accountConverter.decode(muxedAccount);
        System.out.println(muxedAccountrep);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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