Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated cmd line tool and gitignore #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
.idea
cmd/main
39 changes: 39 additions & 0 deletions cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Simple Command Line Utility

This CLI can be used in order to either generate a fresh mix-header or decode and fully process an existing one given a private key.

## Usage

To use the CLI, build it and then run the commands.

```
go build main.go
./main <commands>
```

### Commands
```
./main (new|generate|decode) <private-keys>
```

#### `new`
The `new` command creates a new keypair for use with `generate` and `decode`.

#### `generate`
The `generate` command generates a route, then uses that route to create a new sphinx onion packet, which includes all hop data along the route.

#### `decode`
The `decode` command reads an onion packet in from stdin and a private key as an argument to decode the message. See example below.

`decode` also creates a new router and starts an in-memory ReplayLog.

Example:

```
$ ./main new
privkey: 89301b800e26a4d406b3ddc7bca069ca563871ab32753a63ac72ec890004ba64
pubkey: 035c6a3ce4d94a23071e4333e6ffb4f8aae0021319936e49920a390343f7b42770
$ ./main generate 035c6a3ce4d94a23071e4333e6ffb4f8aae0021319936e49920a390343f7b42770 | ./main decode 89301b800e26a4d406b3ddc7bca069ca563871ab32753a63ac72ec890004ba64
Node 0 pubkey 035c6a3ce4d94a23071e4333e6ffb4f8aae0021319936e49920a390343f7b42770
0003e91983bc6cec837a26f97aa2dd90a90f0041fda04548f4262d9e14dec4c2ca4a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000129b2a5683d565b0e394a4ee48f41eb8dc1d739dfeef13740524d8fdbc65b17baa67bb7b2309c6d4f440840862daa146bf396d6b8b83e47c487ce660a31c25fb5d0000000000000000000000000000000000000000000000000000000000000000
```
27 changes: 25 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"os"
"strings"

sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
sphinx "github.com/lightningnetwork/lightning-onion"
)

// main implements a simple command line utility that can be used in order to
Expand All @@ -23,7 +23,24 @@ func main() {
assocData := bytes.Repeat([]byte{'B'}, 32)

if len(args) == 1 {
fmt.Printf("Usage: %s (generate|decode) <private-keys>\n", args[0])
fmt.Printf("Usage: %s (new|generate|decode) <private-keys>\n", args[0])
} else if args[1] == "new" {
// generate a new private key
privKey1, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
fmt.Printf("private key generation error: %s\n", err)
return
}

privHex := hex.EncodeToString(privKey1.Serialize())
fmt.Printf("privkey: %s\n", privHex)

// get public key from private key
pub := privKey1.PubKey().SerializeCompressed()

// covert publick key to hex and print
pubHex := hex.EncodeToString(pub)
fmt.Printf("pubkey: %s\n", pubHex)
} else if args[1] == "generate" {
var route []*btcec.PublicKey
for i, hexKey := range args[2:] {
Expand Down Expand Up @@ -82,6 +99,12 @@ func main() {
s := sphinx.NewRouter(privkey, &chaincfg.TestNet3Params,
sphinx.NewMemoryReplayLog())

// start router (create memory log)
err = s.Start()
if err != nil {
log.Fatalf("Error starting router: %v", err)
}

var packet sphinx.OnionPacket
err = packet.Decode(bytes.NewBuffer(binMsg))

Expand Down