Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add StringCompressed() for PublicKey #3408

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
3 changes: 1 addition & 2 deletions cli/cmdargs/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmdargs

import (
"encoding/hex"
"strings"
"testing"

Expand Down Expand Up @@ -45,7 +44,7 @@ func TestParseCosigner(t *testing.T) {
Scopes: transaction.CalledByEntry | transaction.CustomContracts,
AllowedContracts: []util.Uint160{c1, c2},
},
acc.StringLE() + ":CustomGroups:" + hex.EncodeToString(priv.PublicKey().Bytes()): {
acc.StringLE() + ":CustomGroups:" + priv.PublicKey().StringCompressed(): {
Account: acc,
Scopes: transaction.CustomGroups,
AllowedGroups: keys.PublicKeys{priv.PublicKey()},
Expand Down
7 changes: 3 additions & 4 deletions cli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package query
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"sort"
"strconv"
Expand Down Expand Up @@ -204,7 +203,7 @@ func queryCandidates(ctx *cli.Context) error {
var res []byte
res = fmt.Appendf(res, "Key\tVotes\tCommittee\tConsensus\n")
for _, val := range vals {
res = fmt.Appendf(res, "%s\t%d\t%t\t%t\n", hex.EncodeToString(val.PublicKey.Bytes()), val.Votes, comm.Contains(&val.PublicKey), val.Active)
res = fmt.Appendf(res, "%s\t%d\t%t\t%t\n", val.PublicKey.StringCompressed(), val.Votes, comm.Contains(&val.PublicKey), val.Active)
}
tw := tabwriter.NewWriter(ctx.App.Writer, 0, 2, 2, ' ', 0)
_, err = tw.Write(res)
Expand Down Expand Up @@ -235,7 +234,7 @@ func queryCommittee(ctx *cli.Context) error {
}

for _, k := range comm {
fmt.Fprintln(ctx.App.Writer, hex.EncodeToString(k.Bytes()))
fmt.Fprintln(ctx.App.Writer, k.StringCompressed())
}
return nil
}
Expand Down Expand Up @@ -306,7 +305,7 @@ func queryVoter(ctx *cli.Context) error {
}
voted := "null"
if st.VoteTo != nil {
voted = fmt.Sprintf("%s (%s)", hex.EncodeToString(st.VoteTo.Bytes()), address.Uint160ToString(st.VoteTo.GetScriptHash()))
voted = fmt.Sprintf("%s (%s)", st.VoteTo.StringCompressed(), address.Uint160ToString(st.VoteTo.GetScriptHash()))
}
fmt.Fprintf(ctx.App.Writer, "\tVoted: %s\n", voted)
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", fixedn.ToString(&st.Balance, int(dec)))
Expand Down
4 changes: 1 addition & 3 deletions cli/smartcontract/permission.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package smartcontract

import (
"encoding/hex"
"errors"
"fmt"

Expand All @@ -28,10 +27,9 @@ func (p permission) MarshalYAML() (any, error) {
&yaml.Node{Kind: yaml.ScalarNode, Value: permHashKey},
&yaml.Node{Kind: yaml.ScalarNode, Value: p.Contract.Value.(util.Uint160).StringLE()})
case manifest.PermissionGroup:
bs := p.Contract.Value.(*keys.PublicKey).Bytes()
m.Content = append(m.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: permGroupKey},
&yaml.Node{Kind: yaml.ScalarNode, Value: hex.EncodeToString(bs)})
&yaml.Node{Kind: yaml.ScalarNode, Value: p.Contract.Value.(*keys.PublicKey).StringCompressed()})
default:
return nil, fmt.Errorf("invalid permission type: %d", p.Contract.Type)
}
Expand Down
5 changes: 2 additions & 3 deletions cli/smartcontract/smart_contract_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package smartcontract

import (
"encoding/hex"
"flag"
"os"
"testing"
Expand Down Expand Up @@ -109,7 +108,7 @@ func TestPermissionMarshal(t *testing.T) {
p.Methods.Add("abc")
p.Methods.Add("lamao")
testPermissionMarshal(t, p,
"group: "+hex.EncodeToString(priv.PublicKey().Bytes())+"\n"+
"group: "+priv.PublicKey().StringCompressed()+"\n"+
"methods:\n - abc\n - lamao\n")
})
}
Expand All @@ -118,7 +117,7 @@ func TestPermissionUnmarshalInvalid(t *testing.T) {
priv, err := keys.NewPrivateKey()
require.NoError(t, err)

pub := hex.EncodeToString(priv.PublicKey().Bytes())
pub := priv.PublicKey().StringCompressed()
u160 := random.Uint160().StringLE()
testCases := []string{
"hash: []\nmethods: '*'\n", // invalid hash type
Expand Down
3 changes: 1 addition & 2 deletions cli/wallet/candidate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wallet_test

import (
"encoding/hex"
"math/big"
"strconv"
"testing"
Expand All @@ -18,7 +17,7 @@ func TestRegisterCandidate(t *testing.T) {

validatorAddress := testcli.ValidatorPriv.Address()
validatorPublic := testcli.ValidatorPriv.PublicKey()
validatorHex := hex.EncodeToString(validatorPublic.Bytes())
validatorHex := validatorPublic.StringCompressed()

e.In.WriteString("one\r")
e.Run(t, "neo-go", "wallet", "nep17", "multitransfer",
Expand Down
7 changes: 3 additions & 4 deletions cli/wallet/multisig_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wallet_test

import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
Expand Down Expand Up @@ -45,9 +44,9 @@ func TestSignMultisigTx(t *testing.T) {
"--wallet", w,
"--wif", wif,
"--min", "2",
hex.EncodeToString(pubs[0].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()))
pubs[0].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed())
}
addAccount(wallet1Path, privs[0].WIF())
addAccount(wallet2Path, privs[1].WIF())
Expand Down
51 changes: 25 additions & 26 deletions cli/wallet/wallet_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wallet_test

import (
"encoding/hex"
"encoding/json"
"math/big"
"os"
Expand Down Expand Up @@ -437,16 +436,16 @@ func TestWalletInit(t *testing.T) {
"--wallet", walletPath,
"--min", "2"}
t.Run("invalid pub encoding", func(t *testing.T) {
e.RunWithError(t, append(cmd, hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()),
e.RunWithError(t, append(cmd, pubs[1].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed(),
"not-a-pub")...)
})
t.Run("missing WIF", func(t *testing.T) {
e.RunWithError(t, append(cmd, hex.EncodeToString(pubs[0].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()),
hex.EncodeToString(pubs[3].Bytes()))...)
e.RunWithError(t, append(cmd, pubs[0].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed(),
pubs[3].StringCompressed())...)
})
cmd = append(cmd, "--wif", privs[0].WIF())
t.Run("InvalidPublicKeys", func(t *testing.T) {
Expand All @@ -455,18 +454,18 @@ func TestWalletInit(t *testing.T) {
e.In.WriteString("multipass\r")
defer e.In.Reset()

e.RunWithError(t, append(cmd, hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()),
hex.EncodeToString(pubs[3].Bytes()))...)
e.RunWithError(t, append(cmd, pubs[1].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed(),
pubs[3].StringCompressed())...)
})
e.In.WriteString("multiacc\r")
e.In.WriteString("multipass\r")
e.In.WriteString("multipass\r")
e.Run(t, append(cmd, hex.EncodeToString(pubs[0].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()),
hex.EncodeToString(pubs[3].Bytes()))...)
e.Run(t, append(cmd, pubs[0].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed(),
pubs[3].StringCompressed())...)

script, err := smartcontract.CreateMultiSigRedeemScript(2, pubs)
require.NoError(t, err)
Expand All @@ -482,10 +481,10 @@ func TestWalletInit(t *testing.T) {
e.In.WriteString("multiacc\r")
e.In.WriteString("multipass\r")
e.In.WriteString("multipass\r")
e.RunWithError(t, append(cmd, hex.EncodeToString(pubs[0].Bytes()),
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()),
hex.EncodeToString(pubs[3].Bytes()))...)
e.RunWithError(t, append(cmd, pubs[0].StringCompressed(),
pubs[1].StringCompressed(),
pubs[2].StringCompressed(),
pubs[3].StringCompressed())...)
})

privs, pubs = testcli.GenerateKeys(t, 3)
Expand All @@ -508,9 +507,9 @@ func TestWalletInit(t *testing.T) {
e.Run(t, "neo-go", "wallet", "import-multisig",
"--wallet", walletPath,
"--min", "2",
hex.EncodeToString(pubs[0].Bytes()), // Public key of the already imported account
hex.EncodeToString(pubs[1].Bytes()),
hex.EncodeToString(pubs[2].Bytes()))
pubs[0].StringCompressed(), // Public key of the already imported account
pubs[1].StringCompressed(),
pubs[2].StringCompressed())

w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err)
Expand All @@ -530,9 +529,9 @@ func TestWalletInit(t *testing.T) {
e.RunWithError(t, "neo-go", "wallet", "import-multisig",
"--wallet", walletPath,
"--min", "2",
hex.EncodeToString(pubsNew[0].Bytes()),
hex.EncodeToString(pubsNew[1].Bytes()),
hex.EncodeToString(pubsNew[2].Bytes()))
pubsNew[0].StringCompressed(),
pubsNew[1].StringCompressed(),
pubsNew[2].StringCompressed())

w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/basicchain/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func Init(t *testing.T, rootpath string, e *neotest.Executor) {
require.NoError(t, ntr.Accounts[0].Decrypt("one", ntr.Scrypt))
designateSuperInvoker.Invoke(t, stackitem.Null{}, "designateAsRole",
int64(noderoles.P2PNotary), []any{ntr.Accounts[0].PublicKey().Bytes()})
t.Logf("Designated Notary node: %s", hex.EncodeToString(ntr.Accounts[0].PublicKey().Bytes()))
t.Logf("Designated Notary node: %s", ntr.Accounts[0].PublicKey().StringCompressed())

// Block #10: push verification contract with arguments into the chain.
verifyPath = filepath.Join(testDataPrefix, "verify_args", "verification_with_args_contract.go")
Expand Down
7 changes: 3 additions & 4 deletions pkg/config/protocol_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"encoding/base64"
"encoding/hex"
"fmt"
"path/filepath"
"testing"
Expand Down Expand Up @@ -320,7 +319,7 @@ func TestGenesisExtensionsMarshalYAML(t *testing.T) {

t.Run("unmarshal config", func(t *testing.T) {
t.Run("good", func(t *testing.T) {
pubStr := hex.EncodeToString(pub.Bytes())
pubStr := pub.StringCompressed()
script := []byte{1, 2, 3, 4}
cfgYml := fmt.Sprintf(`ProtocolConfiguration:
Genesis:
Expand Down Expand Up @@ -354,7 +353,7 @@ func TestGenesisExtensionsMarshalYAML(t *testing.T) {
})

t.Run("unknown role", func(t *testing.T) {
pubStr := hex.EncodeToString(pub.Bytes())
pubStr := pub.StringCompressed()
cfgYml := fmt.Sprintf(`ProtocolConfiguration:
Genesis:
Roles:
Expand All @@ -367,7 +366,7 @@ func TestGenesisExtensionsMarshalYAML(t *testing.T) {
})

t.Run("last role", func(t *testing.T) {
pubStr := hex.EncodeToString(pub.Bytes())
pubStr := pub.StringCompressed()
cfgYml := fmt.Sprintf(`ProtocolConfiguration:
Genesis:
Roles:
Expand Down
12 changes: 9 additions & 3 deletions pkg/crypto/keys/publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (p *PublicKey) Cmp(key *PublicKey) int {
}

// NewPublicKeyFromString returns a public key created from the
// given hex string.
// given hex string public key representation in compressed form.
func NewPublicKeyFromString(s string) (*PublicKey, error) {
b, err := hex.DecodeString(s)
if err != nil {
Expand Down Expand Up @@ -364,7 +364,7 @@ func (p *PublicKey) String() string {

// MarshalJSON implements the json.Marshaler interface.
func (p PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(p.Bytes()))
return json.Marshal(p.StringCompressed())
}

// UnmarshalJSON implements the json.Unmarshaler interface.
Expand All @@ -389,7 +389,7 @@ func (p *PublicKey) UnmarshalJSON(data []byte) error {

// MarshalYAML implements the YAML marshaler interface.
func (p *PublicKey) MarshalYAML() (any, error) {
return hex.EncodeToString(p.Bytes()), nil
return p.StringCompressed(), nil
}

// UnmarshalYAML implements the YAML unmarshaler interface.
Expand All @@ -406,3 +406,9 @@ func (p *PublicKey) UnmarshalYAML(unmarshal func(any) error) error {
}
return p.DecodeBytes(b)
}

// StringCompressed returns the hex string representation of the public key
// in its compressed form.
func (p *PublicKey) StringCompressed() string {
return hex.EncodeToString(p.Bytes())
}
2 changes: 1 addition & 1 deletion pkg/crypto/keys/publickey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestDecodeFromString(t *testing.T) {
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
pubKey, err := NewPublicKeyFromString(str)
require.NoError(t, err)
require.Equal(t, str, hex.EncodeToString(pubKey.Bytes()))
require.Equal(t, str, pubKey.StringCompressed())

_, err = NewPublicKeyFromString(str[2:])
require.Error(t, err)
Expand Down
12 changes: 6 additions & 6 deletions pkg/neotest/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func init() {

// Config entry must contain validators first in a specific order.
standByCommittee = make([]string, len(pubs))
standByCommittee[0] = hex.EncodeToString(pubs[2].Bytes())
standByCommittee[1] = hex.EncodeToString(pubs[0].Bytes())
standByCommittee[2] = hex.EncodeToString(pubs[3].Bytes())
standByCommittee[3] = hex.EncodeToString(pubs[1].Bytes())
standByCommittee[4] = hex.EncodeToString(pubs[4].Bytes())
standByCommittee[5] = hex.EncodeToString(pubs[5].Bytes())
standByCommittee[0] = pubs[2].StringCompressed()
standByCommittee[1] = pubs[0].StringCompressed()
standByCommittee[2] = pubs[3].StringCompressed()
standByCommittee[3] = pubs[1].StringCompressed()
standByCommittee[4] = pubs[4].StringCompressed()
standByCommittee[5] = pubs[5].StringCompressed()

multiValidatorAcc = make([]*wallet.Account, 4)
sort.Sort(pubs[:4])
Expand Down
3 changes: 1 addition & 2 deletions pkg/services/oracle/response.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oracle

import (
"encoding/hex"
"errors"
"fmt"
gio "io"
Expand Down Expand Up @@ -45,7 +44,7 @@ func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) {
ok = pub.VerifyHashable(txSig, uint32(o.Network), incTx.backupTx)
if !ok {
o.Log.Debug("invalid response signature",
zap.String("pub", hex.EncodeToString(pub.Bytes())))
zap.String("pub", pub.StringCompressed()))
incTx.Unlock()
return
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/smartcontract/context/context_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package context

import (
"encoding/hex"
"encoding/json"
"testing"

Expand Down Expand Up @@ -185,7 +184,7 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
Value: sign,
}},
Signatures: map[string][]byte{
hex.EncodeToString(priv.PublicKey().Bytes()): sign,
priv.PublicKey().StringCompressed(): sign,
},
},
},
Expand Down
Loading
Loading