Skip to content

Commit

Permalink
Add a callback field. Parsing will return error if a required field i…
Browse files Browse the repository at this point in the history
…s missing. (#38)
  • Loading branch information
zhenlu authored Jul 13, 2024
1 parent 81210f3 commit a9ca02c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions uma/protocol/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
3 changes: 2 additions & 1 deletion uma/test/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestUMAInvoiceTLVAndBech32(t *testing.T) {
SenderUma: nil,
InvoiceLimit: nil,
KycStatus: &kyc,
Callback: "https://example.com/callback",
Signature: &signature,
}

Expand All @@ -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)
}
24 changes: 24 additions & 0 deletions uma/test/tlv_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion uma/utils/tlv_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9ca02c

Please sign in to comment.