From a9ca02cbb0561bb4ae29cdc4e787bc209f8af100 Mon Sep 17 00:00:00 2001 From: Zhen Lu Date: Fri, 12 Jul 2024 17:39:32 -0700 Subject: [PATCH] Add a callback field. Parsing will return error if a required field is missing. (#38) --- uma/protocol/invoice.go | 3 +++ uma/test/protocol_test.go | 3 ++- uma/test/tlv_utils_test.go | 24 ++++++++++++++++++++++++ uma/utils/tlv_utils.go | 6 +++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/uma/protocol/invoice.go b/uma/protocol/invoice.go index 0c7b560..a9b2d17 100644 --- a/uma/protocol/invoice.go +++ b/uma/protocol/invoice.go @@ -65,6 +65,9 @@ type UmaInvoice struct { // KYC status of the receiver, default is verified. KycStatus *KycStatus `tlv:"11"` + // The callback url that the sender should send the PayRequest to. + Callback string `tlv:"12"` + // The signature of the UMA invoice Signature *[]byte `tlv:"100"` } diff --git a/uma/test/protocol_test.go b/uma/test/protocol_test.go index a2964c4..7fb01b4 100644 --- a/uma/test/protocol_test.go +++ b/uma/test/protocol_test.go @@ -294,6 +294,7 @@ func TestUMAInvoiceTLVAndBech32(t *testing.T) { SenderUma: nil, InvoiceLimit: nil, KycStatus: &kyc, + Callback: "https://example.com/callback", Signature: &signature, } @@ -307,5 +308,5 @@ func TestUMAInvoiceTLVAndBech32(t *testing.T) { bech32String, err := invoice2.ToBech32String() require.NoError(t, err) - require.Equal(t, "uma1qqxzgen0daqxyctj9e3k7mgpy33nwcesxanx2cedvdnrqvpdxsenzced8ycnve3dxe3nzvmxvv6xyd3evcusypp3xqcrqqcnqqp4256yqyy425eqg3hkcmrpwgpqzfqyqucnqvpsxqcrqpgpqyrpkcm0d4cxc6tpde3k2w3393jk6ctfdsarqtrwv9kk2w3squpnqt3npvqnxeqfwd5kwmnpw36hyega7x5zz", bech32String) + require.Equal(t, "uma1qqxzgen0daqxyctj9e3k7mgpy33nwcesxanx2cedvdnrqvpdxsenzced8ycnve3dxe3nzvmxvv6xyd3evcusypp3xqcrqqcnqqp4256yqyy425eqg3hkcmrpwgpqzfqyqucnqvpsxqcrqpgpqyrpkcm0d4cxc6tpde3k2w3393jk6ctfdsarqtrwv9kk2w3squpnqt3npvqnxrqudp68gurn8ghj7etcv9khqmr99e3k7mf0vdskcmrzv93kkeqfwd5kwmnpw36hyeg0e4m4j", bech32String) } diff --git a/uma/test/tlv_utils_test.go b/uma/test/tlv_utils_test.go index 229ab8f..efc8252 100644 --- a/uma/test/tlv_utils_test.go +++ b/uma/test/tlv_utils_test.go @@ -3,6 +3,7 @@ package uma_test import ( "testing" + "github.com/stretchr/testify/require" "github.com/uma-universal-money-address/uma-go-sdk/uma/utils" ) @@ -28,6 +29,12 @@ type TLVUtilsTests struct { OptionalEmptyStringField *string `tlv:"7"` } +type TLVUtilsTestsMissingField struct { + StringField string `tlv:"0"` + IntField int `tlv:"1"` + BoolField bool `tlv:"2"` +} + func (d *TLVUtilsTests) MarshalTLV() ([]byte, error) { return utils.MarshalTLV(d) } @@ -161,3 +168,20 @@ func TestNestedTLVCoder(t *testing.T) { } } + +func TestMissingFieldTLVCoder(t *testing.T) { + tlvUtilsTests := TLVUtilsTestsMissingField{ + StringField: "hello", + IntField: 42, + BoolField: true, + } + + data, err := utils.MarshalTLV(&tlvUtilsTests) + if err != nil { + t.Fatal(err) + } + + var tlvUtilsTests2 TLVUtilsTests + err = utils.UnmarshalTLV(&tlvUtilsTests2, data) + require.Error(t, err) +} diff --git a/uma/utils/tlv_utils.go b/uma/utils/tlv_utils.go index 7f19812..2a7303c 100644 --- a/uma/utils/tlv_utils.go +++ b/uma/utils/tlv_utils.go @@ -176,7 +176,11 @@ func UnmarshalTLV(v interface{}, data []byte) error { content, ok := result[byte(tlv)] if !ok { - continue + if field.Kind() == reflect.Ptr { + continue + } else { + return fmt.Errorf("missing TLV %d", tlv) + } } err = handle(field, content)