Giter Club home page Giter Club logo

Comments (13)

liweiww123 avatar liweiww123 commented on August 15, 2024

if the "sync" is set to true,return 400.
the detail:
{ "code":65538, "message":"{"codespace":1,"code":2,"abci_code":65538,"message":"tx parse error"}" }

how can i find the problem,please。

from java-sdk.

chainwhisper avatar chainwhisper commented on August 15, 2024

if the "sync" is set to true,return 400.
the detail:
{ "code":65538, "message":"{"codespace":1,"code":2,"abci_code":65538,"message":"tx parse error"}" }

how can i find the problem,please。

hey, there

could you tell me which full node did you use for broadcast?

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

i used this base url: https://testnet-dex.binance.org
so node info which from:
https://docs.binance.org/api-reference/dex-api/paths.html#apiv1node-info

chainId used for encryption is "Binance-Chain-Nile"

There is a problem, it is set “sync” to false, the request transfer is successful, return the transaction hash, but the query is not exist。

Thank you for your reply

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

Below are the parameters I requested.
{
"accountNumber":"404312",
"amount":"1.0",
"chainId":"Binance-Chain-Nile",
"fromAddress":"tbnb16hywxpvvkaz6cecjz89mf2w0da3vfeg6z6yky2",
"memo":"大吉大利,今晚吃鸡",
"option_memo":"test",
"option_source":1,
"privateKey":"0f652de68eb8048951c753303f6a3a22114ff7d4418ecbe1711d5d6a02b50522",
"publicKeyForSign":"61rphyECRrQCmvlOnO2w1K3DE59k8GNqqLYnozBmEnQ7iNBiK/M=",
"sequence":"17",
"symbol":"BNB",
"toAddress":"tbnb104z54sq8zpcxefd5t2xqmujqzju0fc9j974yya"
}

the privateKey and address was for your demo.

Thanks

from java-sdk.

abelliumnt avatar abelliumnt commented on August 15, 2024

It seems that your transaction has not been encoded correctly. Can you share your code to build transaction and encode transacion.

from java-sdk.

abelliumnt avatar abelliumnt commented on August 15, 2024

{
"accountNumber":"404312",
"amount":"1.0",
"chainId":"Binance-Chain-Nile",
"fromAddress":"tbnb16hywxpvvkaz6cecjz89mf2w0da3vfeg6z6yky2",
"memo":"大吉大利,今晚吃鸡",
"option_memo":"test",
"option_source":1,
"privateKey":"0f652de68eb8048951c753303f6a3a22114ff7d4418ecbe1711d5d6a02b50522",
"publicKeyForSign":"61rphyECRrQCmvlOnO2w1K3DE59k8GNqqLYnozBmEnQ7iNBiK/M=",
"sequence":"17",
"symbol":"BNB",
"toAddress":"tbnb104z54sq8zpcxefd5t2xqmujqzju0fc9j974yya"
}

You transaction schema is wrong. Please refter to this examples: https://github.com/binance-chain/java-sdk/tree/master/src/test/java/com/binance/dex/api/client/examples

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

Thanks your replay.
I checked it many times and found no problems. It was modified according to your demo.

The parameters used
{
"accountNumber":"404312",
"amount":"1.0",
"chainId":"Binance-Chain-Nile",
"fromAddress":"tbnb16hywxpvvkaz6cecjz89mf2w0da3vfeg6z6yky2",
"memo":"大吉大利,今晚吃鸡",
"option_memo":"test",
"option_source":1,
"privateKey":"0f652de68eb8048951c753303f6a3a22114ff7d4418ecbe1711d5d6a02b50522",
"publicKeyForSign":"61rphyECRrQCmvlOnO2w1K3DE59k8GNqqLYnozBmEnQ7iNBiK/M=",
"sequence":"17",
"symbol":"BNB",
"toAddress":"tbnb104z54sq8zpcxefd5t2xqmujqzju0fc9j974yya"
}

  1. //getTransferPayLoad
    RequestBody requestBody =BnbUtils.getTransferPayload(params);
    2.//Initiate request
    broadcast(true, requestBody);
    3.//result:
    {"code":65540,"message":"{"codespace":1,"code":4,"abci_code":65540,"message":"signature verification failed"}"}

Below are My Key code :

`
public class BnbUtils {

private static final BigDecimal MULTIPLY_FACTOR = BigDecimal.valueOf(1e8);
private static final BigDecimal MAX_NUMBER = new BigDecimal(Long.MAX_VALUE);
private static final okhttp3.MediaType MEDIA_TYPE = okhttp3.MediaType.parse("text/plain; charset=utf-8");


public static RequestBody getTransferPayload(TransferParams params) {
    LogT.e(JSON.toJSONString(params));

    String symbol = params.getSymbol();
    String toAddress = params.getToAddress();
    String fromAddress = params.getFromAddress();
    String amount = params.getAmount();
    byte[] data = params.getOption_data();
    String chainId = params.getChainId();
    String sequence = params.getSequence();
    String memo = params.getMemo();
    long source = params.getOption_source();
    String option_memo = params.getOption_memo();
    String accountNumber = params.getAccountNumber();
    String privateKey = params.getPrivateKey();
    byte[] publicKeyForSign = params.getPublicKeyForSign();

    String buildTransferPayload = "";
    Transfer transfer = new Transfer();
    transfer.setCoin(symbol);
    transfer.setFromAddress(fromAddress);
    transfer.setToAddress(toAddress);
    transfer.setAmount(amount);
    TransferMessage msgBean = createTransferMessage(transfer);

    Log.e("BnbUtils:", "msgBean:" + msgBean.toString());
    try {
        byte[] msg = encodeTransferMessage(msgBean);
        byte[] sign = sign(msgBean, chainId, accountNumber, sequence, option_memo, source, data, privateKey);
        byte[] signature = encodeSignature(sign, publicKeyForSign, accountNumber, sequence);
        byte[] stdTx = encodeStdTx(msg, signature, data, memo, source);
        buildTransferPayload = EncodeUtils.bytesToHex(stdTx);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return RequestBody.create(MEDIA_TYPE, buildTransferPayload);
}


static byte[] sign(BinanceDexTransactionMessage msg, String chainId, String accountNumber, String sequence, String memo, long source, byte[] data, String privateKey) {
    SignData sd = new SignData();
    sd.setChainId(chainId);
    sd.setAccountNumber(accountNumber);
    sd.setSequence(sequence);
    sd.setMsgs(new BinanceDexTransactionMessage[]{msg});
    sd.setMemo(memo);
    sd.setSource(String.valueOf(source));
    if (data != null) {
        sd.setData(data);
    }
    try {
        return Crypto.sign(EncodeUtils.toJsonEncodeBytes(sd), ECKey.fromPrivate(new BigInteger(privateKey, 16)));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}


static byte[] encodeStdTx(byte[] msg, byte[] signature, byte[] data, String memo, long source) {
    StdTx.Builder stdTxBuilder = StdTx.newBuilder()
            .addMsgs(ByteString.copyFrom(msg))
            .addSignatures(ByteString.copyFrom(signature))
            .setMemo(memo)
            .setSource(source);

    if (data != null) {
        stdTxBuilder = stdTxBuilder.setData(ByteString.copyFrom(data));
    }
    StdTx stdTx = stdTxBuilder.build();
    try {
        return EncodeUtils.aminoWrap(stdTx.toByteArray(), MessageType.StdTx.getTypePrefixBytes(), true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

private static byte[] encodeSignature(byte[] signatureBytes, byte[] publickeyForSign, String accountNumber, String sequence) throws IOException {
    StdSignature stdSignature = StdSignature.newBuilder().setPubKey(ByteString.copyFrom(publickeyForSign))
            .setSignature(ByteString.copyFrom(signatureBytes))
            .setAccountNumber(Long.valueOf(accountNumber))
            .setSequence(Long.valueOf(sequence))
            .build();

    return EncodeUtils.aminoWrap(
            stdSignature.toByteArray(), MessageType.StdSignature.getTypePrefixBytes(), false);
}


private static byte[] encodeTransferMessage(TransferMessage msg)
        throws IOException {
    Send.Builder builder = Send.newBuilder();
    for (InputOutput input : msg.getInputs()) {
        builder.addInputs(toProtoInput(input));
    }
    for (InputOutput output : msg.getOutputs()) {
        builder.addOutputs(toProtoOutput(output));
    }
    Send proto = builder.build();
    return EncodeUtils.aminoWrap(proto.toByteArray(), MessageType.Send.getTypePrefixBytes(), false);
}


private static long doubleToLong(String d) {
    BigDecimal encodeValue = new BigDecimal(d).multiply(MULTIPLY_FACTOR);
    if (encodeValue.compareTo(BigDecimal.ZERO) <= 0) {
        throw new IllegalArgumentException(d + " is less or equal to zero.");
    }
    if (encodeValue.compareTo(MAX_NUMBER) > 0) {
        throw new IllegalArgumentException(d + " is too large.");
    }
    return encodeValue.longValue();

}


private static TransferMessage createTransferMessage(Transfer transfer) {
    Token token = new Token();
    token.setDenom(transfer.getCoin());
    token.setAmount(doubleToLong(transfer.getAmount()));
    List<Token> coins = Collections.singletonList(token);

    InputOutput input = new InputOutput();
    input.setAddress(transfer.getFromAddress());
    input.setCoins(coins);
    InputOutput output = new InputOutput();
    output.setAddress(transfer.getToAddress());
    output.setCoins(coins);

    TransferMessage msgBean = new TransferMessage();
    msgBean.setInputs(Collections.singletonList(input));
    msgBean.setOutputs(Collections.singletonList(output));
    return msgBean;
}

private static Send.Input toProtoInput(InputOutput input) {
    byte[] address = Crypto.decodeAddress(input.getAddress());
    Send.Input.Builder builder =
            Send.Input.newBuilder().setAddress(ByteString.copyFrom(address));

    for (Token coin : input.getCoins()) {
        Send.Token protCoin =
                Send.Token.newBuilder().setAmount(coin.getAmount())
                        .setDenom(coin.getDenom()).build();
        builder.addCoins(protCoin);
    }
    return builder.build();
}

private static Send.Output toProtoOutput(InputOutput output) {
    byte[] address = Crypto.decodeAddress(output.getAddress());
    Send.Output.Builder builder =
            Send.Output.newBuilder().setAddress(ByteString.copyFrom(address));

    for (Token coin : output.getCoins()) {
        Send.Token protCoin =
                Send.Token.newBuilder().setAmount(coin.getAmount())
                        .setDenom(coin.getDenom()).build();
        builder.addCoins(protCoin);
    }
    return builder.build();
}

}
`

