Skip to content

[group key addrs 7/7]: send and receive support for V2 addresses #1587

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions authmailbox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *Client) Stop() error {
// and txProof is the proof of the transaction that contains the message.
func (c *Client) SendMessage(ctx context.Context, receiverKey btcec.PublicKey,
encryptedPayload []byte, txProof proof.TxProof,
expiryBlockHeight uint32) (uint64, error) {
expiryBlockDelta uint32) (uint64, error) {

if c.stopped.Load() {
return 0, ErrClientShutdown
Expand All @@ -164,7 +164,7 @@ func (c *Client) SendMessage(ctx context.Context, receiverKey btcec.PublicKey,
Proof: &mboxrpc.SendMessageRequest_TxProof{
TxProof: rpcProof,
},
ExpiryBlockHeight: expiryBlockHeight,
ExpiryBlockDelta: expiryBlockDelta,
})
if err != nil {
return 0, fmt.Errorf("unable to send message: %w", err)
Expand Down
20 changes: 5 additions & 15 deletions authmailbox/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,25 +422,15 @@ func TestSendMessage(t *testing.T) {
expectedErrs: []string{ErrMessageTooLong.Error()},
},
{
name: "missing expiry height",
txProofs: []proof.TxProof{*txProof1},
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{[]byte("yoooo")},
expectedErrs: []string{"missing expiry block height"},
},
{
name: "expiry height too low",
name: "expiry delta not set",
txProofs: []proof.TxProof{
proofWithHeight(*txProof1, 100002),
},
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{[]byte("yoooo")},
expiryHeight: 123,
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{[]byte("yoooo")},
expectedErrs: []string{
"expiry block height 123 is before proof " +
"block height 100002",
"missing expiry block delta",
},
},
{
Expand Down
8 changes: 4 additions & 4 deletions authmailbox/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type (
// MarshalMessage converts a Message to its gRPC representation.
func MarshalMessage(msg *Message) *mboxrpc.MailboxMessage {
return &mboxrpc.MailboxMessage{
MessageId: msg.ID,
EncryptedPayload: msg.EncryptedPayload,
ArrivalTimestamp: msg.ArrivalTimestamp.Unix(),
ExpiryBlockHeight: msg.ExpiryBlockHeight,
MessageId: msg.ID,
EncryptedPayload: msg.EncryptedPayload,
ArrivalTimestamp: msg.ArrivalTimestamp.Unix(),
ExpiryBlockDelta: msg.ExpiryBlockDelta,
}
}
10 changes: 6 additions & 4 deletions authmailbox/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ type Message struct {
// the tx proof for this message.
ProofBlockHeight uint32

// ExpiryBlockHeight is a user-defined expiry block height for the
// message. This is the block height after which the message can be
// considered expired and may be deleted.
ExpiryBlockHeight uint32
// ExpiryBlockDelta is a user-defined expiry block height delta for the
// message. Once the outpoint claimed by the proof provided by this
// message has been spent, then after this number of blocks after the
// spending transaction height the message can be considered expired and
// may be deleted.
ExpiryBlockDelta uint32
}

// Timestamp returns the time when the message was received.
Expand Down
14 changes: 6 additions & 8 deletions authmailbox/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func (s *Server) SendMessage(ctx context.Context,
return nil, ErrMessageTooLong
}

if req.ExpiryBlockHeight == 0 {
return nil, fmt.Errorf("missing expiry block height")
if req.ExpiryBlockDelta == 0 {
return nil, fmt.Errorf("missing expiry block delta")
}

if req.Proof == nil {
Expand Down Expand Up @@ -272,12 +272,10 @@ func (s *Server) SendMessage(ctx context.Context,
return nil, fmt.Errorf("unsupported proof type: %T", p)
}

// Now that we know the arrival block height (either from the proof or
// from our backend), we can validate the expiry block height.
if req.ExpiryBlockHeight <= msg.ProofBlockHeight {
return nil, fmt.Errorf("expiry block height %d is before "+
"proof block height %d", req.ExpiryBlockHeight,
msg.ProofBlockHeight)
// The number of blocks to wait after the claimed outpoint has been
// spent must be non-zero, otherwise the message would never expire.
if req.ExpiryBlockDelta == 0 {
return nil, fmt.Errorf("expiry block delta is missing")
}

// We have verified everything we can, we'll allow the message to be
Expand Down
8 changes: 4 additions & 4 deletions tapdb/authmailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (m MailboxStore) StoreMessage(ctx context.Context, txProof proof.TxProof,
ReceiverKey: receiverKey,
EncryptedPayload: msg.EncryptedPayload,
ArrivalTimestamp: msg.ArrivalTimestamp.Unix(),
ExpiryBlockHeight: sqlInt32(msg.ExpiryBlockHeight),
ExpiryBlockHeight: sqlInt32(msg.ExpiryBlockDelta),
})
if err != nil {
return fmt.Errorf("error inserting message: %w", err)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (m MailboxStore) FetchMessage(ctx context.Context,
EncryptedPayload: dbMsg.EncryptedPayload,
ArrivalTimestamp: time.Unix(dbMsg.ArrivalTimestamp, 0),
ProofBlockHeight: uint32(dbMsg.BlockHeight),
ExpiryBlockHeight: extractSqlInt32[uint32](
ExpiryBlockDelta: extractSqlInt32[uint32](
dbMsg.ExpiryBlockHeight,
),
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (m MailboxStore) FetchMessageByOutPoint(ctx context.Context,
EncryptedPayload: dbMsg.EncryptedPayload,
ArrivalTimestamp: time.Unix(dbMsg.ArrivalTimestamp, 0),
ProofBlockHeight: uint32(dbMsg.BlockHeight),
ExpiryBlockHeight: extractSqlInt32[uint32](
ExpiryBlockDelta: extractSqlInt32[uint32](
dbMsg.ExpiryBlockHeight,
),
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (m MailboxStore) QueryMessages(ctx context.Context,
dbMsg.ArrivalTimestamp, 0,
),
ProofBlockHeight: uint32(dbMsg.BlockHeight),
ExpiryBlockHeight: extractSqlInt32[uint32](
ExpiryBlockDelta: extractSqlInt32[uint32](
dbMsg.ExpiryBlockHeight,
),
}
Expand Down
28 changes: 14 additions & 14 deletions tapdb/authmailbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func TestStoreAndFetchMessage(t *testing.T) {

txProof := proof.MockTxProof(t)
msg := &authmailbox.Message{
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockHeight: 100,
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockDelta: 100,
}

msgID, err := mailboxStore.StoreMessage(ctx, *txProof, msg)
Expand All @@ -52,7 +52,7 @@ func TestStoreAndFetchMessage(t *testing.T) {
require.Equal(
t, msg.ArrivalTimestamp.Unix(), dbMsg.ArrivalTimestamp.Unix(),
)
require.Equal(t, msg.ExpiryBlockHeight, dbMsg.ExpiryBlockHeight)
require.Equal(t, msg.ExpiryBlockDelta, dbMsg.ExpiryBlockDelta)

// We should also be able to fetch the message by its outpoint.
dbMsgByOutPoint, err := mailboxStore.FetchMessageByOutPoint(
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestQueryMessages(t *testing.T) {
ArrivalTimestamp: time.Now().Add(
time.Duration(i) * time.Second,
),
ExpiryBlockHeight: 100,
ExpiryBlockDelta: 100,
}

_, err := mailboxStore.StoreMessage(ctx, *txProof, msg)
Expand Down Expand Up @@ -115,10 +115,10 @@ func TestNumMessages(t *testing.T) {
for i := 0; i < numMessages; i++ {
txProof := proof.MockTxProof(t)
msg := &authmailbox.Message{
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockHeight: 100,
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockDelta: 100,
}

_, err := mailboxStore.StoreMessage(ctx, *txProof, msg)
Expand All @@ -139,10 +139,10 @@ func TestStoreProof(t *testing.T) {

txProof := proof.MockTxProof(t)
msg := &authmailbox.Message{
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockHeight: 100,
ReceiverKey: *receiverKey,
EncryptedPayload: []byte("payload"),
ArrivalTimestamp: time.Now(),
ExpiryBlockDelta: 100,
}

_, err := mailboxStore.StoreMessage(ctx, *txProof, msg)
Expand Down
Loading