-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the signmessage command which allows a user to sign a message with the nodes identity key, similiar to `lncli signmessage`.
- Loading branch information
Showing
6 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
chantools_lnd "github.com/lightninglabs/chantools/lnd" | ||
"github.com/lightningnetwork/lnd/keychain" | ||
"github.com/spf13/cobra" | ||
"github.com/tv42/zbase32" | ||
) | ||
|
||
var ( | ||
signedMsgPrefix = []byte("Lightning Signed Message:") | ||
) | ||
|
||
type signMessageCommand struct { | ||
Msg string | ||
SingleHash bool | ||
|
||
rootKey *rootKey | ||
cmd *cobra.Command | ||
} | ||
|
||
func newSignMessageCommand() *cobra.Command { | ||
cc := &signMessageCommand{} | ||
cc.cmd = &cobra.Command{ | ||
Use: "signmessage", | ||
Short: "Sign a message with the node's private key.", | ||
Long: `Sign msg with the resident node's private key. | ||
Returns the signature as a zbase32 string.`, | ||
Example: `chantools dumpbackup \ | ||
--multi_file ~/.lnd/data/chain/bitcoin/mainnet/channel.backup`, | ||
RunE: cc.Execute, | ||
} | ||
cc.cmd.Flags().StringVar( | ||
&cc.Msg, "msg", "", "the message to sign", | ||
) | ||
cc.cmd.Flags().BoolVar( | ||
&cc.SingleHash, "single_hash", false, "single hash the msg "+ | ||
"instead of double hash (lnd default is false)", | ||
) | ||
|
||
cc.rootKey = newRootKey(cc.cmd, "decrypting the backup") | ||
|
||
return cc.cmd | ||
} | ||
func (c *signMessageCommand) Execute(_ *cobra.Command, _ []string) error { | ||
if c.Msg == "" { | ||
return fmt.Errorf("please enter a valid msg") | ||
} | ||
|
||
extendedKey, err := c.rootKey.read() | ||
if err != nil { | ||
return fmt.Errorf("error reading root key: %w", err) | ||
} | ||
|
||
signer := &chantools_lnd.Signer{ | ||
ExtendedKey: extendedKey, | ||
ChainParams: chainParams, | ||
} | ||
|
||
// Create the key locator for the node key. | ||
keyLocator := keychain.KeyLocator{ | ||
Family: keychain.KeyFamilyNodeKey, | ||
Index: 0, | ||
} | ||
|
||
// Fetch the private key for node key. | ||
privKey, err := signer.FetchPrivKey(&keychain.KeyDescriptor{ | ||
KeyLocator: keyLocator, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create a new signer. | ||
privKeyMsgSigner := keychain.NewPrivKeyMessageSigner( | ||
privKey, keyLocator, | ||
) | ||
|
||
// Prepend the special lnd prefix. | ||
// See: https://github.com/lightningnetwork/lnd/blob/63e698ec4990e678089533561fd95cfd684b67db/rpcserver.go#L1576 . | ||
msg := append(signedMsgPrefix, []byte(c.Msg)...) | ||
sigBytes, err := privKeyMsgSigner.SignMessageCompact(msg, !c.SingleHash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Encode the signature. | ||
sig := zbase32.EncodeToString(sigBytes) | ||
fmt.Println(sig) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## chantools signmessage | ||
|
||
Signs a message with the nodes key, results in the same signature as | ||
`lncli signmessage` | ||
|
||
### Synopsis | ||
|
||
``` | ||
chantools signmessage [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
chantools signmessage --msg=foobar | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--bip39 read a classic BIP39 seed and passphrase from the terminal instead of asking for lnd seed format or providing the --rootkey flag | ||
-h, --help help for signmessage | ||
--msg string the message to sign | ||
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed | ||
--single_hash single hash the msg instead of double hash (lnd default is false) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters