forked from nebulasio/go-nebulas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add Account type, finish address algorithm.
crypto: add ecdsa related functions.
- Loading branch information
1 parent
ee59283
commit 7e69d24
Showing
6 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2017 go-nebulas authors | ||
// | ||
// This file is part of the go-nebulas library. | ||
// | ||
// the go-nebulas library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// the go-nebulas library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
package core | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
|
||
"github.com/satori/go.uuid" | ||
) | ||
|
||
type Account struct { | ||
// uuid is random for unique id | ||
id string | ||
|
||
// nick for user account | ||
nick string | ||
|
||
address Address | ||
|
||
// ecdsa private key ,public & address can be derived from it | ||
privateKey ecdsa.PrivateKey | ||
} | ||
|
||
// create new account with nick and private key | ||
func newAccountFromECDSA(nick string, privateKeyECDSA *ecdsa.PrivateKey) *Account { | ||
id := fmt.Sprintf("%s", uuid.NewV4()) | ||
addr := NewAddressWithPrivateKey(privateKeyECDSA) | ||
account := &Account{ | ||
id: id, | ||
nick: nick, | ||
address: *addr, | ||
privateKey: *privateKeyECDSA, | ||
} | ||
return account | ||
} |
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,74 @@ | ||
// Copyright (C) 2017 go-nebulas authors | ||
// | ||
// This file is part of the go-nebulas library. | ||
// | ||
// the go-nebulas library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// the go-nebulas library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
package crypto | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"io" | ||
|
||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"github.com/ethereum/go-ethereum/crypto/secp256k1" | ||
"math/big" | ||
) | ||
|
||
// S256 returns an instance of the secp256k1 curve. | ||
func S256() elliptic.Curve { | ||
return secp256k1.S256() | ||
} | ||
|
||
// generate a ecdsa private key | ||
func GenerateECDSAPrivateKey(rand io.Reader) (*ecdsa.PrivateKey, error) { | ||
privateKeyECDSA, err := ecdsa.GenerateKey(S256(), rand) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return privateKeyECDSA, nil | ||
} | ||
|
||
// convert ecdsa public key to bytes | ||
func FromECDSAPub(pub *ecdsa.PublicKey) []byte { | ||
if pub == nil || pub.X == nil || pub.Y == nil { | ||
return nil | ||
} | ||
return elliptic.Marshal(S256(), pub.X, pub.Y) | ||
} | ||
|
||
// HexToECDSAPrivate parses a secp256k1 private key. | ||
func HexToECDSAPrivate(hexkey string) (*ecdsa.PrivateKey, error) { | ||
b, err := hex.DecodeString(hexkey) | ||
if err != nil { | ||
return nil, errors.New("invalid hex string") | ||
} | ||
return ToECDSAPrivate(b) | ||
} | ||
|
||
// ToECDSAPrivate creates a private key with the given data value. | ||
func ToECDSAPrivate(d []byte) (*ecdsa.PrivateKey, error) { | ||
priv := new(ecdsa.PrivateKey) | ||
priv.PublicKey.Curve = S256() | ||
if 8*len(d) != priv.Params().BitSize { | ||
return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize) | ||
} | ||
priv.D = new(big.Int).SetBytes(d) | ||
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d) | ||
return priv, nil | ||
} |
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