mirror of https://github.com/guggero/chantools
signmessage: add signmessage cmd
This commit adds the signmessage command which allows a user to sign a message with the nodes identity key, similiar to `lncli signmessage`.pull/121/head
parent
cf4cabbd2a
commit
3fbf8d0bd2
@ -0,0 +1,90 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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 signmessage --msg=foobar`,
|
||||||
|
RunE: cc.Execute,
|
||||||
|
}
|
||||||
|
cc.cmd.Flags().StringVar(
|
||||||
|
&cc.Msg, "msg", "", "the message to sign",
|
||||||
|
)
|
||||||
|
|
||||||
|
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 := []byte(c.Msg)
|
||||||
|
msg = append(signedMsgPrefix, msg...)
|
||||||
|
sigBytes, err := privKeyMsgSigner.SignMessageCompact(msg, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode the signature.
|
||||||
|
sig := zbase32.EncodeToString(sigBytes)
|
||||||
|
fmt.Println(sig)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -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)
|
||||||
|
```
|
Loading…
Reference in New Issue