from java-sdk.

abelliumnt avatar abelliumnt commented on August 15, 2024

What the the error message now? signature verification failed or tx parse error? If error message is signature verification failed, please verify the chainid and signature encoding method.

from java-sdk.

abelliumnt avatar abelliumnt commented on August 15, 2024

Maybe you miss this https://github.com/binance-chain/java-sdk/blob/4835db8da48971af5a328a5f805277d185e5d3eb/src/main/java/com/binance/dex/api/client/impl/BinanceDexApiNodeClientImpl.java#L151

By the way, why didnit you use the sdk to build and broadcast transaction? I saw that you tried to do duplicated work as this sdk.

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

What the the error message now? signature verification failed or tx parse error? If error message is signature verification failed, please verify the chainid and signature encoding method.

Now error message:signature verification failed
I tried the chainId of the test net was "Binance-Chain-Nile" and the chainId from demo "test-chain-n4b735", result were same error.

and the signature encoding method just above, mostly copy form your demo,and checked it many times and found no problems.

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

Maybe you miss this

https://github.com/binance-chain/java-sdk/blob/4835db8da48971af5a328a5f805277d185e5d3eb/src/main/java/com/binance/dex/api/client/impl/BinanceDexApiNodeClientImpl.java#L151

By the way, why didnit you use the sdk to build and broadcast transaction? I saw that you tried to do duplicated work as this sdk.

yes ,because our android project has itself design and network framework,cannot be used directly.

I use the broadcast transaction API was: "api/v1/broadcast", I saw your demo was no add the "0x" before the encryption result.

i saw the demo , only use "/broadcast_tx_commit" and "/broadcast_tx_sync" need to add "0x" before the result..

so , my problem was happen request the "api/v1/broadcast". when i add the "0x" before the result , the request result was:"{"code":400,"message":"encoding/hex: invalid byte: U+0078 'x'"}"...

so , I am a little confused

from java-sdk.

abelliumnt avatar abelliumnt commented on August 15, 2024

Can you verify whether the publicKeyForSign and private key are matched?

from java-sdk.

liweiww123 avatar liweiww123 commented on August 15, 2024

yes ,I think the problem is here.
i had transfer success!
thanks you very much!

from java-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.