Skip to content

Commit

Permalink
chore: made uint a helper method to return a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
JCrawsh committed Sep 19, 2023
1 parent 8810dcc commit bddeac7
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 63 deletions.
100 changes: 50 additions & 50 deletions binary-codec/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,63 +140,63 @@ func TestEncode(t *testing.T) {
// output: "6680000000000000000000000000000000000000004C5543000000000020A85019EA62B48F79EB67273B797EB916438FA4",
// expectedErr: nil,
// },
// {
// description: "successfully serialized signed transaction 1",
// input: &transactions.OfferCreate{
// BaseTx: transactions.BaseTx{
// Account: "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys",
// TransactionType: transactions.OfferCreateTx,
// Fee: 10,
// Flags: 524288,
// Sequence: 1752792,
// SigningPubKey: "03EE83BB432547885C219634A1BC407A9DB0474145D69737D09CCDC63E1DEE7FE3",
// TxnSignature: "30440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED282C97D4CE02204CFD241E86F17E011298FC1A39B63386C74306A5DE047E213B0F29EFA4571C2C",
// },
// Expiration: 595640108,
// OfferSequence: 1752791,
// TakerGets: types.XRPCurrencyAmount(15000000000),
// TakerPays: types.IssuedCurrencyAmount{
// Currency: "USD",
// Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
// Value: "7072.8",
// },
// },
// output: "120007220008000024001ABED82A2380BF2C2019001ABED764D55920AC9391400000000000000000000000000055534400000000000A20B3C85F482532A9578DBB3950B85CA06594D165400000037E11D60068400000000000000A732103EE83BB432547885C219634A1BC407A9DB0474145D69737D09CCDC63E1DEE7FE3744630440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED282C97D4CE02204CFD241E86F17E011298FC1A39B63386C74306A5DE047E213B0F29EFA4571C2C8114DD76483FACDEE26E60D8A586BB58D09F27045C46",
// expectedErr: nil,
// },
// {
// description: "successfully serialized signed transaction 2",
// input: &transactions.EscrowFinish{
// BaseTx: transactions.BaseTx{
// Account: "r3Y6vCE8XqfZmYBRngy22uFYkmz3y9eCRA",
// TransactionType: transactions.EscrowFinishTx,
// Fee: 10101,
// Flags: 2147483648,
// Sequence: 1,
// SigningPubKey: "0268D79CD579D077750740FA18A2370B7C2018B2714ECE70BA65C38D223E79BC9C",
// TxnSignature: "3045022100F06FB54049D6D50142E5CF2E2AC21946AF305A13E2A2D4BA881B36484DD01A540220311557EC8BEF536D729605A4CB4D4DC51B1E37C06C93434DD5B7651E1E2E28BF",
// Memos: []transactions.MemoWrapper{
// {
// Memo: transactions.Memo{
// MemoData: "04C4D46544659A2D58525043686174",
// },
// },
// },
// },
// OfferSequence: 11,
// Owner: "r9NpyVfLfUG8hatuCCHKzosyDtKnBdsEN3",
// },
// output: "1200022280000000240000000120190000000B68400000000000277573210268D79CD579D077750740FA18A2370B7C2018B2714ECE70BA65C38D223E79BC9C74473045022100F06FB54049D6D50142E5CF2E2AC21946AF305A13E2A2D4BA881B36484DD01A540220311557EC8BEF536D729605A4CB4D4DC51B1E37C06C93434DD5B7651E1E2E28BF811452C7F01AD13B3CA9C1D133FA8F3482D2EF08FA7D82145A380FBD236B6A1CD14B939AD21101E5B6B6FFA2F9EA7D0F04C4D46544659A2D58525043686174E1F1",
// expectedErr: nil,
// },
{
description: "successfully serialized signed transaction 1",
input: &transactions.OfferCreate{
BaseTx: transactions.BaseTx{
Account: "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys",
TransactionType: transactions.OfferCreateTx,
Fee: 10,
Flags: types.Uint(524288),
Sequence: 1752792,
SigningPubKey: "03EE83BB432547885C219634A1BC407A9DB0474145D69737D09CCDC63E1DEE7FE3",
TxnSignature: "30440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED282C97D4CE02204CFD241E86F17E011298FC1A39B63386C74306A5DE047E213B0F29EFA4571C2C",
},
Expiration: 595640108,
OfferSequence: 1752791,
TakerGets: types.XRPCurrencyAmount(15000000000),
TakerPays: types.IssuedCurrencyAmount{
Currency: "USD",
Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
Value: "7072.8",
},
},
output: "120007220008000024001ABED82A2380BF2C2019001ABED764D55920AC9391400000000000000000000000000055534400000000000A20B3C85F482532A9578DBB3950B85CA06594D165400000037E11D60068400000000000000A732103EE83BB432547885C219634A1BC407A9DB0474145D69737D09CCDC63E1DEE7FE3744630440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED282C97D4CE02204CFD241E86F17E011298FC1A39B63386C74306A5DE047E213B0F29EFA4571C2C8114DD76483FACDEE26E60D8A586BB58D09F27045C46",
expectedErr: nil,
},
{
description: "successfully serialized signed transaction 2",
input: &transactions.EscrowFinish{
BaseTx: transactions.BaseTx{
Account: "r3Y6vCE8XqfZmYBRngy22uFYkmz3y9eCRA",
TransactionType: transactions.EscrowFinishTx,
Fee: 10101,
Flags: types.Uint(2147483648),
Sequence: 1,
SigningPubKey: "0268D79CD579D077750740FA18A2370B7C2018B2714ECE70BA65C38D223E79BC9C",
TxnSignature: "3045022100F06FB54049D6D50142E5CF2E2AC21946AF305A13E2A2D4BA881B36484DD01A540220311557EC8BEF536D729605A4CB4D4DC51B1E37C06C93434DD5B7651E1E2E28BF",
Memos: []transactions.MemoWrapper{
{
Memo: transactions.Memo{
MemoData: "04C4D46544659A2D58525043686174",
},
},
},
},
OfferSequence: 11,
Owner: "r9NpyVfLfUG8hatuCCHKzosyDtKnBdsEN3",
},
output: "1200022280000000240000000120190000000B68400000000000277573210268D79CD579D077750740FA18A2370B7C2018B2714ECE70BA65C38D223E79BC9C74473045022100F06FB54049D6D50142E5CF2E2AC21946AF305A13E2A2D4BA881B36484DD01A540220311557EC8BEF536D729605A4CB4D4DC51B1E37C06C93434DD5B7651E1E2E28BF811452C7F01AD13B3CA9C1D133FA8F3482D2EF08FA7D82145A380FBD236B6A1CD14B939AD21101E5B6B6FFA2F9EA7D0F04C4D46544659A2D58525043686174E1F1",
expectedErr: nil,
},
{
description: "successfully serialized signed transaction 3",
input: &transactions.Payment{
BaseTx: transactions.BaseTx{
Account: "rweYz56rfmQ98cAdRaeTxQS9wVMGnrdsFp",
TransactionType: transactions.PaymentTx,
Fee: 12,
Flags: 0,
Flags: types.Uint(0),
Sequence: 842,
Memos: []transactions.MemoWrapper{
{
Expand Down
12 changes: 11 additions & 1 deletion binary-codec/types/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"encoding/binary"
"reflect"

"github.com/xyield/xrpl-go/binary-codec/serdes"
)
Expand All @@ -13,8 +14,9 @@ type UInt32 struct{}
// FromJson converts a JSON value into a serialized byte slice representing a 32-bit unsigned integer.
// The input value is assumed to be an integer. If the serialization fails, an error is returned.
func (u *UInt32) FromJson(value any) ([]byte, error) {
v := expandInt(value)
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint32(value.(uint)))
err := binary.Write(buf, binary.BigEndian, uint32(v.(uint)))

if err != nil {
return nil, err
Expand All @@ -32,3 +34,11 @@ func (u *UInt32) ToJson(p *serdes.BinaryParser, opts ...int) (any, error) {
}
return int(binary.BigEndian.Uint32(b)), nil
}

func expandInt(v any) any {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
return rv.Elem().Interface()
}
return v
}
4 changes: 2 additions & 2 deletions model/client/admin/signing/sign_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSignForRequest(t *testing.T) {
BaseTx: transactions.BaseTx{
TransactionType: transactions.TrustSetTx,
Account: "rEuLyBCvcw4CFmzv8RepSiAoNgF8tTGJQC",
Flags: 262144,
Flags: types.Uint(262144),
Sequence: 2,
Fee: types.XRPCurrencyAmount(30000),
},
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestSignForResponse(t *testing.T) {
TransactionType: transactions.TrustSetTx,
Fee: types.XRPCurrencyAmount(30000),
Sequence: 2,
Flags: 262144,
Flags: types.Uint(262144),
Signers: []transactions.Signer{
{
SignerData: transactions.SignerData{
Expand Down
2 changes: 1 addition & 1 deletion model/client/admin/signing/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestSignResponse(t *testing.T) {
TransactionType: transactions.TrustSetTx,
Fee: types.XRPCurrencyAmount(30000),
Sequence: 2,
Flags: 262144,
Flags: types.Uint(262144),
Signers: []transactions.Signer{
{
SignerData: transactions.SignerData{
Expand Down
4 changes: 2 additions & 2 deletions model/client/transactions/submit_multisigned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestSubmitMultisignedRequest(t *testing.T) {
TransactionType: "Payment",
Fee: types.XRPCurrencyAmount(10000),
Sequence: 360,
Flags: 2147483648,
Flags: types.Uint(2147483648),
SigningPubKey: "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB",
TxnSignature: "304402200E5C2DD81FDF0BE9AB2A8D797885ED49E804DBF28E806604D878756410CA98B102203349581946B0DDA06B36B35DBC20EDA27552C1F167BCF5C6ECFF49C6A46F8580",
Signers: []transactions.Signer{
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestSubmitMultisignedResponse(t *testing.T) {
TransactionType: "Payment",
Fee: types.XRPCurrencyAmount(10000),
Sequence: 360,
Flags: 2147483648,
Flags: types.Uint(2147483648),
SigningPubKey: "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB",
TxnSignature: "304402200E5C2DD81FDF0BE9AB2A8D797885ED49E804DBF28E806604D878756410CA98B102203349581946B0DDA06B36B35DBC20EDA27552C1F167BCF5C6ECFF49C6A46F8580",
Signers: []transactions.Signer{
Expand Down
2 changes: 1 addition & 1 deletion model/client/transactions/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSubmitResponse(t *testing.T) {
TransactionType: "Payment",
Fee: types.XRPCurrencyAmount(10000),
Sequence: 360,
Flags: 2147483648,
Flags: types.Uint(2147483648),
SigningPubKey: "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB",
TxnSignature: "304402200E5C2DD81FDF0BE9AB2A8D797885ED49E804DBF28E806604D878756410CA98B102203349581946B0DDA06B36B35DBC20EDA27552C1F167BCF5C6ECFF49C6A46F8580",
},
Expand Down
2 changes: 1 addition & 1 deletion model/client/transactions/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestTxResponse(t *testing.T) {
TransactionType: transactions.OfferCreateTx,
Account: "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
Fee: types.XRPCurrencyAmount(12),
Flags: 0,
Flags: types.Uint(0),
LastLedgerSequence: 56865248,
Sequence: 5037710,
SigningPubKey: "03B51A3EDF70E4098DA7FB053A01C5A6A0A163A30ED1445F14F87C7C3295FCB3BE",
Expand Down
2 changes: 1 addition & 1 deletion model/transactions/nftoken_create_offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNFTokenCreateOfferTx(t *testing.T) {
BaseTx: BaseTx{
Account: "rs8jBmmfpwgmrSPgwMsh7CvKRmRt1JTVSX",
TransactionType: NFTokenCreateOfferTx,
Flags: 1,
Flags: types.Uint(1),
},
NFTokenID: "000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007",
Amount: types.XRPCurrencyAmount(1000000),
Expand Down
2 changes: 1 addition & 1 deletion model/transactions/nftoken_mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNFTokenMintTx(t *testing.T) {
Account: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
TransactionType: NFTokenMintTx,
Fee: types.XRPCurrencyAmount(10),
Flags: 8,
Flags: types.Uint(8),
Memos: []MemoWrapper{
{
Memo: Memo{
Expand Down
2 changes: 1 addition & 1 deletion model/transactions/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestPaymentTx(t *testing.T) {
Account: "abc",
TransactionType: PaymentTx,
Fee: types.XRPCurrencyAmount(1000),
Flags: 262144,
Flags: types.Uint(262144),
},
Amount: types.IssuedCurrencyAmount{
Issuer: "def",
Expand Down
2 changes: 1 addition & 1 deletion model/transactions/trust_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestTrustSetTx(t *testing.T) {
Account: "ra5nK24KXen9AHvsdFTKHSANinZseWnPcX",
TransactionType: TrustSetTx,
Fee: types.XRPCurrencyAmount(12),
Flags: 262144,
Flags: types.Uint(262144),
Sequence: 12,
LastLedgerSequence: 8007750,
},
Expand Down
2 changes: 1 addition & 1 deletion model/transactions/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type BaseTx struct {
Fee types.XRPCurrencyAmount `json:",omitempty"`
Sequence uint `json:",omitempty"`
AccountTxnID types.Hash256 `json:",omitempty"`
Flags uint `json:",omitempty"`
Flags *uint `json:",omitempty"`
LastLedgerSequence uint `json:",omitempty"`
Memos []MemoWrapper `json:",omitempty"`
Signers []Signer `json:",omitempty"`
Expand Down
9 changes: 9 additions & 0 deletions model/transactions/types/uint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

// Uint is a helper function that allocates a new uint value
// to store v and returns a pointer to it.
func Uint(v uint) *uint {
p := new(uint)
*p = v
return p
}

0 comments on commit bddeac7

Please sign in to comment.