From 11124fa3bff4ad00856891ecdc77e02e2627c153 Mon Sep 17 00:00:00 2001 From: Luca Ambrosini Date: Fri, 4 Oct 2019 02:38:52 +0200 Subject: [PATCH] Add compability to current formalism to short_channel_id-to-txid.sh * Add compatibility with the new shortchannelid syntax with x as separator * Add an error message in case the parse failed, instead of receveing an unrelated error from the bitcoin-cli --- contrib/short_channel_id-to-txid.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/contrib/short_channel_id-to-txid.sh b/contrib/short_channel_id-to-txid.sh index 1a4af3f4d..faeecff69 100755 --- a/contrib/short_channel_id-to-txid.sh +++ b/contrib/short_channel_id-to-txid.sh @@ -2,14 +2,27 @@ set -e -if [ "$#" != 1 ]; then - echo Usage: "$0" "short:channel:id" >&2 +if [ "$#" != 1 ] +then + echo Usage: "$0" "shortchannelid (e.g. 532046x1702x0 or 532046:1702:0)" >&2 echo Uses bitcoin-cli to extract the actual txid >&2 exit 1 fi -BLOCK=$(echo "$1" | cut -d: -f1) -TXNUM=$(echo "$1" | cut -d: -f2) +# Try to segment using both x and : as delimiters (compatibility with the old shortchannelid standard) +BLOCK=$(echo "$1" | cut -dx -f1) +TXNUM=$(echo "$1" | cut -dx -f2) -bitcoin-cli getblock "$(bitcoin-cli getblockhash "$BLOCK")" true | grep '^ "' | head -n "$((TXNUM + 1))" | tail -n 1 | tr -dc '0-9a-f\n' +if [ "$BLOCK" = "$1" ] && [ "$TXNUM" = "$1" ] +then + BLOCK=$(echo "$1" | cut -d: -f1) + TXNUM=$(echo "$1" | cut -d: -f2) +fi +if [ "$BLOCK" = "$1" ] && [ "$TXNUM" = "$1" ] +then + echo The provided shortchannelid is invalid. Valid examples: 532046x1702x0 or 532046:1702:0 >&2 + exit 1 +fi + +bitcoin-cli getblock "$(bitcoin-cli getblockhash "$BLOCK")" true | grep '^ "' | head -n "$((TXNUM + 1))" | tail -n 1 | tr -dc '0-9a-f\n' \ No newline at end of file