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

DRAFT: Wip binary encode txs #43

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,775 changes: 1,324 additions & 451 deletions binary-codec/definitions/definitions.json

Large diffs are not rendered by default.

95 changes: 48 additions & 47 deletions binary-codec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"encoding/hex"
"errors"
"reflect"
"strings"

"github.com/xyield/xrpl-go/binary-codec/definitions"
"github.com/xyield/xrpl-go/model/transactions"

"github.com/xyield/xrpl-go/binary-codec/serdes"
"github.com/xyield/xrpl-go/binary-codec/types"
Expand All @@ -20,51 +21,47 @@ const (
txSigPrefix = "53545800"
)

// Encode converts a JSON transaction object to a hex string in the canonical binary format.
// The binary format is defined in XRPL's core codebase.
func Encode(json map[string]any) (string, error) {

st := &types.STObject{}

// Iterate over the keys in the provided JSON
for k := range json {

// Get the FieldIdNameMap from the definitions package
fh := definitions.Get().Fields[k]

// If the field is not found in the FieldIdNameMap, delete it from the JSON

if fh == nil {
delete(json, k)
continue
}
func encode(tx transactions.Tx, onlySigning bool, mutations map[string]types.FieldMutation) (string, error) {
st := &types.STObject{
OnlySigning: onlySigning,
Mutations: mutations,
}

b, err := st.FromJson(json)
b, err := st.FromJson(tx)
if err != nil {
return "", err
}

return strings.ToUpper(hex.EncodeToString(b)), nil
}

// Encode converts a JSON transaction object to a hex string in the canonical binary format.
// The binary format is defined in XRPL's core codebase.
func Encode(tx transactions.Tx) (string, error) {
return encode(tx, false, nil)
}

// EncodeForMultiSign: encodes a transaction into binary format in preparation for providing one
// signature towards a multi-signed transaction.
// (Only encodes fields that are intended to be signed.)
func EncodeForMultisigning(json map[string]any, xrpAccountID string) (string, error) {
func EncodeForMultisigning(tx transactions.Tx, xrpAccountID string) (string, error) {

st := &types.AccountID{}

// SigningPubKey is required for multi-signing but should be set to empty string.

json["SigningPubKey"] = ""

suffix, err := st.FromJson(xrpAccountID)
if err != nil {
return "", err
}

encoded, err := Encode(removeNonSigningFields(json))
// SigningPubKey is required for multi-signing but should be set to empty string.
err = setFieldFromTx(tx, "SigningPubKey", "placeholder", func(v any) bool {
return v.(string) == ""
})
if err != nil {
return "", err
}
encoded, err := encode(tx, true, map[string]types.FieldMutation{
"SigningPubKey": types.Zero(),
})

if err != nil {
return "", err
Expand All @@ -74,9 +71,9 @@ func EncodeForMultisigning(json map[string]any, xrpAccountID string) (string, er
}

// Encodes a transaction into binary format in preparation for signing.
func EncodeForSigning(json map[string]any) (string, error) {
func EncodeForSigning(tx transactions.Tx) (string, error) {

encoded, err := Encode(removeNonSigningFields(json))
encoded, err := encode(tx, true, nil)

if err != nil {
return "", err
Expand All @@ -86,20 +83,20 @@ func EncodeForSigning(json map[string]any) (string, error) {
}

// EncodeForPaymentChannelClaim: encodes a payment channel claim into binary format in preparation for signing.
func EncodeForSigningClaim(json map[string]any) (string, error) {
func EncodeForSigningClaim(tx transactions.PaymentChannelClaim) (string, error) {

if json["Channel"] == nil || json["Amount"] == nil {
if tx.Channel == "" || tx.Amount == 0 {
return "", ErrSigningClaimFieldNotFound
}

channel, err := types.NewHash256().FromJson(json["Channel"])
channel, err := types.NewHash256().FromJson(tx.Channel)

if err != nil {
return "", err
}

t := &types.Amount{}
amount, err := t.FromJson(json["Amount"])
amount, err := t.FromJson(tx.Amount)

if err != nil {
return "", err
Expand All @@ -113,20 +110,6 @@ func EncodeForSigningClaim(json map[string]any) (string, error) {
return strings.ToUpper(paymentChannelClaimPrefix + hex.EncodeToString(channel) + hex.EncodeToString(amount)), nil
}

// removeNonSigningFields removes the fields from a JSON transaction object that should not be signed.
func removeNonSigningFields(json map[string]any) map[string]any {

for k := range json {
fi, _ := definitions.Get().GetFieldInstanceByFieldName(k)

if fi != nil && !fi.IsSigningField {
delete(json, k)
}
}

return json
}

// Decode decodes a hex string in the canonical binary format into a JSON transaction object.
func Decode(hexEncoded string) (map[string]any, error) {
b, err := hex.DecodeString(hexEncoded)
Expand All @@ -142,3 +125,21 @@ func Decode(hexEncoded string) (map[string]any, error) {

return m.(map[string]any), nil
}

// Overwrites a field in a transaction with a new value if condition is met.
func setFieldFromTx(tx transactions.Tx, fieldName string, value any, condition func(any) bool) error {
rv := reflect.ValueOf(tx)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
} else {
return errors.New("invalid transaction")
}
if !rv.FieldByName(fieldName).IsValid() {
return errors.New("invalid field name")
}
if condition != nil && condition(rv.FieldByName(fieldName).Interface()) {
rv.FieldByName(fieldName).Set(reflect.ValueOf(value))
return nil
}
return nil
}
Loading
Loading