-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1194 from iost-official/refactor
Refactor
- Loading branch information
Showing
69 changed files
with
4,930 additions
and
906 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,72 @@ | ||
package account | ||
|
||
import ( | ||
"github.com/iost-official/go-iost/v3/common" | ||
"github.com/iost-official/go-iost/v3/crypto" | ||
) | ||
|
||
// KeyPair account of the ios | ||
type KeyPair struct { | ||
Algorithm crypto.Algorithm | ||
Pubkey []byte | ||
Seckey []byte | ||
// Account type of a permission tree | ||
type Account struct { | ||
ID string `json:"id"` | ||
Referrer string `json:"referrer"` | ||
Groups map[string]*Group `json:"groups"` | ||
Permissions map[string]*Permission `json:"permissions"` | ||
} | ||
|
||
// NewKeyPair create an account | ||
func NewKeyPair(seckey []byte, algo crypto.Algorithm) (*KeyPair, error) { | ||
if seckey == nil { | ||
seckey = algo.GenSeckey() | ||
} | ||
|
||
err := algo.CheckSeckey(seckey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pubkey := algo.GetPubkey(seckey) | ||
|
||
account := &KeyPair{ | ||
Algorithm: algo, | ||
Pubkey: pubkey, | ||
Seckey: seckey, | ||
} | ||
return account, nil | ||
// Item identity of a permission owner | ||
type Item struct { | ||
ID string `json:"id"` // key pair id | ||
Permission string `json:"permission"` | ||
IsKeyPair bool `json:"is_key_pair"` | ||
Weight int `json:"weight"` | ||
} | ||
|
||
// Sign sign a tx | ||
func (a *KeyPair) Sign(info []byte) *crypto.Signature { | ||
return crypto.NewSignature(a.Algorithm, info, a.Seckey) | ||
// Group group of permissions | ||
type Group struct { | ||
Name string `json:"name"` | ||
Items []*Item `json:"items"` | ||
} | ||
|
||
// ReadablePubkey ... | ||
func (a *KeyPair) ReadablePubkey() string { | ||
return EncodePubkey(a.Pubkey) | ||
// Permission permission struct | ||
type Permission struct { | ||
Name string `json:"name"` | ||
Groups []string `json:"groups"` | ||
Items []*Item `json:"items"` | ||
Threshold int `json:"threshold"` | ||
} | ||
|
||
// EncodePubkey ... | ||
func EncodePubkey(pubkey []byte) string { | ||
return common.Base58Encode(pubkey) | ||
// NewAccount a new empty account | ||
func NewAccount(id string) *Account { | ||
return &Account{ | ||
ID: id, | ||
Groups: make(map[string]*Group), | ||
Permissions: make(map[string]*Permission), | ||
} | ||
} | ||
|
||
// DecodePubkey ... | ||
func DecodePubkey(readablePubKey string) []byte { | ||
return common.Base58Decode(readablePubKey) | ||
// NewAccountFromKeys new account with owner and active key | ||
func NewAccountFromKeys(id, ownerKey, activeKey string) *Account { | ||
a := &Account{ | ||
ID: id, | ||
Groups: make(map[string]*Group), | ||
Permissions: make(map[string]*Permission), | ||
} | ||
a.Permissions["owner"] = &Permission{ | ||
Name: "owner", | ||
Threshold: 1, | ||
Items: []*Item{ | ||
{ | ||
ID: ownerKey, | ||
IsKeyPair: true, | ||
Weight: 1, | ||
}, | ||
}, | ||
} | ||
a.Permissions["active"] = &Permission{ | ||
Name: "active", | ||
Threshold: 1, | ||
Items: []*Item{ | ||
{ | ||
ID: activeKey, | ||
IsKeyPair: true, | ||
Weight: 1, | ||
}, | ||
}, | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package account | ||
|
||
import ( | ||
"github.com/iost-official/go-iost/v3/common" | ||
"github.com/iost-official/go-iost/v3/crypto" | ||
) | ||
|
||
// KeyPair keypair of the iost account | ||
type KeyPair struct { | ||
Algorithm crypto.Algorithm | ||
Pubkey []byte | ||
Seckey []byte | ||
} | ||
|
||
// NewKeyPair create a keypair | ||
func NewKeyPair(seckey []byte, algo crypto.Algorithm) (*KeyPair, error) { | ||
if seckey == nil { | ||
seckey = algo.GenSeckey() | ||
} | ||
|
||
err := algo.CheckSeckey(seckey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pubkey := algo.GetPubkey(seckey) | ||
|
||
account := &KeyPair{ | ||
Algorithm: algo, | ||
Pubkey: pubkey, | ||
Seckey: seckey, | ||
} | ||
return account, nil | ||
} | ||
|
||
// Sign sign a tx | ||
func (a *KeyPair) Sign(info []byte) *crypto.Signature { | ||
return crypto.NewSignature(a.Algorithm, info, a.Seckey) | ||
} | ||
|
||
// ReadablePubkey ... | ||
func (a *KeyPair) ReadablePubkey() string { | ||
return EncodePubkey(a.Pubkey) | ||
} | ||
|
||
// EncodePubkey ... | ||
func EncodePubkey(pubkey []byte) string { | ||
return common.Base58Encode(pubkey) | ||
} | ||
|
||
// DecodePubkey ... | ||
func DecodePubkey(readablePubKey string) []byte { | ||
return common.Base58Decode(readablePubKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/iost-official/go-iost/v3/metrics" | ||
) | ||
|
||
// ModeType is the type of mode. | ||
type ModeType uint | ||
|
||
// Constant of mode type. | ||
const ( | ||
ModeNormal ModeType = iota | ||
ModeSync | ||
ModeInit | ||
) | ||
|
||
var ( | ||
mode = ModeInit | ||
modeMutex = new(sync.RWMutex) | ||
modeGauge = metrics.NewGauge("iost_node_mode", nil) | ||
) | ||
|
||
func init() { | ||
modeGauge.Set(float64(mode), nil) | ||
} | ||
|
||
// Mode will return the mode of iserver. | ||
func Mode() string { | ||
modeMutex.RLock() | ||
defer modeMutex.RUnlock() | ||
|
||
switch mode { | ||
case ModeNormal: | ||
return "ModeNormal" | ||
case ModeSync: | ||
return "ModeSync" | ||
case ModeInit: | ||
return "ModeInit" | ||
default: | ||
return "Undefined" | ||
} | ||
} | ||
|
||
// SetMode will set the mode of iserver. | ||
func SetMode(m ModeType) { | ||
modeMutex.Lock() | ||
defer modeMutex.Unlock() | ||
|
||
mode = m | ||
modeGauge.Set(float64(mode), nil) | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.