Skip to content

Commit c372808

Browse files
wip: funding transaction in rgb
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 6c81e2e commit c372808

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

lightningd/opening_control.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ static void openchannel_hook_serialize(struct openchannel_hook_payload *payload,
676676

677677
/* openingd dies? Remove openingd ptr from payload */
678678
static void openchannel_payload_remove_openingd(struct subd *openingd,
679-
struct openchannel_hook_payload *payload)
679+
struct openchannel_hook_payload *payload)
680680
{
681681
assert(payload->openingd == openingd);
682682
payload->openingd = NULL;
@@ -860,6 +860,74 @@ static void opening_got_offer(struct subd *openingd,
860860
plugin_hook_call_openchannel(openingd->ld, NULL, payload);
861861
}
862862

863+
struct onfunding_channel_tx_hook_payload {
864+
struct subd *openingd;
865+
struct bitcoin_tx *tx;
866+
};
867+
868+
static void onfunding_channel_tx_hook_final(struct onfunding_channel_tx_hook_payload *payload STEALS)
869+
{
870+
struct subd *openingd = payload->openingd;
871+
// FIXME(bitfinix): manage the errors!
872+
subd_send_msg(openingd,
873+
take(towire_openingd_on_funding_tx_reply(NULL, payload->tx)));
874+
}
875+
876+
static void onfunding_channel_tx_hook_serialize(struct onfunding_channel_tx_hook_payload *payload,
877+
struct json_stream *stream,
878+
struct plugin *plugin)
879+
{
880+
json_object_start(stream, "onfunding_channel_tx");
881+
json_add_tx(stream, "tx", payload->tx);
882+
json_object_end(stream);
883+
}
884+
885+
static bool onfunding_channel_tx_hook_deserialize(struct onfunding_channel_tx_hook_payload *payload,
886+
const char *buffer,
887+
const jsmntok_t *toks)
888+
{
889+
const jsmntok_t *result_tok, *error_tok, *tx_tok;
890+
891+
if ((error_tok = json_get_member(buffer, toks, "error")) != NULL)
892+
/*FIXME(bitfinix): please return an error to openingd*/
893+
return false;
894+
895+
if ((result_tok = json_get_member(buffer, toks, "result")) == NULL)
896+
fatal("Plugin returned an invalid response to the"
897+
" onfunding_channel_tx hook: %.*s",
898+
toks[0].end - toks[0].start, buffer + toks[0].start);
899+
900+
if ((tx_tok = json_get_member(buffer, result_tok, "tx")) == NULL)
901+
fatal("Plugin returned an invalid response to the"
902+
" onfunding_channel_tx hook: %.*s",
903+
toks[0].end - toks[0].start, buffer + toks[0].start);
904+
905+
if (!json_to_tx(buffer, tx_tok, payload->tx))
906+
return false;
907+
return true;
908+
}
909+
910+
REGISTER_PLUGIN_HOOK(onfunding_channel_tx,
911+
onfunding_channel_tx_hook_deserialize,
912+
onfunding_channel_tx_hook_final,
913+
onfunding_channel_tx_hook_serialize,
914+
struct onfunding_channel_tx_hook_payload *);
915+
916+
917+
static char *opening_on_funding_tx(struct subd *openingd, const u8 *msg)
918+
{
919+
struct onfunding_channel_tx_hook_payload *payload;
920+
payload = tal(openingd, struct onfunding_channel_tx_hook_payload);
921+
payload->openingd = openingd;
922+
923+
if (!fromwire_openingd_on_funding_tx(msg, msg, &payload->tx))
924+
return tal_fmt(tmpctx, "Unexpected encoding of openingd_on_funding_tx msg: %s",
925+
tal_hex(tmpctx, msg));
926+
assert(plugin_hook_call_onfunding_channel_tx(openingd->ld, NULL, payload));
927+
// FIXME(bitfinix): return an error
928+
return NULL;
929+
}
930+
863931
static unsigned int openingd_msg(struct subd *openingd,
864932
const u8 *msg, const int *fds)
865933
{
@@ -900,13 +968,20 @@ static unsigned int openingd_msg(struct subd *openingd,
900968
case WIRE_OPENINGD_GOT_OFFER:
901969
opening_got_offer(openingd, msg, uc);
902970
return 0;
903-
971+
case WIRE_OPENINGD_ON_FUNDING_TX:
972+
const char *err;
973+
if ((err = opening_on_funding_tx(openingd, msg)) != NULL) {
974+
log_debug(openingd->log, "Unexpected error while handling on funding tx: %s", err);
975+
return 1;
976+
}
977+
return 0;
904978
/* We send these! */
905979
case WIRE_OPENINGD_INIT:
906980
case WIRE_OPENINGD_FUNDER_START:
907981
case WIRE_OPENINGD_FUNDER_COMPLETE:
908982
case WIRE_OPENINGD_FUNDER_CANCEL:
909983
case WIRE_OPENINGD_GOT_OFFER_REPLY:
984+
case WIRE_OPENINGD_ON_FUNDING_TX_REPLY:
910985
case WIRE_OPENINGD_DEV_MEMLEAK:
911986
/* Replies never get here */
912987
case WIRE_OPENINGD_DEV_MEMLEAK_REPLY:

openingd/openingd.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,12 @@ static bool funder_finalize_channel_setup(struct state *state,
703703
else
704704
*pbase = NULL;
705705

706-
// TODO(bitfinix): Send the commitment transaction to master and generate an hook
706+
msg = towire_openingd_on_funding_tx(tmpctx, *tx);
707+
wire_sync_write(REQ_FD, msg);
708+
msg = wire_sync_read(tmpctx, REQ_FD);
709+
if (!fromwire_openingd_on_funding_tx_reply(msg, msg, tx))
710+
status_failed(STATUS_FAIL_MASTER_IO, "Bad onfunding_tx %s",
711+
tal_hex(tmpctx, msg));
707712

708713
/* We ask the HSM to sign their commitment transaction for us: it knows
709714
* our funding key, it just needs the remote funding key to create the
@@ -817,6 +822,7 @@ static bool funder_finalize_channel_setup(struct state *state,
817822
goto fail;
818823
}
819824

825+
// TODO(bitfixnix): RGB for initial tx channel?
820826
validate_initial_commitment_signature(HSM_FD, *tx, sig);
821827

822828
if (!check_tx_sig(*tx, 0, NULL, wscript, &state->their_funding_pubkey, sig)) {
@@ -1503,6 +1509,8 @@ static u8 *handle_master_in(struct state *state)
15031509
case WIRE_OPENINGD_FAILED:
15041510
case WIRE_OPENINGD_GOT_OFFER:
15051511
case WIRE_OPENINGD_GOT_OFFER_REPLY:
1512+
case WIRE_OPENINGD_ON_FUNDING_TX:
1513+
case WIRE_OPENINGD_ON_FUNDING_TX_REPLY:
15061514
break;
15071515
}
15081516

openingd/openingd_wire.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ msgdata,openingd_fundee,remote_shutdown_len,u16,
140140
msgdata,openingd_fundee,remote_shutdown_scriptpubkey,u8,remote_shutdown_len
141141
msgdata,openingd_fundee,channel_type,channel_type,
142142

143+
# Communicate the Funding Tx to master
144+
msgtype,openingd_on_funding_tx,6045
145+
msgdata,openingd_on_funding_tx,tx,bitcoin_tx,
146+
147+
# Get a reply from master -> openingd
148+
msgtype,openingd_on_funding_tx_reply,6145
149+
msgdata,openingd_on_funding_tx_reply,tx,bitcoin_tx,
150+
143151
# master -> openingd: do you have a memleak?
144152
msgtype,openingd_dev_memleak,6033
145153

0 commit comments

Comments
 (0)