func (service *EMPService) CreateAddress(r *http.Request, args *NilParam, reply *objects.AddressDetail) error { if !basicAuth(service.Config, r) { service.Config.Log <- fmt.Sprintf("Unauthorized RPC Request from: %s", r.RemoteAddr) return errors.New("Unauthorized") } // Create Address priv, x, y := encryption.CreateKey(service.Config.Log) reply.Privkey = priv if x == nil { return errors.New("Key Pair Generation Error") } reply.Pubkey = encryption.MarshalPubkey(x, y) reply.IsRegistered = true reply.Address = encryption.GetAddress(service.Config.Log, x, y) if reply.Address == nil { return errors.New("Could not create address, function returned nil.") } reply.String = encryption.AddressToString(reply.Address) // Add Address to Database err := localdb.AddUpdateAddress(reply) if err != nil { service.Config.Log <- fmt.Sprintf("Error Adding Address: ", err) return err } // Send Pubkey to Network encPub := new(objects.EncryptedPubkey) encPub.AddrHash = objects.MakeHash(reply.Address) encPub.IV, encPub.Payload, err = encryption.SymmetricEncrypt(reply.Address, string(reply.Pubkey)) if err != nil { service.Config.Log <- fmt.Sprintf("Error Encrypting Pubkey: ", err) return nil } // Record Pubkey for Network service.Config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, encPub) return nil }
func GetAddressDetail(addrHash objects.Hash) (*objects.AddressDetail, error) { localMutex.Lock() defer localMutex.Unlock() if Contains(addrHash) != ADDRESS { return nil, errors.New("Address not found!") } ret := new(objects.AddressDetail) s, err := LocalDB.Query("SELECT address, registered, pubkey, privkey, label, subscribed, encprivkey FROM addressbook WHERE hash=?", addrHash.GetBytes()) if err == nil { s.Scan(&ret.Address, &ret.IsRegistered, &ret.Pubkey, &ret.Privkey, &ret.Label, &ret.IsSubscribed, &ret.EncPrivkey) ret.String = encryption.AddressToString(ret.Address) return ret, nil } return nil, err }
func AddUpdateAddress(address *objects.AddressDetail) error { localMutex.Lock() defer localMutex.Unlock() var err error if address.Address == nil { address.Address = encryption.StringToAddress(address.String) } if address.Address == nil { return errors.New("Invalid Address!") } addrHash := objects.MakeHash(address.Address) if Contains(addrHash) == ADDRESS { // Exists in message database, update pubkey, privkey, and registration err = LocalDB.Exec("UPDATE addressbook SET registered=?, subscribed=?, label=? WHERE hash=?", address.IsRegistered, address.IsSubscribed, address.Label, addrHash.GetBytes()) if err != nil { return err } if address.Pubkey != nil { err = LocalDB.Exec("UPDATE addressbook SET pubkey=? WHERE hash=?", address.Pubkey, addrHash.GetBytes()) if err != nil { return err } } if address.Privkey != nil { err = LocalDB.Exec("UPDATE addressbook SET privkey=? WHERE hash=?", address.Privkey, addrHash.GetBytes()) if err != nil { return err } } if address.EncPrivkey != nil { err = LocalDB.Exec("UPDATE addressbook SET encprivkey=? WHERE hash=?", address.EncPrivkey, addrHash.GetBytes()) if err != nil { return err } } } else { // Doesn't exist yet, insert it! err = LocalDB.Exec("INSERT INTO addressbook VALUES (?, ?, ?, ?, ?, ?, ?, ?)", addrHash.GetBytes(), address.Address, address.IsRegistered, address.Pubkey, address.Privkey, address.Label, address.IsSubscribed, address.EncPrivkey) if err != nil { return err } Add(addrHash, ADDRESS) } return nil }