Skip to content

Commit

Permalink
Merge pull request #14 from persistenceOne/avkr003/asyncTx
Browse files Browse the repository at this point in the history
Avkr003/async tx
  • Loading branch information
Deepanshu Tripathi authored Jul 10, 2021
2 parents fe2eff6 + 65fcb02 commit de94064
Show file tree
Hide file tree
Showing 79 changed files with 2,868 additions and 1,121 deletions.
22 changes: 22 additions & 0 deletions application/casp/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package casp

import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
caspQueries "github.com/persistenceOne/persistenceBridge/application/rest/casp"
)

func GetEthAddress() (common.Address, error) {
uncompressedPublicKeys, err := caspQueries.GetUncompressedEthPublicKeys()
if err != nil {
return common.Address{}, err
}
if len(uncompressedPublicKeys.PublicKeys) == 0 {
return common.Address{}, fmt.Errorf("no public keys got from casp")
}
publicKey := GetEthPubKey(uncompressedPublicKeys.PublicKeys[0])

fromAddress := crypto.PubkeyToAddress(publicKey)
return fromAddress, nil
}
50 changes: 50 additions & 0 deletions application/casp/publicKey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package casp

import (
"crypto/ecdsa"
"encoding/hex"
"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/ethereum/go-ethereum/crypto"
"log"
"math/big"
)

// Should include prefix "04"
func GetTMPubKey(caspPubKey string) cryptotypes.PubKey {
x, y := getXY(caspPubKey)

pubKey := ecdsa.PublicKey{
Curve: btcec.S256(),
X: &x,
Y: &y,
}
pubkeyObject := (*btcec.PublicKey)(&pubKey)
pk := pubkeyObject.SerializeCompressed()
return &secp256k1.PubKey{Key: pk}
}

// Should include prefix "04"
func GetEthPubKey(caspPubKey string) ecdsa.PublicKey {
x, y := getXY(caspPubKey)
publicKey := ecdsa.PublicKey{
Curve: crypto.S256(),
X: &x,
Y: &y,
}
return publicKey
}

// Should include prefix "04"
func getXY(caspPubKey string) (big.Int, big.Int) {
pubKeyBytes, err := hex.DecodeString(string([]rune(caspPubKey)[2:])) // uncompressed pubkey
if err != nil {
log.Fatalln(err)
}
var x big.Int
x.SetBytes(pubKeyBytes[0:32])
var y big.Int
y.SetBytes(pubKeyBytes[32:])
return x, y
}
94 changes: 94 additions & 0 deletions application/casp/sign_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package casp

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"testing"
)

const API_TOKEN = "Bearer cHVuZWV0TmV3QXBpa2V5MTI6OWM1NDBhMzAtNTQ5NC00ZDdhLTljODktODA3MDZiNWNhYzQ1"

type request struct {
DataToSign []string `json:"dataToSign"`
Description string `json:"description"`
ProviderData string `json:"providerData"`
Details string `json:"details"`
PublicKeys []string `json:"publicKeys"`
AllowConcurrentKeyUsage bool `json:"allowConcurrentKeyUsage"`
}

func TestSignTx(t *testing.T) {
//Encode the data
postBody, _ := json.Marshal(request{
DataToSign: []string{"55C53F5D490297900CEFA825D0C8E8E9532EE8A118ABE7D8570762CD38BE9818"},
Description: "60",
ProviderData: "",
Details: "",
PublicKeys: []string{"3056301006072A8648CE3D020106052B8104000A03420004B40777F842A9F8BB7ECB94785926D725EB1F96611DC2B2C424EBC8BD1A9B7651302DC7A55301D560D599B3F72D630353325FAED84514C4ECD58330B965A1946A"},
AllowConcurrentKeyUsage: true,
})
responseBody := bytes.NewBuffer(postBody)
//Leverage Go's HTTP Post function to make request
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}}
request, err := http.NewRequest("POST", "https://65.2.149.241:443/casp/api/v1.0/mng/vaults/509fd89a-762a-40ec-bd4b-0745b06e2d3d/sign", responseBody)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
request.Header.Set("authorization", API_TOKEN)
request.Header.Set("Content-Type", "application/json")
fmt.Println(request.Header)
//Read the response body
resp, err := client.Do(request)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
log.Printf(sb)
}

