2021-10-26 11:52:26 +00:00
|
|
|
package owner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-11-22 12:38:15 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/sha256"
|
2021-10-26 11:52:26 +00:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2021-11-22 12:38:15 +00:00
|
|
|
"golang.org/x/crypto/ripemd160"
|
2021-10-26 11:52:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NEO3Wallet represents NEO3 wallet address.
|
|
|
|
type NEO3Wallet [NEO3WalletSize]byte
|
|
|
|
|
|
|
|
// NEO3WalletSize contains size of neo3 wallet.
|
|
|
|
const NEO3WalletSize = 25
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
const addressPrefixN3 = 0x35
|
|
|
|
|
2021-10-26 11:52:26 +00:00
|
|
|
// ErrEmptyPublicKey when PK passed to Verify method is nil.
|
|
|
|
var ErrEmptyPublicKey = errors.New("empty public key")
|
|
|
|
|
|
|
|
// NEO3WalletFromPublicKey converts public key to NEO3 wallet address.
|
|
|
|
func NEO3WalletFromPublicKey(key *ecdsa.PublicKey) (*NEO3Wallet, error) {
|
|
|
|
if key == nil {
|
|
|
|
return nil, ErrEmptyPublicKey
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
b := elliptic.MarshalCompressed(key.Curve, key.X, key.Y)
|
|
|
|
script := []byte{0x0C /* PUSHDATA1 */, byte(len(b)) /* 33 */}
|
|
|
|
script = append(script, b...)
|
|
|
|
script = append(script, 0x41 /* SYSCALL */)
|
|
|
|
h := sha256.Sum256([]byte("System.Crypto.CheckSig"))
|
|
|
|
script = append(script, h[:4]...)
|
2021-10-26 11:52:26 +00:00
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
h1 := sha256.Sum256(script)
|
|
|
|
rw := ripemd160.New()
|
|
|
|
rw.Write(h1[:])
|
|
|
|
h160 := rw.Sum(nil)
|
2021-10-26 11:52:26 +00:00
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
var w NEO3Wallet
|
|
|
|
w[0] = addressPrefixN3
|
|
|
|
copy(w[1:21], h160)
|
|
|
|
copy(w[21:], addressChecksum(w[:21]))
|
2021-10-26 11:52:26 +00:00
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
return &w, nil
|
|
|
|
}
|
2021-10-26 11:52:26 +00:00
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
func addressChecksum(data []byte) []byte {
|
|
|
|
h1 := sha256.Sum256(data)
|
|
|
|
h2 := sha256.Sum256(h1[:])
|
|
|
|
return h2[:4]
|
2021-10-26 11:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (w *NEO3Wallet) String() string {
|
|
|
|
if w != nil {
|
|
|
|
return base58.Encode(w[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns slice of NEO3 wallet address bytes.
|
|
|
|
func (w *NEO3Wallet) Bytes() []byte {
|
|
|
|
if w != nil {
|
|
|
|
return w[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|