pool/sidecar/codec_test.go
Oliver Gugger 7c3bf310c0
sidecar: add string encoder and decoder
To allow a ticket to be transported easily over any communication
channel we also provide an easy ascii compatible exchange format which
includes a prefix and a checksum in addition to the tlv stream data.
The string encoding chosen is URL safe base64.
2021-02-22 10:26:00 +01:00

68 lines
1.9 KiB
Go

package sidecar
import (
"math/big"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/stretchr/testify/require"
)
var (
hardcodedTicket = "sidecarAAQgHBgUEAwIBAAIBYwMBAgp5CwgAAAAAAAADCQwIAA" +
"AAAAAAA3gNIQLVLm5gAB7Vh7eEvz8Y3CkF2DsvNuSWJj8oOxp1iSh5xw5AAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAFhRGFSEC1S5uYAAe1Ye3hL8_GNwpBdg7Lzbkli" +
"Y_KDsadYkoeccWIQMZ1hb2o3OyT-XUX7KWS-pAVBloIPQe8aCTqcjiNOV89x" +
"5kHyALFiEsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGMAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAISgiKSBjWE0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAA3VE-c="
)
// TestEncodeDecode tests that a ticket can be encoded and decoded from/to a
// string and back.
func TestEncodeDecode(t *testing.T) {
// Test with all struct members set.
ticketMaximal := &Ticket{
ID: [8]byte{7, 6, 5, 4, 3, 2, 1, 0},
Version: Version(99),
State: StateRegistered,
Offer: Offer{
Capacity: 777,
PushAmt: 888,
SignPubKey: testPubKey,
SigOfferDigest: &btcec.Signature{
R: new(big.Int).SetInt64(44),
S: new(big.Int).SetInt64(22),
},
},
Recipient: &Recipient{
NodePubKey: testPubKey,
MultiSigPubKey: testPubKey2,
},
Order: &Order{
BidNonce: [32]byte{11, 22, 33, 44},
SigOrderDigest: &btcec.Signature{
R: new(big.Int).SetInt64(99),
S: new(big.Int).SetInt64(33),
},
},
Execution: &Execution{
PendingChannelID: [32]byte{99, 88, 77},
},
}
serialized, err := EncodeToString(ticketMaximal)
require.NoError(t, err)
deserializedTicket, err := DecodeString(serialized)
require.NoError(t, err)
require.Equal(t, ticketMaximal, deserializedTicket)
// Make sure nothing changed in the encoding without us noticing by
// comparing it to a hard coded version.
deserializedTicket, err = DecodeString(hardcodedTicket)
require.NoError(t, err)
require.Equal(t, ticketMaximal, deserializedTicket)
}