func TestGet(t *testing.T) {

client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}}
//request, err := http.NewRequest("GET", "https://65.2.149.241:443/casp/api/v1.0/mng/auth/users", nil)
//request, err := http.NewRequest("GET", "https://65.2.149.241:443/casp/api/v1.0/mng/vaults/509fd89a-762a-40ec-bd4b-0745b06e2d3d/coins/118/accounts/0/chains/all/addresses?encoding=uncompressed", nil)
request, err := http.NewRequest("GET", "https://65.2.149.241:443/casp/api/v1.0/mng/operations/sign/5848021b-8140-4e3e-96cc-922b0534a5f3", nil)
//request, err := http.NewRequest("GET", "https://65.2.149.241:443/casp/api/v1.0/mng/accounts/bd4c618e-8046-4fef-bdaa-9716ade77553/participants", nil)

if err != nil {
log.Fatalf("An Error Occured %v", err)
}

request.Header.Set("authorization", API_TOKEN)
resp, err := client.Do(request)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
log.Printf(sb)
}
48 changes: 48 additions & 0 deletions application/casp/signing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package casp

import (
"fmt"
"github.com/persistenceOne/persistenceBridge/application/configuration"
"github.com/persistenceOne/persistenceBridge/application/constants"
caspQueries "github.com/persistenceOne/persistenceBridge/application/rest/casp"
caspResponses "github.com/persistenceOne/persistenceBridge/application/rest/responses/casp"
"log"
"time"
)

// GetCASPSigningOperationID description should be small
func GetCASPSigningOperationID(dataToSign []string, publicKeys []string, description string) (string, error) {
for {
signDataResponse, busy, err := caspQueries.SignData(dataToSign, publicKeys, description)
if busy {
time.Sleep(configuration.GetAppConfig().CASP.SignatureWaitTime)
}
if err != nil {
return "", err
}
return signDataResponse.OperationID, nil
}
}

func GetCASPSignature(operationID string) (caspResponses.SignOperationResponse, error) {
if operationID == "" {
return caspResponses.SignOperationResponse{}, fmt.Errorf("empty operationID")
}
for {
signOperationResponse, err := caspQueries.GetSignOperation(operationID)
if err != nil {
if err.Error() == constants.OPERATION_ID_NOT_FOUND {
return caspResponses.SignOperationResponse{}, fmt.Errorf("operation id not found")
}
log.Printf("Error while getting sign operation %v\n", err)
time.Sleep(configuration.GetAppConfig().CASP.SignatureWaitTime)
continue
}
if signOperationResponse.Status == "PENDING" {
log.Printf("CASP signing operation pending for %s\n", operationID)
time.Sleep(configuration.GetAppConfig().CASP.SignatureWaitTime)
continue
}
return signOperationResponse, nil
}
}
79 changes: 79 additions & 0 deletions application/commands/addValidator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package commands

import (
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
constants2 "github.com/persistenceOne/persistenceBridge/application/constants"
"github.com/persistenceOne/persistenceBridge/application/db"
"github.com/persistenceOne/persistenceBridge/application/rpc"
"github.com/spf13/cobra"
"log"
)

func AddCommand(initClientCtx client.Context) *cobra.Command {
addCommand := &cobra.Command{
Use: "add [ValoperAddress] [name]",
Short: "Add validator address to signing group",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
validatorAddress, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

validatorName := args[1]

homePath, err := cmd.Flags().GetString(constants2.FlagPBridgeHome)
if err != nil {
log.Fatalln(err)
}
rpcEndpoint, err := cmd.Flags().GetString(constants2.FlagRPCEndpoint)
if err != nil {
log.Fatalln(err)
}
var validators []db.Validator
database, err := db.OpenDB(homePath + "/db")
if err != nil {
log.Printf("Db is already open: %v", err)
log.Printf("sending rpc")
var err2 error
validators, err2 = rpc.AddValidator(db.Validator{
Address: validatorAddress,
Name: validatorName,
}, rpcEndpoint)
if err2 != nil {
return err2
}
} else {
defer database.Close()

err2 := db.SetValidator(db.Validator{
Address: validatorAddress,
Name: validatorName,
})
if err2 != nil {
return err2
}

validators, err2 = db.GetValidators()
if err2 != nil {
return err2
}
}
if len(validators) == 0 {
log.Println("No validators in db, panic.")
} else {
log.Printf("Total validators %d:\n", len(validators))
for i, validator := range validators {
log.Printf("%d. %s - %s\n", i+1, validator.Name, validator.Address.String())
}
}

return nil
},
}

addCommand.Flags().String(constants2.FlagRPCEndpoint, constants2.DefaultRPCEndpoint, "rpc endpoint for bridge relayer")
addCommand.Flags().String(constants2.FlagPBridgeHome, constants2.DefaultPBridgeHome, "home for pBridge")
return addCommand
}
Loading

0 comments on commit de94064

Please sign in to comment.