-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from sputn1ck/signmessage
signmessage: add signmessage cmd
- Loading branch information
Showing
6 changed files
with
122 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,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 | ||
} |
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