-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sign lightning network algorithm
- Loading branch information
1 parent
7751893
commit 57a423c
Showing
7 changed files
with
149 additions
and
2 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,15 @@ | ||
package goutils | ||
|
||
import "fmt" | ||
|
||
func Todo(message string, args ...interface{}) error { | ||
return fmt.Errorf(message, args...) | ||
} | ||
|
||
func Errf(message string, args ...interface{}) error { | ||
return fmt.Errorf(message, args...) | ||
} | ||
|
||
func NotImplementYet() error { | ||
return Todo("not implemented yet") | ||
} |
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,56 @@ | ||
// Package ln is a package that implement a signature proposed in the | ||
// lightning network protocol, but not yet specified. | ||
package ln | ||
|
||
import ( | ||
"encoding/base32" | ||
"encoding/hex" | ||
"github.com/LNOpenMetrics/lnmetrics.utils/goutils" | ||
"github.com/LNOpenMetrics/lnmetrics.utils/hash/sha256" | ||
"github.com/btcsuite/btcd/btcec/v2/ecdsa" | ||
) | ||
|
||
// LNSigner is a struct that implement th Signer interface | ||
type LNSigner struct { | ||
encoder *base32.Encoding | ||
signedMsgPrefix string | ||
} | ||
|
||
func NewLNSigner() *LNSigner { | ||
return &LNSigner{ | ||
// Copied from https://github.com/ElementsProject/lightning/blob/master/lightningd/signmessage.c#L11 | ||
encoder: base32.NewEncoding("ybndrfg8ejkmcpqxot1uwisza345h769"), | ||
signedMsgPrefix: "Lightning Signed Message:", | ||
} | ||
} | ||
|
||
// SignMsg sign the message by following the lightning network rules, but it is not implemented | ||
// because required to import the keys to sign the message. | ||
func (self *LNSigner) SignMsg(msg *string) (*string, error) { | ||
return nil, goutils.NotImplementYet() | ||
} | ||
|
||
// VerifyMsg sign the message with the lightning network logics implemented in the most popular implementation | ||
func (self *LNSigner) VerifyMsg(key *string, signature *string, msg *string) (bool, error) { | ||
u8sing, err := self.encoder.DecodeString(*signature) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
// https://github.com/ElementsProject/lightning/blob/master/lightningd/signmessage.c#L177 | ||
if len(u8sing) != 65 { | ||
return false, goutils.Errf("zbase is too is wrong size %d, need to be exactly 65", len(u8sing)) | ||
} | ||
|
||
// The signature is over the double-sha256 hash of the message. | ||
toVerify := append([]byte(self.signedMsgPrefix), []byte(*msg)...) | ||
digest := sha256.DoubleSHA256FromByte(toVerify) | ||
|
||
// RecoverCompact both recovers the pubkey and validates the signature. | ||
pubKey, _, err := ecdsa.RecoverCompact(u8sing, digest) | ||
if err != nil { | ||
return false, nil | ||
} | ||
pubKeyHex := hex.EncodeToString(pubKey.SerializeCompressed()) | ||
return pubKeyHex == *key, goutils.NotImplementYet() | ||
} |
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,30 @@ | ||
package ln | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// Test the following command in core lightning | ||
// clightning --testnet signmessage "Testing go utils" | ||
// pub key: 03b39d1ddf13ce486de74e9e44e0538f960401a9ec75534ba9cfe4100d65426880 | ||
// zbase32 encoding: d7m5xma1tia1hw6mjkeixqoocexiyoyn1jgm53mncszdeut5j9r1k1nq7wsrh3pkk3c3xp7mkuuxmbneitu3us36cqfn9a6sdu3wa45j | ||
// signature: 57b7af128d712e53cb4a9157ba10621f504002924cbde56265ae344e3b4fc925484eed2c4e65aa565997b7ab54e6f58448ac6799db3e638a2fe3d61cf34c6b69 | ||
// message: "Testing go utils" | ||
func TestLNSignerVerifyMsgTrue(t *testing.T) { | ||
pubKey := "03b39d1ddf13ce486de74e9e44e0538f960401a9ec75534ba9cfe4100d65426880" | ||
zbase32 := "d7m5xma1tia1hw6mjkeixqoocexiyoyn1jgm53mncszdeut5j9r1k1nq7wsrh3pkk3c3xp7mkuuxmbneitu3us36cqfn9a6sdu3wa45j" | ||
msg := "Testing go utils" | ||
signer := NewLNSigner() | ||
verified, _ := signer.VerifyMsg(&pubKey, &zbase32, &msg) | ||
assert.Equal(t, true, verified, "Return level returned it is diffirent") | ||
} | ||
|
||
func TestLNSignerVerifyMsgFalse(t *testing.T) { | ||
pubKey := "03b39d1ddf13ce486de74e9e44e0538f960401a9ec75534ba9cfe4100d65426880" | ||
zbase32 := "d7m5xma1tia1hw6mjkeixqoocexiyoyn1jgm53mncszdeut5j9r1k1nq7wsrh3pkk3c3xp7mkuuxmbneitu3us36cqfn9a6sdu3wa45j" | ||
msg := "Invalid message" | ||
signer := NewLNSigner() | ||
verified, _ := signer.VerifyMsg(&pubKey, &zbase32, &msg) | ||
assert.Equal(t, false, verified, "Return level returned it is diffirent") | ||
} |
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,19 @@ | ||
// Package sign give the API to implement a custom signer | ||
// that is able to sign and verify a sequence of bytes or | ||
// a string. | ||
package sign | ||
|
||
// Signer is a common interface that give the possibility to implement | ||
// a signer and verify algorithm in pure go | ||
type Signer interface { | ||
// SignMsg sign a string and return the result or an error | ||
SignMsg(msg *string) (*string, error) | ||
|
||
// VerifyMsg a signature with optional public key specified as parameter | ||
// or use some key stored inside the struct. | ||
VerifyMsg(signature *string) (bool, error) | ||
} | ||
|
||
// Keys tagger interface to store the information about the | ||
// key (if any) that are used to sign a message. | ||
type Keys interface{} |