# ReceiveMessages

Initiates a bidirectional stream to receive messages for a specific receiver. This stream implements the challenge-response handshake required for receiver authentication before messages are delivered.

Expected flow:

1. Client -> Server: ReceiveMessagesRequest(init = InitReceive{...})
2. Server -> Client: ReceiveMessagesResponse(challenge = Challenge{...})
3. Client -> Server: ReceiveMessagesRequest(auth_sig = AuthSignature{...})
4. Server -> Client: [Stream of ReceiveMessagesResponse(
   message = MailboxMessage{...}
)]
5. Server -> Client: ReceiveMessagesResponse(eos = EndOfStream{})

Source: [authmailboxrpc/mailbox.proto](https://github.com/lightninglabs/taproot-assets/blob/0bf1b0f1d89f0cf5b7560389a089728ded156952/taprpc/authmailboxrpc/mailbox.proto#L36)

### gRPC

info

This is a bidirectional-streaming RPC

```text
rpc ReceiveMessages (stream ReceiveMessagesRequest) returns (stream ReceiveMessagesResponse);
```

### REST

| HTTP Method | Path |
| --- | --- |
| POST | `/v1/taproot-assets/mailbox/receive` |

## Code Samples

### gRPC

```javascript
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

const GRPC_HOST = 'localhost:10029'
const MACAROON_PATH = 'TAPD_DIR/data/regtest/admin.macaroon'
const TLS_PATH = 'TAPD_DIR/tls.cert'

const loaderOptions = {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
};
const packageDefinition = protoLoader.loadSync('authmailboxrpc/mailbox.proto', loaderOptions);
const authmailboxrpc = grpc.loadPackageDefinition(packageDefinition).authmailboxrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
  let metadata = new grpc.Metadata();
  metadata.add('macaroon', macaroon);
  callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new authmailboxrpc.Mailbox(GRPC_HOST, creds);
let request = {
  init: <InitReceive>,
  auth_sig: <AuthSignature>,
};
let call = client.receiveMessages({});
call.on('data', function(response) {
  console.log(response);
});
call.on('status', function(status) {
});
call.on('end', function() {
});
call.write(request);
```

### Python

```python
import codecs, grpc, os
import mailbox_pb2 as authmailboxrpc, mailbox_pb2_grpc as mailboxstub

GRPC_HOST = 'localhost:10029'
MACAROON_PATH = 'TAPD_DIR/data/regtest/admin.macaroon'
TLS_PATH = 'TAPD_DIR/tls.cert'

macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
  callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = mailboxstub.MailboxStub(channel)

def request_generator():
    while True:
        request = authmailboxrpc.ReceiveMessagesRequest(
            init=<InitReceive>,
            auth_sig=<AuthSignature>,
        )
        yield request
request_iterable = request_generator()
for response in stub.ReceiveMessages(request_iterable):
  print(response)
```

### curl

```bash
# Client streaming or bidirectional streaming RPCs are not easily supported by grpcurl. Please use a programming language client for this RPC.
```

## Messages

### authmailboxrpc.ReceiveMessagesRequest

| Field | gRPC Type | REST Type | REST Placement |
| --- | --- | --- | --- |
| `init`  <br>The initial parameters sent by the client to start receiving messages. | [`InitReceive`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcreceivemessagesrequest/index.html) | `object` | `body` |
| `auth_sig`  <br>The client's signature in response to the server's challenge. | [`AuthSignature`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcauthsignature/index.html) | `object` | `body` |

### authmailboxrpc.ReceiveMessagesResponse

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `challenge`  <br>The challenge sent by the server to the client, which the client must sign to prove ownership of the receiver's public key. | [`Challenge`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcchallenge/index.html) | `object` |
| `auth_success`  <br>A successful authentication response, indicating the client has successfully signed the challenge and is now authenticated to receive messages. | `bool` | `boolean` |
| `messages`  <br>A list of mailbox messages sent to the client. | [`MailboxMessages`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcmailboxmessages/index.html) | `object` |
| `eos`  <br>An EndOfStream message indicating that the server is shutting down. | [`EndOfStream`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcendofstream/index.html) | `object` |

### Nested Messages

### authmailboxrpc.AuthSignature

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `signature`  <br>The client's Schnorr signature over the challenge hash provided by the server. | `bytes` | `string` |

### authmailboxrpc.Challenge

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `challenge_hash`  <br>The challenge hash that the client must sign to prove ownership of the receiver's public key. | `bytes` | `string` |

### authmailboxrpc.EndOfStream

note

This response has no parameters.

### authmailboxrpc.InitReceive

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `receiver_id`  <br>The public key identifier of the receiver of the messages. | `bytes` | `string` |
| `start_message_id_exclusive`  <br>The exclusive start message ID. | `uint64` | `string` |
| `start_block_height_inclusive`  <br>The inclusive start block height. | `uint32` | `integer` |
| `start_timestamp_exclusive`  <br>The exclusive start timestamp. | `int64` | `string` |

### authmailboxrpc.MailboxMessage

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `message_id`  <br>The unique ID assigned to the message by the server. | `uint64` | `string` |
| `encrypted_payload`  <br>The ECIES encrypted message payload, intended for the receiver. | `bytes` | `string` |
| `arrival_timestamp`  <br>Timestamp when the message arrived at the server. | `int64` | `string` |

### authmailboxrpc.MailboxMessages

| Field | gRPC Type | REST Type |
| --- | --- | --- |
| `messages`  <br>The list of mailbox messages. | [`MailboxMessage[]`](/content/api-docs/api/taproot-assets/mailbox/receive-messages/#authmailboxrpcmailboxmessage/index.html) | `array` |
