Skip to content
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
67 changes: 34 additions & 33 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,64 @@
package account

import (
"github.com/SealSC/SealABC/crypto/ciphers/cipherCommon"
"github.com/SealSC/SealABC/crypto/signers"
"github.com/SealSC/SealABC/crypto/signers/signerCommon"
"github.com/SealSC/SealABC/common"
"github.com/SealSC/SealABC/crypto/ciphers/cipherCommon"
"github.com/SealSC/SealABC/crypto/signers"
"github.com/SealSC/SealABC/crypto/signers/signerCommon"
)

const encryptedKeyLen = 32

type StoreConfig struct {
CipherType string
CipherParam []byte
CipherType string
CipherParam []byte

KDFType string
KDFSalt []byte
KDFParam []byte
KDFType string
KDFSalt []byte
KDFParam []byte

KeyLength int
KeyLength int
}

type Encrypted struct {
Address string
Data cipherCommon.EncryptedData
Config StoreConfig
Address common.Address
Data cipherCommon.EncryptedData
Config StoreConfig
}

type accountDataForEncrypt struct {
SignerType string
KeyData []byte
SignerType string
KeyData []byte
}

type SealAccount struct {
Address string
SingerType string
Signer signerCommon.ISigner
Address common.Address
SingerType string
Signer signerCommon.ISigner
}

func NewAccount(privateKey []byte, sg signers.ISignerGenerator) (sa SealAccount, err error) {
signer, err := sg.NewSigner(privateKey)
signer, err := sg.NewSigner(privateKey)

if err != nil {
return
}
if err != nil {
return
}

sa.Address = signer.PublicKeyString()
sa.SingerType = signer.Type()
sa.Signer = signer
return
sa.Address = common.BytesToAddress(signer.PublicKeyBytes())
sa.SingerType = signer.Type()
sa.Signer = signer
return
}

func NewAccountForVerify(publicKey []byte, sg signers.ISignerGenerator) (sa SealAccount, err error) {
signer, err := sg.FromRawPublicKey(publicKey)
if err != nil {
return
}
signer, err := sg.FromRawPublicKey(publicKey)
if err != nil {
return
}

sa.Address = signer.PublicKeyString()
sa.SingerType = signer.Type()
sa.Signer = signer
sa.Address = common.BytesToAddress(signer.PublicKeyBytes())
sa.SingerType = signer.Type()
sa.Signer = signer

return
return
}
13 changes: 6 additions & 7 deletions account/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
package account

import (
"bytes"
"encoding/json"
"errors"
"github.com/SealSC/SealABC/crypto/ciphers"
"github.com/SealSC/SealABC/crypto/ciphers/cipherCommon"
"github.com/SealSC/SealABC/crypto/kdf/pbkdf2"
"github.com/SealSC/SealABC/crypto/signers"
"encoding/json"
"errors"
"io/ioutil"
)

