Skip to content
Snippets Groups Projects
key_client.go 2.72 KiB
Newer Older
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Silas Davis's avatar
Silas Davis committed

	"github.com/hyperledger/burrow/logging"
	"github.com/hyperledger/burrow/logging/loggers"
Benjamin Bollen's avatar
Benjamin Bollen committed
type KeyClient interface {
	// Sign needs to return the signature bytes for given message to sign
	// and the address to sign it with.
	Sign(signBytesString string, signAddress []byte) (signature []byte, err error)
	// PublicKey needs to return the public key associated with a given address
	PublicKey(address []byte) (publicKey []byte, err error)
Silas Davis's avatar
Silas Davis committed
// NOTE [ben] Compiler check to ensure monaxKeyClient successfully implements
// burrow/keys.KeyClient
var _ KeyClient = (*monaxKeyClient)(nil)
Silas Davis's avatar
Silas Davis committed
type monaxKeyClient struct {
Silas Davis's avatar
Silas Davis committed
	logger    loggers.InfoTraceLogger
Silas Davis's avatar
Silas Davis committed
// monaxKeyClient.New returns a new monax-keys client for provided rpc location
// Monax-keys connects over http request-responses
func NewBurrowKeyClient(rpcString string, logger loggers.InfoTraceLogger) *monaxKeyClient {
	return &monaxKeyClient{
Silas Davis's avatar
Silas Davis committed
		logger:    logging.WithScope(logger, "BurrowKeyClient"),
Silas Davis's avatar
Silas Davis committed
// Monax-keys client Sign requests the signature from BurrowKeysClient over rpc for the given
// bytes to be signed and the address to sign them with.
Silas Davis's avatar
Silas Davis committed
func (monaxKeys *monaxKeyClient) Sign(signBytesString string, signAddress []byte) (signature []byte, err error) {
		"msg":  signBytesString,
		"hash": signBytesString, // TODO:[ben] backwards compatibility
		"addr": fmt.Sprintf("%X", signAddress),
Silas Davis's avatar
Silas Davis committed
	sigS, err := RequestResponse(monaxKeys.rpcString, "sign", args, monaxKeys.logger)
	if err != nil {
		return
	}
	sigBytes, err := hex.DecodeString(sigS)
	if err != nil {
		return
	}
Silas Davis's avatar
Silas Davis committed
// Monax-keys client PublicKey requests the public key associated with an address from
// the monax-keys server.
func (monaxKeys *monaxKeyClient) PublicKey(address []byte) (publicKey []byte, err error) {
		"addr": fmt.Sprintf("%X", address),
Silas Davis's avatar
Silas Davis committed
	pubS, err := RequestResponse(monaxKeys.rpcString, "pub", args, monaxKeys.logger)
Benjamin Bollen's avatar
Benjamin Bollen committed
	// TODO: [ben] assert that received public key results in
	// address
	return hex.DecodeString(pubS)