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:
- Client -> Server: ReceiveMessagesRequest(init = InitReceive{...})
- Server -> Client: ReceiveMessagesResponse(challenge = Challenge{...})
- Client -> Server: ReceiveMessagesRequest(auth_sig = AuthSignature{...})
- Server -> Client: [Stream of ReceiveMessagesResponse( message = MailboxMessage{...} )]
- Server -> Client: ReceiveMessagesResponse(eos = EndOfStream{})
Source: authmailboxrpc/mailbox.proto
gRPC
info
This is a bidirectional-streaming RPC
rpc ReceiveMessages (stream ReceiveMessagesRequest) returns (stream ReceiveMessagesResponse);
REST
HTTP Method | Path |
---|---|
POST | /v1/taproot-assets/mailbox/receive |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10029'
const MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
const TLS_PATH = 'TAPROOT-ASSETS_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) {
// A response was received from the server.
console.log(response);
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
call.write(request);
// Console output:
// {
// "challenge": <Challenge>,
// "auth_success": <bool>,
// "messages": <MailboxMessages>,
// "eos": <EndOfStream>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the authmailboxrpc/mailbox.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import mailbox_pb2 as authmailboxrpc, mailbox_pb2_grpc as mailboxstub
GRPC_HOST = 'localhost:10029'
MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
TLS_PATH = 'TAPROOT-ASSETS_DIR/tls.cert'
# create macaroon credentials
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)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = mailboxstub.MailboxStub(channel)
# Define a generator that returns an Iterable of ReceiveMessagesRequest objects.
def request_generator():
# Initialization code here.
while True:
# Parameters here can be set as arguments to the generator.
request = authmailboxrpc.ReceiveMessagesRequest(
init=<InitReceive>,
auth_sig=<AuthSignature>,
)
yield request
# Do things between iterations here.
request_iterable = request_generator()
for response in stub.ReceiveMessages(request_iterable):
print(response)
# {
# "challenge": <Challenge>,
# "auth_success": <bool>,
# "messages": <MailboxMessages>,
# "eos": <EndOfStream>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8089'
const MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
let requestBody = {
init: <object>, // <InitReceive>
auth_sig: <object>, // <AuthSignature>
};
let options = {
url: `https://${REST_HOST}/v1/taproot-assets/mailbox/receive`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
form: JSON.stringify(requestBody),
}
request.post(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "challenge": <object>, // <Challenge>
// "auth_success": <boolean>, // <bool>
// "messages": <object>, // <MailboxMessages>
// "eos": <object>, // <EndOfStream>
// }
// --------------------------
// Example with websockets:
// --------------------------
const WebSocket = require('ws');
const fs = require('fs');
const REST_HOST = 'localhost:8089'
const MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
let ws = new WebSocket(`wss://${REST_HOST}/v1/taproot-assets/mailbox/receive?method=POST`, {
// Work-around for self-signed certificates.
rejectUnauthorized: false,
headers: {
'Grpc-Metadata-Macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
});
let requestBody = {
init: <InitReceive>, // <InitReceive>
auth_sig: <AuthSignature>, // <AuthSignature>
};
ws.on('open', function() {
ws.send(JSON.stringify(requestBody));
});
ws.on('error', function(err) {
console.log('Error: ' + err);
});
ws.on('message', function(body) {
console.log(body);
});
// Console output:
// {
// "challenge": <object>, // <Challenge>
// "auth_success": <boolean>, // <bool>
// "messages": <object>, // <MailboxMessages>
// "eos": <object>, // <EndOfStream>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8089'
MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
TLS_PATH = 'TAPROOT-ASSETS_DIR/tls.cert'
url = f'https://{REST_HOST}/v1/taproot-assets/mailbox/receive'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
data = {
'init': <InitReceive>,
'auth_sig': <AuthSignature>,
}
r = requests.post(url, headers=headers, stream=True, data=json.dumps(data), verify=TLS_PATH)
for raw_response in r.iter_lines():
json_response = json.loads(raw_response)
print(json_response)
# {
# "challenge": <Challenge>,
# "auth_success": <bool>,
# "messages": <MailboxMessages>,
# "eos": <EndOfStream>,
# }
# There is no CLI command for this RPC
Messages
authmailboxrpc.ReceiveMessagesRequest
Source: authmailboxrpc/mailbox.proto
Field | gRPC Type | REST Type | REST Placement |
---|---|---|---|
init | InitReceive | object | body |
auth_sig | AuthSignature | object | body |
authmailboxrpc.ReceiveMessagesResponse
Source: authmailboxrpc/mailbox.proto
Field | gRPC Type | REST Type |
---|---|---|
challenge | Challenge | object |
auth_success | bool | boolean |
messages | MailboxMessages | object |
eos | EndOfStream | object |
Nested Messages
authmailboxrpc.AuthSignature
Field | gRPC Type | REST Type |
---|---|---|
signature | bytes | string |
authmailboxrpc.Challenge
Field | gRPC Type | REST Type |
---|---|---|
challenge_hash | bytes | string |
authmailboxrpc.EndOfStream
note
This response has no parameters.
authmailboxrpc.InitReceive
Field | gRPC Type | REST Type |
---|---|---|
receiver_id | bytes | string |
start_message_id_exclusive | uint64 | string |
start_block_height_inclusive | uint32 | integer |
start_timestamp_exclusive | int64 | string |
authmailboxrpc.MailboxMessage
Field | gRPC Type | REST Type |
---|---|---|
message_id | uint64 | string |
encrypted_payload | bytes | string |
arrival_timestamp | int64 | string |
expiry_block_height | uint32 | integer |
authmailboxrpc.MailboxMessages
Field | gRPC Type | REST Type |
---|---|---|
messages | MailboxMessage[] | array |