Skip to content
Merged
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
43 changes: 41 additions & 2 deletions testing/testclient/testclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ package main
import (
"flag"
"io/ioutil"
"math/big"

"github.com/celer-network/agent-pay/celersdk"
"github.com/celer-network/agent-pay/common"
"github.com/celer-network/agent-pay/ctype"
"github.com/celer-network/agent-pay/webapi"
"github.com/celer-network/goutils/eth"
"github.com/celer-network/goutils/log"
)

Expand All @@ -29,13 +34,47 @@ func main() {
log.Fatal(err)
}
log.Infoln("start testclient on port", *grpcPort, "using ks", *ksPath)
if *extSigner {
addr, priv, err := eth.GetAddrPrivKeyFromKeystore(string(ksBytes), "")
if err != nil {
log.Fatal(err)
}
p := common.Bytes2Profile(cfg)
signer, err := eth.NewSigner(priv, big.NewInt(p.ChainId))
if err != nil {
log.Fatal(err)
}
webapi.NewInternalApiServerWithExternalSigner(
-1,
*grpcPort,
"http://localhost:*",
ctype.Addr2Hex(addr),
*dataPath,
string(cfg[:]),
&testExternalSigner{Signer: signer},
nil).Start()
return
}
webapi.NewInternalApiServer(
-1,
*grpcPort,
"http://localhost:*",
string(ksBytes[:]),
"",
*dataPath,
string(cfg[:]),
*extSigner).Start()
string(cfg[:])).Start()
}

type testExternalSigner struct {
eth.Signer
}

func (es *testExternalSigner) OnSignMessage(reqid int, msg []byte) {
res, _ := es.SignEthMessage(msg)
celersdk.PublishSignedResult(reqid, res)
}

func (es *testExternalSigner) OnSignTransaction(reqid int, rawtx []byte) {
res, _ := es.SignEthTransaction(rawtx)
celersdk.PublishSignedResult(reqid, res)
}
67 changes: 28 additions & 39 deletions webapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"net"
"net/http"
"strconv"
Expand All @@ -17,12 +16,10 @@ import (

"github.com/celer-network/agent-pay/celersdk"
"github.com/celer-network/agent-pay/celersdkintf"
"github.com/celer-network/agent-pay/common"
"github.com/celer-network/agent-pay/ctype"
"github.com/celer-network/agent-pay/entity"
msgrpc "github.com/celer-network/agent-pay/rpc"
"github.com/celer-network/agent-pay/webapi/rpc"
"github.com/celer-network/goutils/eth"
"github.com/celer-network/goutils/log"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/rs/cors"
Expand All @@ -44,32 +41,41 @@ type ApiServer struct {
appSessionMapLock sync.Mutex
}

// implement celersdk.ExternalSignerCallback interface
// also embed eth.Signer so celersdk.ExternalSignerManager can tell the difference and
// avoid double hash
type extSigner struct {
eth.Signer
}

func (es *extSigner) OnSignMessage(reqid int, msg []byte) {
res, _ := es.SignEthMessage(msg)
celersdk.PublishSignedResult(reqid, res)
}

func (es *extSigner) OnSignTransaction(reqid int, rawtx []byte) {
res, _ := es.SignEthTransaction(rawtx)
celersdk.PublishSignedResult(reqid, res)
}

func NewApiServer(
webPort int,
grpcPort int,
allowedOrigins string,
keystore string,
password string,
dataPath string,
config string) *ApiServer {
return newApiServerWithClientInit(webPort, grpcPort, allowedOrigins, func(callbackImpl *callbackImpl) {
go celersdk.InitClient(
&celersdk.Account{Keystore: keystore, Password: password},
config,
dataPath,
callbackImpl)
})
}

func NewApiServerWithExternalSigner(
webPort int,
grpcPort int,
allowedOrigins string,
addr string,
dataPath string,
config string,
useExtSigner bool) *ApiServer {
signcb celersdk.ExternalSignerCallback) *ApiServer {
return newApiServerWithClientInit(webPort, grpcPort, allowedOrigins, func(callbackImpl *callbackImpl) {
go celersdk.InitClientWithSigner(addr, config, dataPath, callbackImpl, signcb)
})
}

func newApiServerWithClientInit(
webPort int,
grpcPort int,
allowedOrigins string,
initClient func(*callbackImpl)) *ApiServer {
callbackImpl := NewCallbackImpl()
s := &ApiServer{
webPort: webPort,
Expand All @@ -78,24 +84,7 @@ func NewApiServer(
callbackImpl: callbackImpl,
appSessionMap: make(map[string]*celersdk.AppSession),
}
if !useExtSigner {
go celersdk.InitClient(
&celersdk.Account{Keystore: keystore, Password: password},
config,
dataPath,
callbackImpl)
} else { // exercise external signer code path
addr, priv, err := eth.GetAddrPrivKeyFromKeystore(keystore, password)
if err != nil {
log.Fatal(err)
}
p := common.Bytes2Profile([]byte(config))
signer, err := eth.NewSigner(priv, big.NewInt(p.ChainId))
if err != nil {
log.Fatal(err)
}
go celersdk.InitClientWithSigner(ctype.Addr2Hex(addr), config, dataPath, callbackImpl, &extSigner{signer})
}
initClient(callbackImpl)

select {
case client := <-callbackImpl.clientReady:
Expand Down
2 changes: 1 addition & 1 deletion webapi/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ func main() {
string(ksBytes[:]),
ksPasswordStr,
*dataPath,
string(cfg[:]), false).Start()
string(cfg[:])).Start()
}
22 changes: 19 additions & 3 deletions webapi/internal_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type InternalApiServer struct {
*ApiServer
register func(*grpc.Server)
}

func NewInternalApiServer(
Expand All @@ -23,14 +24,29 @@ func NewInternalApiServer(
keystore string,
password string,
dataPath string,
config string) *InternalApiServer {
apiServer := NewApiServer(webPort, grpcPort, allowedOrigins, keystore, password, dataPath, config)
return &InternalApiServer{ApiServer: apiServer}
}

func NewInternalApiServerWithExternalSigner(
webPort int,
grpcPort int,
allowedOrigins string,
addr string,
dataPath string,
config string,
extSigner bool) *InternalApiServer {
apiServer := NewApiServer(webPort, grpcPort, allowedOrigins, keystore, password, dataPath, config, extSigner)
return &InternalApiServer{apiServer}
cb celersdk.ExternalSignerCallback,
register func(*grpc.Server)) *InternalApiServer {
apiServer := NewApiServerWithExternalSigner(webPort, grpcPort, allowedOrigins, addr, dataPath, config, cb)
return &InternalApiServer{ApiServer: apiServer, register: register}
}

func (s *InternalApiServer) Start() {
gs := grpc.NewServer()
if s.register != nil {
s.register(gs)
}
rpc.RegisterWebApiServer(gs, s.ApiServer)
rpc.RegisterInternalWebApiServer(gs, s)
s.ApiServer.serve(gs)
Expand Down
Loading