Skip to content

Commit

Permalink
Merge pull request #69 from make-software/release/v1.4.3
Browse files Browse the repository at this point in the history
Release/v1.4.3
  • Loading branch information
koltsov-iv authored Nov 1, 2023
2 parents 8041afc + 6229202 commit 1f295b5
Show file tree
Hide file tree
Showing 19 changed files with 261 additions and 66 deletions.
2 changes: 1 addition & 1 deletion rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type PutDeployResult struct {

type SpeculativeExecResult struct {
ApiVersion string `json:"api_version"`
DeployHash key.Hash `json:"deploy_hash"`
BlockHash key.Hash `json:"block_hash"`
ExecutionResult types.ExecutionResultStatus `json:"execution_result"`
}

Expand Down
4 changes: 2 additions & 2 deletions sse/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ type (
EraID uint64 `json:"era_id"`
ExecutionEffect types.Effect `json:"execution_effect"`
// Todo: not sure, didn't found example to test
Operations []types.Operation `json:"operations"`
Operations *[]types.Operation `json:"operations,omitempty"`
// Todo: not sure, didn't found example to test
Transform types.TransformKey `json:"transform"`
Transform *types.TransformKey `json:"transform,omitempty"`
}
StepEvent struct {
Step StepPayload `json:"step"`
Expand Down
6 changes: 6 additions & 0 deletions tests/data/transform/AddUInt512.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"key": "balance-cac81394a4906c62a999c4b3c24d886c452b0a9ed39419e6e8f861d72ee59303",
"transform": {
"AddUInt512": "100000000"
}
}
14 changes: 14 additions & 0 deletions tests/data/transform/WriteDeployInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"key": "deploy-e379d531188e90892ca07dba8469def6f9a45e627717b2a3ae4a8ec440c9f001",
"transform": {
"WriteDeployInfo": {
"deploy_hash": "e379d531188e90892ca07dba8469def6f9a45e627717b2a3ae4a8ec440c9f001",
"transfers": [
"transfer-78c4663dd4b83fa35d031e8173dccb53431520adf559a3853892530946fcb777"
],
"from": "account-hash-e15156d73d79f92b13aa147552a01ef2a3d677134f13961b51033ffd9fba481c",
"source": "uref-012e4506440c6bf5427d3d76ad20a1bdefc6bf5d79cee3d28bfaae36603f1133-007",
"gas": "100000000"
}
}
}
3 changes: 1 addition & 2 deletions tests/rpc/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -146,7 +145,7 @@ func Test_DefaultClient_QueryGlobalStateByBlock_GetWithdraw(t *testing.T) {
res, err := client.QueryGlobalStateByBlockHash(context.Background(), blockHash, withdrawKey, nil)
require.NoError(t, err)
assert.NotEmpty(t, res.BlockHeader.BodyHash)
assert.Equal(t, "500468846906", strconv.Itoa(int(res.StoredValue.Withdraw[0].Amount)))
assert.Equal(t, "500468846906", res.StoredValue.Withdraw[0].Amount.String())
}

