unwrap: add PublicKey to unwrap public keys

We have this type in NEP-14 directly.
This commit is contained in:
Roman Khimov 2022-10-27 14:45:40 +03:00
parent e0eff94094
commit f0abc035af
2 changed files with 25 additions and 0 deletions

View file

@ -146,6 +146,16 @@ func Uint256(r *result.Invoke, err error) (util.Uint256, error) {
return util.Uint256DecodeBytesBE(b) return util.Uint256DecodeBytesBE(b)
} }
// PublicKey expects correct execution (HALT state) with a single stack item
// returned. A public key is extracted from this item and returned.
func PublicKey(r *result.Invoke, err error) (*keys.PublicKey, error) {
b, err := Bytes(r, err)
if err != nil {
return nil, err
}
return keys.NewPublicKeyFromBytes(b, elliptic.P256())
}
// SessionIterator expects correct execution (HALT state) with a single stack // SessionIterator expects correct execution (HALT state) with a single stack
// item returned. If this item is an iterator it's returned to the caller along // item returned. If this item is an iterator it's returned to the caller along
// with the session ID. Notice that this function also returns successfully // with the session ID. Notice that this function also returns successfully

View file

@ -43,6 +43,9 @@ func TestStdErrors(t *testing.T) {
func(r *result.Invoke, err error) (interface{}, error) { func(r *result.Invoke, err error) (interface{}, error) {
return Uint256(r, err) return Uint256(r, err)
}, },
func(r *result.Invoke, err error) (interface{}, error) {
return PublicKey(r, err)
},
func(r *result.Invoke, err error) (interface{}, error) { func(r *result.Invoke, err error) (interface{}, error) {
_, _, err = SessionIterator(r, err) _, _, err = SessionIterator(r, err)
return nil, err return nil, err
@ -189,6 +192,18 @@ func TestUint256(t *testing.T) {
require.Equal(t, util.Uint256{1, 2, 3}, u) require.Equal(t, util.Uint256{1, 2, 3}, u)
} }
func TestPublicKey(t *testing.T) {
k, err := keys.NewPrivateKey()
require.NoError(t, err)
_, err = PublicKey(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make(util.Uint160{1, 2, 3}.BytesBE())}}, nil)
require.Error(t, err)
pk, err := PublicKey(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make(k.PublicKey().Bytes())}}, nil)
require.NoError(t, err)
require.Equal(t, k.PublicKey(), pk)
}
func TestSessionIterator(t *testing.T) { func TestSessionIterator(t *testing.T) {
_, _, err := SessionIterator(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make(42)}}, nil) _, _, err := SessionIterator(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make(42)}}, nil)
require.Error(t, err) require.Error(t, err)