// Rand returns a random address from the addresslist. func (adl AddressList) Rand() *Address { if adl == nil || len(adl) == 0 { return nil } rand.Seed(times.NowNano()) return &adl[int(rand.Int31())%len(adl)] }
// FetchMessage fetches a message from the accountserver. func FetchMessage(privkey *[ed25519.PrivateKeySize]byte, messageID []byte, server string, cacert []byte) ([]byte, error) { var authtoken []byte var err error var message []byte lastcounter := uint64(times.NowNano()) pubkey := splitKey(privkey) i := 3 // This should skip error and a collision, but stop if it's an ongoing parallel access CallLoop: for { if authtoken == nil { authtoken = walletauth.CreateToken(pubkey, privkey, lastcounter+1) } message, lastcounter, err = fetchmessage(messageID, authtoken, server, cacert) if err == walletauth.ErrReplay { authtoken = nil if i > 0 { i-- continue CallLoop } } break CallLoop } return message, err }
// DeleteAccount deletes the account of privkey on server. func DeleteAccount(privkey *[ed25519.PrivateKeySize]byte, server string, cacert []byte) (err error) { var authtoken []byte var result bool lastcounter := uint64(times.NowNano()) pubkey := splitKey(privkey) i := 3 // This should skip error and a collision, but stop if it's an ongoing parallel access CallLoop: for { if authtoken == nil { authtoken = walletauth.CreateToken(pubkey, privkey, lastcounter+1) } result, lastcounter, err = delAccount(authtoken, server, cacert) if err == walletauth.ErrReplay { authtoken = nil if i > 0 { i-- continue CallLoop } } break CallLoop } if err != nil { return err } if result { return nil } return ErrNoMatch }
// ListMessages gets the messages for account identified by privkey. func ListMessages(privkey *[ed25519.PrivateKeySize]byte, lastMessageTime int64, server string, cacert []byte) (messages []MessageMeta, err error) { var authtoken []byte lastcounter := uint64(times.NowNano()) pubkey := splitKey(privkey) i := 3 // This should skip error and a collision, but stop if it's an ongoing parallel access CallLoop: for { if authtoken == nil { authtoken = walletauth.CreateToken(pubkey, privkey, lastcounter+1) } messages, lastcounter, err = listMessages(authtoken, lastMessageTime, server, cacert) if err == walletauth.ErrReplay { authtoken = nil if i > 0 { i-- continue CallLoop } } break CallLoop } if err != nil { return nil, err } return messages, nil }
// PayAccount makes a pay call to server (or selects a new one) to create or // extend an account identified by privkey. func PayAccount(privkey *[ed25519.PrivateKeySize]byte, paytoken []byte, serverKnown string, cacert []byte) (server string, err error) { var authtoken []byte lastcounter := uint64(times.NowNano()) pubkey := splitKey(privkey) i := 3 // This should skip error and a collision, but stop if it's an ongoing parallel access CallLoop: for { if authtoken == nil { authtoken = walletauth.CreateToken(pubkey, privkey, lastcounter+1) } server, lastcounter, err = payAccount(authtoken, paytoken, serverKnown, cacert) if err == walletauth.ErrReplay { authtoken = nil if i > 0 { i-- continue CallLoop } } break CallLoop } return server, err }
func getLockID() int64 { mathrand.Seed(times.NowNano()) lockID := mathrand.Int31() return int64(lockID) }