Expand All @@ -47,7 +48,7 @@ func (s SealAccount) Store(filename string, password string, cipher ciphers.ICip

encrypted.Address = s.Address
encrypted.Data = encData
encrypted.Config = StoreConfig {
encrypted.Config = StoreConfig{
CipherType: cipher.Type(),
CipherParam: []byte(encMode),
KDFType: pbkdf2.Generator.Name(),
Expand All @@ -61,7 +62,6 @@ func (s SealAccount) Store(filename string, password string, cipher ciphers.ICip
return
}


err = ioutil.WriteFile(filename, fileData, 0666)
return
}
Expand All @@ -72,7 +72,7 @@ func (s *SealAccount) FromStore(filename string, password string) (sa SealAccoun
return
}

encAccount := Encrypted {}
encAccount := Encrypted{}
err = json.Unmarshal(data, &encAccount)
if err != nil {
return
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *SealAccount) FromStore(filename string, password string) (sa SealAccoun
return
}

if signer.PublicKeyString() != encAccount.Address {
if !bytes.Equal(signer.PublicKeyBytes(), encAccount.Address.Bytes()) {
err = errors.New("address not equal")
return
}
Expand All @@ -119,4 +119,3 @@ func (s *SealAccount) FromStore(filename string, password string) (sa SealAccoun
sa.Signer = signer
return
}

4 changes: 2 additions & 2 deletions cli/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
package cli

import (
"github.com/SealSC/SealABC/cli/cliFlags"
"errors"
"github.com/SealSC/SealABC/cli/cliFlags"
cliV2 "github.com/urfave/cli/v2"
)

func SetAction(app *cliV2.App) {
app.Action = func(c *cliV2.Context) error {
app.Action = func(c *cliV2.Context) error {
cfgFile := c.String(cliFlags.Config)

//config file
Expand Down
70 changes: 35 additions & 35 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@
package cli

import (
"github.com/SealSC/SealABC/cli/cliFlags"
cliV2 "github.com/urfave/cli/v2"
"io"
"os"
"github.com/SealSC/SealABC/cli/cliFlags"
cliV2 "github.com/urfave/cli/v2"
"io"
"os"
)

func Run() (app *cliV2.App) {

oldHelpPrinter := cliV2.HelpPrinter
cliV2.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
oldHelpPrinter(w, templ, data)
os.Exit(0)
}

oldVersionPrinter := cliV2.VersionPrinter
cliV2.VersionPrinter = func(c *cliV2.Context) {
oldVersionPrinter(c)
os.Exit(0)
}

app = cliV2.NewApp()
app.Name = "SealABC"
app.Version = "0.1"
app.HelpName = "SealABC"
app.Usage = "SealABC"
app.UsageText = "SealABC [options] [args]"
app.HideHelp = false
app.HideVersion = false

cliFlags.SetFlags(app)
SetAction(app)

//run
err := app.Run(os.Args)
if nil != err {
os.Exit(-1)
}

return
oldHelpPrinter := cliV2.HelpPrinter
cliV2.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
oldHelpPrinter(w, templ, data)
os.Exit(0)
}

oldVersionPrinter := cliV2.VersionPrinter
cliV2.VersionPrinter = func(c *cliV2.Context) {
oldVersionPrinter(c)
os.Exit(0)
}

app = cliV2.NewApp()
app.Name = "SealABC"
app.Version = "0.1"
app.HelpName = "SealABC"
app.Usage = "SealABC"
app.UsageText = "SealABC [options] [args]"
app.HideHelp = false
app.HideVersion = false

cliFlags.SetFlags(app)
SetAction(app)

//run
err := app.Run(os.Args)
if nil != err {
os.Exit(-1)
}

return
}
6 changes: 3 additions & 3 deletions cli/cliFlags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

func newConfigFlag() cliV2.Flag {
return &(cliV2.StringFlag{
Name: Config,
Usage: "set config file name",
Hidden: false,
Name: Config,
Usage: "set config file name",
Hidden: false,
Required: true,
})
}
2 changes: 1 addition & 1 deletion cli/cliFlags/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
package cliFlags

const (
Config = "config"
Config = "config"
)
4 changes: 2 additions & 2 deletions cli/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package cli

type parameters struct {
ConfigFile string
ConfigFile string
}

var Parameters = parameters{
"",
"",
}
14 changes: 14 additions & 0 deletions common/big.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

import "math/big"

// Common big integers often used
var (
Big1 = big.NewInt(1)
Big2 = big.NewInt(2)
Big3 = big.NewInt(3)
Big0 = big.NewInt(0)
Big32 = big.NewInt(32)
Big256 = big.NewInt(0xff)
Big257 = big.NewInt(257)
)
11 changes: 11 additions & 0 deletions common/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common

func CopyBytes(b []byte) (copiedBytes []byte) {
if b == nil {
return nil
}
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)

return
}
6 changes: 3 additions & 3 deletions common/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package common

const (
JSON_MARSHAL_INDENT = " "
JSON_MARSHAL_INDENT = " "

INT256_BYTE_COUNT = 8
INT256_BYTE_COUNT = 8

BASIC_TIME_FORMAT = "2006-01-02 15:04:05"
BASIC_TIME_FORMAT = "2006-01-02 15:04:05"
)
60 changes: 60 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package common

import "encoding/hex"

const (
HashLength = 32
AddressLength = 20
)

type Hash [HashLength]byte
type Address [AddressLength]byte

func (h Hash) Bytes() []byte { return h[:] }

func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }

func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}

func FromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
if len(s)%2 == 1 {
s = "0" + s
}
h, _ := hex.DecodeString(s)
return h

}
return nil
}

func (h *Hash) SetBytes(b []byte) {
if len(b) > len(h) {
b = b[len(b)-HashLength:]
}

copy(h[HashLength-len(b):], b)
}

func BytesToAddress(b []byte) Address {
var a Address
a.SetBytes(b)
return a
}

func (a *Address) SetBytes(b []byte) {
if len(b) > len(a) {
b = b[len(b)-AddressLength:]
}
copy(a[AddressLength-len(b):], b)
}

func (a *Address) Bytes() []byte { return a[:] }
func (a *Address) String() string { return hex.EncodeToString(a.Bytes()) }
Loading