taurushq-io / multi-party-sig

Implementation of protocols for threshold signatures
Apache License 2.0
312 stars 120 forks source link

Minimum example for creating keys & signing #73

Closed Jefferson111 closed 2 years ago

Jefferson111 commented 2 years ago

It would be great to have a minimum working example on how to use this library. I tried following the example in README and it wasn't enough.

func main() {
    sessionID := []byte("dddd")
    ids := party.IDSlice{"a", "b", "c", "d", "e", "f"}
    threshold := 4
    pl := pool.NewPool(0)
    defer pl.TearDown()

    for _, id := range ids {
        handler, err := protocol.NewMultiHandler(cmp.Keygen(curve.Secp256k1{}, id, ids, threshold, pl), sessionID)
        if err != nil {
            // the handler was not able to start the protocol, most likely due to incorrect configuration.
            fmt.Println("error")
        }
        fmt.Println(handler.Result())
    }

The output of the handler just gives <nil> protocol: not finished My instinct tells me that I need to do something with the handler to make it work. So I tried adding this to handle my handlers based on the README

func runProtocol(handler *protocol.MultiHandler) {
    // Message handling loop
    for {
      select {

      // Message to be sent to other participants
      case msg, ok := <-handler.Listen():
        // a closed channel indicates that the protocol has finished executing
        if !ok {
          return
        }
        if handler.CanAccept(msg) {
            handler.Accept(msg)
        }
    }
  }
}

And it did not work.

So I gave up and open an issue on Github and hope that developer of this software is kind enough to provide an example on how to use their software

0xYao commented 2 years ago

This is a minimal keygen example adapted from example.go

package main

import (
    "fmt"
    "sync"

    "github.com/taurusgroup/multi-party-sig/internal/test"
    "github.com/taurusgroup/multi-party-sig/pkg/math/curve"
    "github.com/taurusgroup/multi-party-sig/pkg/party"
    "github.com/taurusgroup/multi-party-sig/pkg/pool"
    "github.com/taurusgroup/multi-party-sig/pkg/protocol"
    "github.com/taurusgroup/multi-party-sig/protocols/cmp"
)

func CMPKeygen(id party.ID, ids party.IDSlice, threshold int, n *test.Network, pl *pool.Pool) (*cmp.Config, error) {
    h, err := protocol.NewMultiHandler(cmp.Keygen(curve.Secp256k1{}, id, ids, threshold, pl), nil)
    if err != nil {
        return nil, err
    }
    test.HandlerLoop(id, h, n)
    r, err := h.Result()
    if err != nil {
        return nil, err
    }

    fmt.Println("Successfully generated the keygen config!")
    fmt.Println(r)

    return r.(*cmp.Config), nil
}

func main() {
    ids := party.IDSlice{"a", "b", "c", "d", "e", "f"}
    threshold := 4

    net := test.NewNetwork(ids)

    var wg sync.WaitGroup

    for _, id := range ids {
        wg.Add(1)

        go func(id party.ID) {
            pl := pool.NewPool(0)
            defer pl.TearDown()
            defer wg.Done()
            CMPKeygen(id, ids, threshold, net, pl)
        }(id)
    }

    wg.Wait()
}
Jefferson111 commented 2 years ago

I got this main.go:7:2: use of internal package github.com/taurusgroup/multi-party-sig/internal/test not allowed When I exposed it, I realized its running on stubs and not producing key shares

0xYao commented 2 years ago

When I exposed it, I realized its running on stubs and not producing key shares

You are not getting anything from the console output?

It should say something like this

image
Jefferson111 commented 2 years ago

Turns out it was the "github.com/taurusgroup/multi-party-sig/internal/test" and its dependencies causing me trouble. All I had to do was to git clone instead of running it stand alone. Thank you so much for the example, will look into test implementation and figure things out from there on.