func Test_DefaultClient_QueryGlobalStateByStateRoot_GetAccount(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions tests/sse/events_parsing_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sse

import (
"encoding/json"
"os"
"testing"

Expand Down Expand Up @@ -97,3 +98,15 @@ func Test_RawEvent_ParseAsStepEvent(t *testing.T) {
require.NoError(t, err)
assert.False(t, res.Step.ExecutionEffect.Transforms[0].Transform.IsWriteTransfer())
}

func Test_RawEvent_ParseAndMarshalStepEvent(t *testing.T) {
data, err := os.ReadFile("../data/sse/step_event.json")
require.NoError(t, err)
rawEvent := sse.RawEvent{
Data: data,
}
res, err := rawEvent.ParseAsStepEvent()
require.NoError(t, err)
_, err = json.Marshal(res)
assert.NoError(t, err)
}
20 changes: 20 additions & 0 deletions tests/types/keypair/private_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ func Test_SECPKey_From_PemFile(t *testing.T) {
assert.Equal(t, "0203c90c0ee375abc85da81a982507d1f8258a380af2058b63c37abdb9c7045940f4", privateKeyData.PublicKey().ToHex())
}

func Test_SECPKey_ToPemFile(t *testing.T) {
privateKeyData, err := keypair.NewPrivateKeySECP256K1("../../data/keys/account_test_SECP_secret_key.pem")
require.NoError(t, err)
data, err := privateKeyData.ToPem()
require.NoError(t, err)
privateKeyData2, err := keypair.NewPrivateKeyFromPEM(data, keypair.SECP256K1)
require.NoError(t, err)
assert.Equal(t, privateKeyData.PublicKey().Bytes(), privateKeyData2.PublicKey().Bytes())
}

func Test_ED25519_PrivateKey_ToPemFile(t *testing.T) {
privateKeyData, err := keypair.NewPrivateKeyED25518("../../data/keys/account_test_ED25519_secret_key.pem")
require.NoError(t, err)
data, err := privateKeyData.ToPem()
require.NoError(t, err)
privateKeyData2, err := keypair.NewPrivateKeyFromPEM(data, keypair.ED25519)
require.NoError(t, err)
assert.Equal(t, privateKeyData.PublicKey().Bytes(), privateKeyData2.PublicKey().Bytes())
}

func Test_SECPKey_CreateAndValidateSignature(t *testing.T) {
secretMessage := []byte("Enigmatic Shadows Concealing Ancient Whispers")
privateKeyData, err := keypair.GeneratePrivateKey(keypair.SECP256K1)
Expand Down
42 changes: 42 additions & 0 deletions tests/types/transform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/types"
)

func Test_Transform_AddUInt512(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/AddUInt512.json")
require.NoError(t, err)
var transformKey types.TransformKey

err = json.Unmarshal(fixture, &transformKey)
require.NoError(t, err)

val, err := transformKey.Transform.ParseAsUInt512()
require.NoError(t, err)

assert.True(t, transformKey.Transform.IsAddUint512())
assert.EqualValues(t, 100000000, val.Value().Int64())
}

func Test_Transform_WriteDeployInfo(t *testing.T) {
fixute, err := os.ReadFile("../data/transform/WriteDeployInfo.json")
require.NoError(t, err)
var transformKey types.TransformKey

err = json.Unmarshal(fixute, &transformKey)
require.NoError(t, err)

val, err := transformKey.Transform.ParseAsWriteDeployInfo()
require.NoError(t, err)

assert.True(t, transformKey.Transform.IsWriteDeployInfo())
assert.EqualValues(t, 1, len(val.Transfers))
}
9 changes: 5 additions & 4 deletions types/bid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"github.com/make-software/casper-go-sdk/types/clvalue"
"github.com/make-software/casper-go-sdk/types/key"
"github.com/make-software/casper-go-sdk/types/keypair"
)
Expand All @@ -14,7 +15,7 @@ type baseBid struct {
// `true` if validator has been "evicted"
Inactive bool `json:"inactive"`
// The amount of tokens staked by a validator (not including delegators).
StakedAmount uint64 `json:"staked_amount,string"`
StakedAmount clvalue.UInt512 `json:"staked_amount"`
}

// Bid is an entry stored in the Global state and representing a bid.
Expand Down Expand Up @@ -43,7 +44,7 @@ type baseDelegator struct {
// The purse that was used for delegating.
BondingPurse key.URef `json:"bonding_purse"`
// Amount of Casper token (in motes) delegated
StakedAmount uint64 `json:"staked_amount,string"`
StakedAmount clvalue.UInt512 `json:"staked_amount"`
}

// Delegator is associated with the given validator.
Expand All @@ -68,6 +69,6 @@ type AuctionDelegators struct {

// VestingSchedule for a genesis validator.
type VestingSchedule struct {
InitialReleaseTimestampMillis uint64 `json:"initial_release_timestamp_millis"`
LockedAmounts []uint64 `json:"locked_amounts"`
InitialReleaseTimestampMillis uint64 `json:"initial_release_timestamp_millis"`
LockedAmounts []clvalue.UInt512 `json:"locked_amounts"`
}
33 changes: 26 additions & 7 deletions types/clvalue/uint512.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package clvalue
import (
"bytes"
"encoding/json"
"fmt"
"math/big"

"github.com/make-software/casper-go-sdk/types/clvalue/cltype"
)

type UInt512 struct {
val *big.Int
val *big.Int
isStringFmt bool
}

func (v *UInt512) Bytes() []byte {
Expand All @@ -24,18 +26,35 @@ func (v *UInt512) Value() *big.Int {
return v.val
}

func (v *UInt512) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Value().String())
func (v UInt512) MarshalJSON() ([]byte, error) {
if v.isStringFmt {
return json.Marshal(v.String())
}
return []byte(v.String()), nil
}

func (v *UInt512) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
var num json.Number
err := json.Unmarshal(b, &num)
if err != nil {
return err
}
v.val = &big.Int{}
v.val.SetString(val, 10)

// Convert json.Number to string
s := num.String()

// Check if the original data was a quoted string
if b[0] == '"' {
v.isStringFmt = true
}

v.val = new(big.Int)
val, ok := v.val.SetString(s, 10)
if !ok {
return fmt.Errorf("invalid integer string: %s", s)
}
v.val = val

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion types/era_end.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"github.com/make-software/casper-go-sdk/types/clvalue"
"github.com/make-software/casper-go-sdk/types/keypair"
)

Expand All @@ -22,5 +23,5 @@ type EraReport struct {

type Reward struct {
Validator keypair.PublicKey `json:"validator"`
Amount uint64 `json:"amount"`
Amount clvalue.UInt512 `json:"amount"`
}
43 changes: 43 additions & 0 deletions types/keypair/ed25519/pem_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ed25519

import (
"crypto/ed25519"
"encoding/pem"
)

const PemFramePrivateKeyPrefixSize = 16

func PrivateKeyToPem(priv ed25519.PrivateKey) ([]byte, error) {
// Extract the seed (first 32 bytes of the private key)
seed := priv[:32]

// Create the prefix for the seed
prefix := make([]byte, PemFramePrivateKeyPrefixSize)

// Combine the prefix and the seed to match the original format
fullKey := append(prefix, seed...)

return pem.EncodeToMemory(
&pem.Block{
Type: "PRIVATE KEY",
Bytes: fullKey,
},
), nil
}

func NewPrivateKeyFromPEM(content []byte) (PrivateKey, error) {
block, _ := pem.Decode(content)
// Trim PEM prefix
data := block.Bytes[PemFramePrivateKeyPrefixSize:]
// Use only last 32 bytes of private Key
privateKeySize := len(data)
if privateKeySize > 32 {
data = data[privateKeySize%32:]
}

privateEdDSA := ed25519.NewKeyFromSeed(data)

return PrivateKey{
key: privateEdDSA,
}, nil
}
35 changes: 4 additions & 31 deletions types/keypair/ed25519/private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package ed25519

import (
"crypto/ed25519"
"encoding/pem"
"errors"
"os"
)

const PemFramePrivateKeyPrefixSize = 16

type PrivateKey struct {
key ed25519.PrivateKey
}
Expand All @@ -21,6 +16,10 @@ func (k PrivateKey) Sign(mes []byte) ([]byte, error) {
return ed25519.Sign(k.key, mes), nil
}

func (k PrivateKey) ToPem() ([]byte, error) {
return PrivateKeyToPem(k.key)
}

func GeneratePrivateKey() (PrivateKey, error) {
_, priv, err := ed25519.GenerateKey(nil)
if err != nil {
Expand All @@ -30,29 +29,3 @@ func GeneratePrivateKey() (PrivateKey, error) {
key: priv,
}, nil
}

func NewPrivateKeyFromPEMFile(path string) (PrivateKey, error) {
content, err := os.ReadFile(path)
if err != nil {
return PrivateKey{}, errors.New("can't read file")
}

return NewPrivateKeyFromPEM(content)
}

func NewPrivateKeyFromPEM(content []byte) (PrivateKey, error) {
block, _ := pem.Decode(content)
// Trim PEM prefix
data := block.Bytes[PemFramePrivateKeyPrefixSize:]
// Use only last 32 bytes of private Key
privateKeySize := len(data)
if privateKeySize > 32 {
data = data[privateKeySize%32:]
}

privateEdDSA := ed25519.NewKeyFromSeed(data)

return PrivateKey{
key: privateEdDSA,
}, nil
}
Loading

0 comments on commit 1f295b5

Please sign in to comment.