Skip to content

596 multisig skeleton #597

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

Merged
merged 18 commits into from
Feb 24, 2020
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
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/zoobc/zoobc-core/cmd/parser"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zoobc/zoobc-core/cmd/account"
Expand Down Expand Up @@ -48,6 +50,10 @@ func main() {
Use: "generate",
Short: "generate command is a parent command for generating stuffs",
}
parserCmd = &cobra.Command{
Use: "parser",
Short: "parse data to understandable struct",
}
)

sqliteDbInstance = database.NewSqliteDB()
Expand All @@ -67,9 +73,11 @@ func main() {
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(genesisblock.Commands())
rootCmd.AddCommand(rollback.Commands(sqliteDB))
rootCmd.AddCommand(parserCmd)
generateCmd.AddCommand(account.Commands())
generateCmd.AddCommand(transaction.Commands(sqliteDB))
generateCmd.AddCommand(block.Commands())
parserCmd.AddCommand(parser.Commands())
_ = rootCmd.Execute()

}
58 changes: 58 additions & 0 deletions cmd/parser/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package parser

import (
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/zoobc/zoobc-core/common/transaction"
)

var (
/*
Transaction Parser Command
*/
txParserCmd = &cobra.Command{
Use: "tx",
Short: "parse transaction from its hex representation",
Long: "transaction parser to check the content of your transaction hex",
}
)

func init() {
txParserCmd.Flags().StringVar(&parserTxHex, "transaction-hex", "", "hex string of the transaction bytes")
txParserCmd.Flags().StringVar(&parserTxBytes, "transaction-bytes", "", "transaction bytes separated by `, `. eg:"+
"--transaction-bytes='1, 222, 54, 12, 32'")
}

func Commands() *cobra.Command {
txParserCmd.Run = ParseTransaction
return txParserCmd
}

func ParseTransaction(*cobra.Command, []string) {
var txBytes []byte
if parserTxHex != "" {
txBytes, _ = hex.DecodeString(parserTxHex)
} else {
txByteCharSlice := strings.Split(parserTxBytes, ", ")
for _, v := range txByteCharSlice {
byteValue, err := strconv.Atoi(v)
if err != nil {
panic("failed to parse transaction bytes")
}
txBytes = append(txBytes, byte(byteValue))
}
}
tx, err := (&transaction.Util{}).ParseTransactionBytes(txBytes, false)
if err != nil {
panic("error parsing tx" + err.Error())
}
tx.TransactionBody, err = (&transaction.MultiSignatureTransaction{}).ParseBodyBytes(tx.TransactionBodyBytes)
if err != nil {
panic("error parsing tx body" + err.Error())
}
fmt.Printf("transaction:\n%v\n", tx)
}
7 changes: 7 additions & 0 deletions cmd/parser/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package parser

var (
// txParser
parserTxHex string
parserTxBytes string
)
34 changes: 34 additions & 0 deletions cmd/transaction/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transaction

import (
"database/sql"
"fmt"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,6 +55,12 @@ var (
Short: "transaction sub command used to generate 'escrow approval' transaction",
Long: "transaction sub command used to generate 'escrow approval' transaction. required transaction id and approval = true:false",
}
multiSigCmd = &cobra.Command{
Use: "multi-signature",
Short: "transaction sub command used to generate 'multi signature' transaction",
Long: "transaction sub command used to generate 'multi signature' transaction that require multiple account to submit their signature " +
"before it is valid to be executed",
}
)

func init() {
Expand Down Expand Up @@ -124,6 +131,18 @@ func init() {
*/
escrowApprovalCmd.Flags().Int64Var(&transactionID, "transaction-id", 0, "escrow approval body field which is int64")
escrowApprovalCmd.Flags().BoolVar(&approval, "approval", false, "escrow approval body field which is bool")
/*
MultiSig Command
*/
multiSigCmd.Flags().StringSliceVar(&addresses, "addresses", []string{}, "list of participants "+
"--addresses='address1,address2'")
multiSigCmd.Flags().Int64Var(&nonce, "nonce", 0, "random number / access code for the multisig info")
multiSigCmd.Flags().Uint32Var(&minSignature, "min-signature", 0, "minimum number of signature required for the transaction "+
"to be valid")
multiSigCmd.Flags().StringVar(&unsignedTxHex, "unsigned-transaction", "", "hex string of the unsigned transaction bytes")
multiSigCmd.Flags().StringVar(&txHash, "transaction-hash", "", "hash of transaction being signed by address-signature list (hex)")
multiSigCmd.Flags().StringSliceVar(&addressSignatures, "address-signatures", []string{}, "address-signature list "+
"--address-signatures='address1-signature1,address2-signature2'")
}

// Commands set TXGeneratorCommandsInstance that will used by whole commands
Expand All @@ -148,6 +167,8 @@ func Commands(sqliteDB *sql.DB) *cobra.Command {
txCmd.AddCommand(removeAccountDatasetCmd)
escrowApprovalCmd.Run = txGeneratorCommandsInstance.EscrowApprovalProcess()
txCmd.AddCommand(escrowApprovalCmd)
multiSigCmd.Run = txGeneratorCommandsInstance.MultiSignatureProcess()
txCmd.AddCommand(multiSigCmd)
return txCmd
}

Expand Down Expand Up @@ -258,3 +279,16 @@ func (*TXGeneratorCommands) EscrowApprovalProcess() RunCommand {
PrintTx(GenerateSignedTxBytes(tx, senderSeed), outputType)
}
}

