Skip to content

Commit

Permalink
Merge pull request #179 from multiversx/merge_rc/v1.6.0_in_pubkey_con…
Browse files Browse the repository at this point in the history
…v_8mar

Merge rc/v1.6.0 in pubkey conv 8mar
  • Loading branch information
schimih authored Mar 8, 2023
2 parents 5ec8f51 + 3cedc44 commit 5262fb0
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 123 deletions.
6 changes: 6 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const AllShardId = uint32(0xFFFFFFF0)
// MegabyteSize represents the size in bytes of a megabyte
const MegabyteSize = 1024 * 1024

// MaxMachineIDLen is the maximum machine ID length
const MaxMachineIDLen = 10

// BuiltInFunctionClaimDeveloperRewards is the key for the claim developer rewards built-in function
const BuiltInFunctionClaimDeveloperRewards = "ClaimDeveloperRewards"

Expand Down Expand Up @@ -257,3 +260,6 @@ const GasRefundForRelayerMessage = "gas refund for relayer"

// DefaultAddressPrefix is the default hrp of MultiversX/Elrond
const DefaultAddressPrefix = "erd"

// TopicRequestSuffix represents the topic name suffix for requests
const TopicRequestSuffix = "_REQUEST"
59 changes: 55 additions & 4 deletions core/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"bytes"
"encoding/json"
"encoding/pem"
"fmt"
Expand All @@ -13,6 +14,8 @@ import (
"github.com/pelletier/go-toml"
)

const pemPkHeader = "PRIVATE KEY for "

// ArgCreateFileArgument will hold the arguments for a new file creation method call
type ArgCreateFileArgument struct {
Directory string
Expand Down Expand Up @@ -162,16 +165,64 @@ func LoadSkPkFromPemFile(relativePath string, skIndex int) ([]byte, string, erro
}

blockType := blkRecovered.Type
header := "PRIVATE KEY for "
if strings.Index(blockType, header) != 0 {
return nil, "", fmt.Errorf("%w missing '%s' in block type", ErrPemFileIsInvalid, header)

if strings.Index(blockType, pemPkHeader) != 0 {
return nil, "", fmt.Errorf("%w missing '%s' in block type", ErrPemFileIsInvalid, pemPkHeader)
}

blockTypeString := blockType[len(header):]
blockTypeString := blockType[len(pemPkHeader):]

return blkRecovered.Bytes, blockTypeString, nil
}

// LoadAllKeysFromPemFile loads all the secret keys and existing public key bytes stored in the file
func LoadAllKeysFromPemFile(relativePath string) ([][]byte, []string, error) {
file, err := OpenFile(relativePath)
if err != nil {
return nil, nil, err
}

defer func() {
_ = file.Close()
}()

buff, err := ioutil.ReadAll(file)
if err != nil {
return nil, nil, fmt.Errorf("%w while reading %s file", err, relativePath)
}
if len(buff) == 0 {
return nil, nil, fmt.Errorf("%w while reading %s file", ErrEmptyFile, relativePath)
}

var blkRecovered *pem.Block
privateKeys := make([][]byte, 0)
publicKeys := make([]string, 0)

for {
if len(buff) == 0 {
break
}

blkRecovered, buff = pem.Decode(buff)
if blkRecovered == nil {
return nil, nil, fmt.Errorf("%w while reading %s file, error decoding", ErrPemFileIsInvalid, relativePath)
}
buff = bytes.TrimSpace(buff)

blockType := blkRecovered.Type
if strings.Index(blockType, pemPkHeader) != 0 {
return nil, nil, fmt.Errorf("%w missing '%s' in block type", ErrPemFileIsInvalid, pemPkHeader)
}

blockTypeString := blockType[len(pemPkHeader):]

privateKeys = append(privateKeys, blkRecovered.Bytes)
publicKeys = append(publicKeys, blockTypeString)
}

return privateKeys, publicKeys, nil
}

// SaveSkToPemFile saves secret key bytes in the file
func SaveSkToPemFile(file *os.File, identifier string, skBytes []byte) error {
if file == nil {
Expand Down
Loading

0 comments on commit 5262fb0

Please sign in to comment.