Skip to content

Commit

Permalink
address from private key
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Sep 17, 2020
1 parent a296762 commit b9337b6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
19 changes: 19 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package bitcoin

import (
"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvd/chaincfg"
"github.com/bitcoinsv/bsvutil"
)

func AddressFromPrivKey(privKey string) string {
pubKey := PrivateKey(privKey).PubKey()
return Address(pubKey).EncodeAddress()
}

// Address gets a bsvutil.LegacyAddressPubKeyHash
func Address(publicKey *bsvec.PublicKey) (address *bsvutil.LegacyAddressPubKeyHash) {
publicKeyHash := bsvutil.Hash160(publicKey.SerializeCompressed())
address, _ = bsvutil.NewLegacyAddressPubKeyHash(publicKeyHash, &chaincfg.MainNetParams)
return
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9
github.com/itchyny/base58-go v0.1.0
github.com/piotrnar/gocoin v0.0.0-20200912093848-56d57d5b6be5
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9 h1:hFI8rT84FCA0FFy3cFrkW5Nz4FyNKlIdCvEvvTNySKg=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9/go.mod h1:p44KuNKUH5BC8uX4ONEODaHUR4+ibC8todEAOGQEJAM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/itchyny/base58-go v0.1.0 h1:zF5spLDo956exUAD17o+7GamZTRkXOZlqJjRciZwd1I=
github.com/itchyny/base58-go v0.1.0/go.mod h1:SrMWPE3DFuJJp1M/RUhu4fccp/y9AlB8AL3o3duPToU=
Expand Down
23 changes: 23 additions & 0 deletions privateKey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bitcoin

import (
"crypto/ecdsa"
"math/big"

"github.com/bitcoinsv/bsvd/bsvec"
)

func PrivateKey(privKey string) (ecdsaPrivKey *bsvec.PrivateKey) {
privKeyBytes := HexDecode(privKey)
x, y := bsvec.S256().ScalarBaseMult(privKeyBytes)
ecdsaPubKey := ecdsa.PublicKey{
Curve: bsvec.S256(),
X: x,
Y: y,
}
ecdsaPrivKey = &bsvec.PrivateKey{
PublicKey: ecdsaPubKey,
D: new(big.Int).SetBytes(privKeyBytes),
}
return
}
14 changes: 1 addition & 13 deletions sign.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package bitcoin

import (
"crypto/ecdsa"
"encoding/base64"
"math/big"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/bitcoinsv/bsvd/chaincfg/chainhash"
Expand All @@ -17,17 +15,7 @@ func SignMessage(privKey string, message string) string {
bytes = append(bytes, prefixBytes...)
bytes = append(bytes, byte(len(messageBytes)))
bytes = append(bytes, messageBytes...)
privKeyBytes := HexDecode(privKey)
x, y := bsvec.S256().ScalarBaseMult(privKeyBytes)
ecdsaPubKey := ecdsa.PublicKey{
Curve: bsvec.S256(),
X: x,
Y: y,
}
ecdsaPrivKey := &bsvec.PrivateKey{
PublicKey: ecdsaPubKey,
D: new(big.Int).SetBytes(privKeyBytes),
}
ecdsaPrivKey := PrivateKey(privKey)

sigbytes, err := bsvec.SignCompact(bsvec.S256(), ecdsaPrivKey, chainhash.DoubleHashB(bytes), true)
if err != nil {
Expand Down

0 comments on commit b9337b6

Please sign in to comment.