immutability-io / vault-ethereum

A plugin that turns Vault into an Ethereum wallet.
244 stars 66 forks source link

remove file sign, add signing a hex-encoded string #63

Closed cypherhat closed 5 years ago

cypherhat commented 5 years ago

Description

Remove signing of a file. Added the ability to sign a hex-encoded string.

Motivation and Context

https://github.com/immutability-io/vault-ethereum/issues/56

How Has This Been Tested?

Local sandbox testing. Used this code to encode/decode and compare:

package main

import (
    "encoding/hex"
    "flag"
    "fmt"
    "io/ioutil"
    "os"
)

// Encode will encode a raw key or seed
func Encode(src []byte) ([]byte, error) {
    buf := make([]byte, hex.EncodedLen(len(src)))
    hex.Encode(buf, src)

    return buf[:], nil
}

// Decode will decode the hex
func Decode(src []byte) ([]byte, error) {
    raw := make([]byte, hex.EncodedLen(len(src)))
    n, err := hex.Decode(raw, src)
    if err != nil {
        return nil, err
    }
    raw = raw[:n]
    return raw[:], nil
}

// ReadFile returns the contents of a file
func ReadFile(path string) ([]byte, error) {
    var contents []byte
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    contents, err = ioutil.ReadFile(path)
    if err != nil {
        return nil, err
    }
    return contents, nil

}

func main() {
    filePtr := flag.String("file", "./data", "File containing data to encode")
    dataPtr := flag.String("data", "", "Data to decode")
    decodePtr := flag.Bool("decode", false, "a bool")
    flag.Parse()

    if *decodePtr {
        decoded, err := Decode([]byte(*dataPtr))
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Printf("DECODED = %v\n", decoded)
        }
    } else {
        data, err := ReadFile(*filePtr)
        if err != nil {
            fmt.Println(err)
        } else {
            encoded, err := Encode([]byte(data))
            if err != nil {
                fmt.Println(err)
            } else {
                fmt.Printf("ENCODED = %s\n", encoded)
            }
        }
    }
}

Screenshots (if appropriate):

Types of changes

Checklist: