Ejemplo n.º 1
0
func checkPubkey(config *api.ApiConfig, addrHash objects.Hash) []byte {

	// First check local DB
	detail, err := localdb.GetAddressDetail(addrHash)
	if err != nil {
		// If not in database, won't be able to decrypt anyway!
		return nil
	}
	if len(detail.Pubkey) > 0 {
		if db.Contains(addrHash) != db.PUBKEY {
			enc := new(objects.EncryptedPubkey)

			enc.IV, enc.Payload, _ = encryption.SymmetricEncrypt(detail.Address, string(detail.Pubkey))
			enc.AddrHash = objects.MakeHash(detail.Address)

			config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, enc)
		}
		return detail.Pubkey
	}

	// If not there, check local database
	if db.Contains(addrHash) == db.PUBKEY {
		enc := db.GetPubkey(config.Log, addrHash)

		pubkey := encryption.SymmetricDecrypt(enc.IV, detail.Address, enc.Payload)
		pubkey = pubkey[:65]

		// Check public Key
		x, y := encryption.UnmarshalPubkey(pubkey)
		if x == nil {
			config.Log <- "Decrypted Public Key Invalid"
			return nil
		}

		address2 := encryption.GetAddress(config.Log, x, y)
		if string(detail.Address) != string(address2) {
			config.Log <- "Decrypted Public Key doesn't match provided address!"
			return nil
		}

		detail.Pubkey = pubkey
		err := localdb.AddUpdateAddress(detail)
		if err != nil {
			config.Log <- "Error adding pubkey to local database!"
			return nil
		}

		return pubkey
	}

	// If not there, send a pubkey request
	config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY_REQUEST, objects.BROADCAST, &addrHash)
	return nil
}
Ejemplo n.º 2
0
func TestPubkey(t *testing.T) {
	p := new(EncryptedPubkey)
	var err error

	address := make([]byte, 25, 25)
	pubkey := [65]byte{'a'}
	p.AddrHash = MakeHash(address)
	p.IV, p.Payload, err = encryption.SymmetricEncrypt(address, string(pubkey[:]))
	if err != nil {
		fmt.Println("Could not encrypt pubkey: ", err)
		t.FailNow()
	}

	pBytes := p.GetBytes()
	if len(pBytes) != 144 {
		fmt.Println("Incorrect length for pubkey: ", pBytes)
		t.FailNow()
	}

	pubkey2 := new(EncryptedPubkey)
	err = pubkey2.FromBytes(pBytes)
	if err != nil {
		fmt.Println("Error decoding pubkey: ", err)
		t.Fail()
	}
	if string(pubkey2.AddrHash.GetBytes()) != string(p.AddrHash.GetBytes()) {
		fmt.Println("Incorrect Address Hash: ", pubkey2.AddrHash)
		t.FailNow()
	}

	pubkeyTest := encryption.SymmetricDecrypt(pubkey2.IV, address, pubkey2.Payload)
	if string(pubkeyTest[:65]) != string(pubkey[:]) {
		fmt.Println("Incorrect public key decryption: ", pubkeyTest)
		t.Fail()
	}
}