示例#1
0
文件: datainput.go 项目: eXcomm/urs
// ReadKeyRing reads a key ring of public keys from a file in JSON object format,
// and also inserts the pubkey of a keypair if it's not already present (handles
// bug in URS implementation).
func ReadKeyRing(filename string, kp *ecdsa.PrivateKey) (*PublicKeyRing, error) {
	// Load the file
	f, err := ioutil.ReadFile(filename)

	if err != nil {
		fError := errors.New("os.Open: Couldn't open keyring file.")
		return nil, fError
	}

	// Unmarshall the loaded file into a map
	var keyMap = make(map[string]string)

	err = json.Unmarshal(f, &keyMap)
	if err != nil {
		str := fmt.Sprintf("json error: Couldn't unmarshall keyring file.", err)
		jsonError := errors.New(str)
		return nil, jsonError
	}

	kr := NewPublicKeyRing(uint(len(keyMap)))

	// Stick the pubkeys into the keyring as long as it doesn't belong to the
	// keypair given.
	for i := 0; i < len(keyMap); i++ {
		pkBytes, errDecode := hex.DecodeString(keyMap[strconv.Itoa(i)])
		if errDecode != nil {
			decodeError := errors.New("decode error: Couldn't decode hex.")
			return nil, decodeError
		}

		pubkey, errParse := btcec.ParsePubKey(pkBytes, btcec.S256())
		if errParse != nil {
			return nil, errParse
		}

		ecdsaPubkey := ecdsa.PublicKey{pubkey.Curve, pubkey.X, pubkey.Y}

		if kp == nil || !CmpPubKey(&kp.PublicKey, &ecdsaPubkey) {
			kr.Add(ecdsaPubkey)
		} else {
			kr.Add(kp.PublicKey)
		}
	}

	// Stick the keypair in if it's missing.
	if kp != nil && !kr.keyInKeyRing(&kp.PublicKey) {
		kr.Add(kp.PublicKey)
	}

	return kr, nil
}
示例#2
0
文件: datainput.go 项目: eXcomm/urs
// ReadKeyPair reads an ECDSA keypair a file in JSON object format.
// Example JSON input:
//  {
//    "privkey": "..."
//  }
// It also checks if a pubkey is in the keyring and, if not, appends it to the
// keyring.
func ReadKeyPair(filename string) (*ecdsa.PrivateKey, error) {
	// Load the file
	f, err := ioutil.ReadFile(filename)

	if err != nil {
		fError := errors.New("os.Open: Couldn't open keypair file.")
		return nil, fError
	}

	// Unmarshall the loaded file into a map.
	var keyMap = make(map[string]string)
	var pubkey *ecdsa.PublicKey
	var privkey *ecdsa.PrivateKey

	err = json.Unmarshal(f, &keyMap)
	if err != nil {
		jsonError := errors.New("json error: Couldn't unmarshall keypair file.")
		return nil, jsonError
	}

	privBytes, errDecode := hex.DecodeString(keyMap["privkey"])
	if errDecode != nil {
		decodeError := errors.New("decode error: Couldn't decode hex for privkey.")
		return nil, decodeError
	}

	// PrivKeyFromBytes doesn't return an error, so this could possibly be ugly.
	privkeyBtcec, _ := btcec.PrivKeyFromBytes(btcec.S256(), privBytes)

	pubBytes, errDecode := hex.DecodeString(keyMap["pubkey"])
	if errDecode != nil {
		decodeError := errors.New("decode error: Couldn't decode hex for privkey.")
		return nil, decodeError
	}

	pubkeyBtcec, errParse := btcec.ParsePubKey(pubBytes, btcec.S256())
	if errParse != nil {
		return nil, errParse
	}

	// Assign the things to return
	pubkey = &ecdsa.PublicKey{pubkeyBtcec.Curve,
		pubkeyBtcec.X,
		pubkeyBtcec.Y}

	privkey = &ecdsa.PrivateKey{*pubkey, privkeyBtcec.D}

	return privkey, nil
}