// MultiSignatureProcess for generate TX MultiSignature type
func (*TXGeneratorCommands) MultiSignatureProcess() RunCommand {
return func(ccmd *cobra.Command, args []string) {
tx := GenerateBasicTransaction(senderSeed, version, timestamp, fee, recipientAccountAddress)
tx = GeneratedMultiSignatureTransaction(tx, minSignature, nonce, unsignedTxHex, txHash, addressSignatures, addresses)
if tx == nil {
fmt.Printf("fail to generate transaction, please check the provided parameter")
} else {
PrintTx(GenerateSignedTxBytes(tx, senderSeed), outputType)
}
}
}
9 changes: 9 additions & 0 deletions cmd/transaction/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
"setupAccountDataset": {3, 0, 0, 0},
"removeAccountDataset": {3, 1, 0, 0},
"approvalEscrow": {4, 0, 0, 0},
"multiSignature": {5, 0, 0, 0},
}
signature = &crypto.Signature{}

Expand Down Expand Up @@ -46,4 +47,12 @@ var (
// escrowApproval
approval bool
transactionID int64

// multiSignature
unsignedTxHex string
addressSignatures []string
txHash string
addresses []string
nonce int64
minSignature uint32
)
75 changes: 75 additions & 0 deletions cmd/transaction/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ func GenerateSignedTxBytes(tx *model.Transaction, senderSeed string) []byte {
senderSeed,
)
signedTxBytes, _ := transactionUtil.GetTransactionBytes(tx, true)
fmt.Printf("signedBytes: %v\n", len(signedTxBytes))
return signedTxBytes
}

Expand Down Expand Up @@ -324,3 +325,77 @@ func GenerateEscrowedTransaction(
}
return tx
}

/*
GeneratedMultiSignatureTransaction inject escrow. Need:
1. unsignedTxHex
2. signatures
3. multisigInfo:
- minSignature
- nonce
- addresses
Invalid escrow validation when those fields has not set
*/
func GeneratedMultiSignatureTransaction(
tx *model.Transaction,
minSignature uint32,
nonce int64,
unsignedTxHex, txHash string,
addressSignatures, addresses []string,
) *model.Transaction {
var (
signatures = make(map[string][]byte)
signatureInfo *model.SignatureInfo
unsignedTx []byte
multiSigInfo *model.MultiSignatureInfo
err error
)
if minSignature > 0 && len(addresses) > 0 {
multiSigInfo = &model.MultiSignatureInfo{
MinimumSignatures: minSignature,
Nonce: nonce,
Addresses: addresses,
}
}
if unsignedTxHex != "" {
unsignedTx, err = hex.DecodeString(unsignedTxHex)
if err != nil {
return nil
}
}

if txHash != "" {
transactionHash, err := hex.DecodeString(txHash)
if err != nil {
return nil
}
for _, v := range addressSignatures {
asig := strings.Split(v, "-")
if len(asig) < 2 {
return nil
}
signature, err := hex.DecodeString(asig[1])
if err != nil {
return nil
}
signatures[asig[0]] = signature
}
signatureInfo = &model.SignatureInfo{
TransactionHash: transactionHash,
Signatures: signatures,
}
}

tx.TransactionType = util.ConvertBytesToUint32(txTypeMap["multiSignature"])
txBody := &model.MultiSignatureTransactionBody{
MultiSignatureInfo: multiSigInfo,
UnsignedTransactionBytes: unsignedTx,
SignatureInfo: signatureInfo,
}
tx.TransactionBodyBytes = (&transaction.MultiSignatureTransaction{
Body: txBody,
}).GetBodyBytes()
fmt.Printf("length: %v\n", len(tx.TransactionBodyBytes))
tx.TransactionBodyLength = uint32(len(tx.TransactionBodyBytes))
return tx
}
16 changes: 16 additions & 0 deletions common/constant/fieldsSize.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ var (
EscrowID uint32 = 8
EscrowApprovalBytesLength = EscrowApproval + EscrowID
EscrowInstructionLength uint32 = 4
MultisigFieldLength uint32 = 4
// MultiSigFieldMissing indicate fields is missing, no need to read the bytes
MultiSigFieldMissing uint32
// MultiSigFieldPresent indicate fields is present, parse the byte accordingly
MultiSigFieldPresent uint32 = 1
MultiSigAddressLength uint32 = 4
MultiSigSignatureLength uint32 = 4
MultiSigSignatureAddressLength uint32 = 4
MultiSigNumberOfAddress uint32 = 4
MultiSigNumberOfSignatures uint32 = 4
MultiSigUnsignedTxBytesLength uint32 = 4
MultiSigInfoSize uint32 = 4
MultiSigInfoSignatureInfoSize uint32 = 4
MultiSigInfoNonce uint32 = 8
MultiSigInfoMinSignature uint32 = 4
MultiSigTransactionHash uint32 = 32
)
Loading