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

Multisign with existing transaction #104

Open
J3imip opened this issue Sep 25, 2023 · 5 comments
Open

Multisign with existing transaction #104

J3imip opened this issue Sep 25, 2023 · 5 comments

Comments

@J3imip
Copy link

J3imip commented Sep 25, 2023

I'm getting my transaction from front-end in json-like format:

{
  "visible": false,
  "txID": "4b4c894993ce89901274f18c20a230b9f8dc893ba88f361397e031ff2c25ff0d",
  "raw_data": {
    "contract": [
      {
        "parameter": {
          "value": {
            "data": "a9059cbb0000000000000000000000004459a6b37eac771a6691d7af986a737c24705feb00000000000000000000000000000000000000000000000000000000000003e8",
            "owner_address": "41ab86c6f9504d57d628a28bbc440ce764e85417da",
            "contract_address": "41ea51342dabbb928ae1e576bd39eff8aaf070a8c6"
          },
          "type_url": "type.googleapis.com/protocol.TriggerSmartContract"
        },
        "type": "TriggerSmartContract"
      }
    ],
    "ref_block_bytes": "3b21",
    "ref_block_hash": "c4533393ce34a563",
    "expiration": 1695648204000,
    "fee_limit": 150000000,
    "timestamp": 1695648146110
  },
  "raw_data_hex": "0a023b212208c4533393ce34a56340e08989e4ac315aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a1541ab86c6f9504d57d628a28bbc440ce764e85417da121541ea51342dabbb928ae1e576bd39eff8aaf070a8c62244a9059cbb0000000000000000000000004459a6b37eac771a6691d7af986a737c24705feb00000000000000000000000000000000000000000000000000000000000003e870bec585e4ac31900180a3c347"
}

I want to add 1 more signature and broadcast this transaction, but I can't find examples how to create core.Transaction{} of this json tx and broadcast it. If anyone figured it out, please send me some examples.

I've tried to parse it to my own type structs and cast it to core.Transaction, but it was unsuccessful, I guess because of encoding.

Here is an example how I tried to parse it to structs:

func convertJSONTransactionToTransaction(jsonTx types.InputJSON) (*core.Transaction, error) {
	tx := &core.Transaction{
		RawData: &core.TransactionRaw{
			RefBlockBytes: []byte(jsonTx.RawData.RefBlockBytes),
			RefBlockHash:  []byte(jsonTx.RawData.RefBlockHash),
			Expiration:    jsonTx.RawData.Expiration,
			Timestamp:     jsonTx.RawData.Timestamp,
			FeeLimit:      jsonTx.RawData.FeeLimit,
		},
	}

	value, _ := json.Marshal(jsonTx.RawData.Contract[0].Parameter.Value)

	contract := &core.Transaction_Contract{
		Type: 31,
		Parameter: &anypb.Any{
			TypeUrl: jsonTx.RawData.Contract[0].Parameter.TypeURL,
			Value:   value,
		},
		PermissionId: 0,
	}
	tx.RawData.Contract = []*core.Transaction_Contract{contract}

	return tx, nil
}
@codes0003
Copy link

I encountered the same problem as yours, have you solved it?

@J3imip
Copy link
Author

J3imip commented Nov 9, 2023

I encountered the same problem as yours, have you solved it?

Yes I have. I'll tell you how to do that soon

@codes0003
Copy link

作者

I am very anxious😭😭😭😭😭

@J3imip
Copy link
Author

J3imip commented Nov 9, 2023

I encountered the same problem as yours, have you solved it?

Here is an example how I did that stuff:

...
rawData, err := hex.DecodeString(withdrawal.RawDataHex)
if err != nil {
	return errors.Wrap(err, "failed to marshal rawData")
}

tx := new(core.Transaction)
tx.RawData = new(core.TransactionRaw)
if err := proto.Unmarshal(rawData, tx.RawData); err != nil {
	return errors.Wrap(err, "failed to parse proto")
}

signatures, err := stringArrayToByteSlice(withdrawal.Signatures)
if err != nil {
	return errors.Wrap(err, "failed to unmarshal signatures")
}

h256h := sha256.New()
h256h.Write(rawData)
hash := h256h.Sum(nil)

txHash := hex.EncodeToString(hash)
withdrawal.TxHash = txHash

signature, err := crypto.Sign(hash, p.tronConfig.SignatureKey)
if err != nil {
	return errors.Wrap(err, "failed to create signature")
}

tx.Signature = append(tx.Signature, signatures...)
tx.Signature = append(tx.Signature, signature)

result, err := p.client.WrappedCli.Broadcast(tx)
...

func stringArrayToByteSlice(signaturesString string) ([][]byte, error) {
	var signaturesArray []string

	err := json.Unmarshal([]byte(signaturesString), &signaturesArray)
	if err != nil {
		return nil, err
	}

	var signatures [][]byte

	for _, str := range signaturesArray {
		bytes, err := hex.DecodeString(str)
		if err != nil {
			continue
		}
		signatures = append(signatures, bytes)
	}

	return signatures, nil
}

@codes0003
Copy link

I encountered the same problem as yours, have you solved it?

Here is an example how I did that stuff:

...
rawData, err := hex.DecodeString(withdrawal.RawDataHex)
if err != nil {
	return errors.Wrap(err, "failed to marshal rawData")
}

tx := new(core.Transaction)
tx.RawData = new(core.TransactionRaw)
if err := proto.Unmarshal(rawData, tx.RawData); err != nil {
	return errors.Wrap(err, "failed to parse proto")
}

signatures, err := stringArrayToByteSlice(withdrawal.Signatures)
if err != nil {
	return errors.Wrap(err, "failed to unmarshal signatures")
}

h256h := sha256.New()
h256h.Write(rawData)
hash := h256h.Sum(nil)

txHash := hex.EncodeToString(hash)
withdrawal.TxHash = txHash

signature, err := crypto.Sign(hash, p.tronConfig.SignatureKey)
if err != nil {
	return errors.Wrap(err, "failed to create signature")
}

tx.Signature = append(tx.Signature, signatures...)
tx.Signature = append(tx.Signature, signature)

result, err := p.client.WrappedCli.Broadcast(tx)
...

func stringArrayToByteSlice(signaturesString string) ([][]byte, error) {
	var signaturesArray []string

	err := json.Unmarshal([]byte(signaturesString), &signaturesArray)
	if err != nil {
		return nil, err
	}

	var signatures [][]byte

	for _, str := range signaturesArray {
		bytes, err := hex.DecodeString(str)
		if err != nil {
			continue
		}
		signatures = append(signatures, bytes)
	}

	return signatures, nil
}

112#issue-1985969141
Please help me take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants