Skip to content

Commit

Permalink
require addresses to be 90 trytes long (#102)
Browse files Browse the repository at this point in the history
* require addresses to be 90 trytes long

* adjusts readme and examples
  • Loading branch information
luca-moser authored Mar 29, 2019
1 parent c43c163 commit c276ddc
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 162 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const mwm = 14
// how many milestones back to start the random walk from
const depth = 3

// can be 90 trytes long (with checksum)
// must be 90 trytes long (with checksum)
const recipientAddress = "BBBB....."

func main() {
Expand All @@ -140,6 +140,7 @@ func main() {
// optionally define a message and tag
transfers := bundle.Transfers{
{
// must be 90 trytes long (include the checksum)
Address: recipientAddress,
Value: 80,
},
Expand All @@ -148,6 +149,7 @@ func main() {
// create inputs for the transfer
inputs := []Input{
{
// must be 90 trytes long (include the checksum)
Address: "CCCCC....",
Security: SecurityLevelMedium,
KeyIndex: 0,
Expand All @@ -158,7 +160,7 @@ func main() {
// create an address for the remainder.
// in this case we will have 20 iotas as the remainder, since we spend 100 from our input
// address and only send 80 to the recipient.
remainderAddress, err := address.GenerateAddress(seed, 1, SecurityLevelMedium)
remainderAddress, err := address.GenerateAddress(seed, 1, SecurityLevelMedium, true)
must(err)

// we don't need to set the security level or timestamp in the options because we supply
Expand Down
8 changes: 5 additions & 3 deletions api/.examples/api_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func ExampleFindTransactions() {
// o: *Balances, The object describing the result of the balance query.
// o: error, Returned for invalid addresses and internal errors.
func ExampleGetBalances() {
balances, err := iotaAPI.GetBalances(trinary.Hashes{"DJDMZD9G9VMGR9UKMEYJWYRLUDEVWTPQJXIQAAXFGMXXSCONBGCJKVQQZPXFMVHAAPAGGBMDXESTZ9999"}, 100)
balances, err := iotaAPI.GetBalances(trinary.Hashes{"LWVVGCWMYKZGMBE9GOCB9J9QALRKWGAVISAEXEOM9NVCGJCCGSNBXXGYQDNZBXBWCEM9RMFHYBCSFWE9XEHAPSXHRY"}, 100)
if err != nil {
// handle error
return
Expand Down Expand Up @@ -271,7 +271,7 @@ func ExampleStoreTransactions() {}
// o: []bool, The spent states of the addresses.
// o: error, Returned for internal errors.
func ExampleWereAddressesSpentFrom() {
spentStates, err := iotaAPI.WereAddressesSpentFrom("AETRKPXQNEK9GWM9ILSODEOZEFDDROCNKYQLWBDHWAEQJIGMSOJSETHNAMZOWDIVVMYPOPSFJRZYMDNRDQSGLFVZNY")
spentStates, err := iotaAPI.WereAddressesSpentFrom("LWVVGCWMYKZGMBE9GOCB9J9QALRKWGAVISAEXEOM9NVCGJCCGSNBXXGYQDNZBXBWCEM9RMFHYBCSFWE9XEHAPSXHRY")
if err != nil {
// handle error
return
Expand Down Expand Up @@ -415,6 +415,7 @@ func ExamplePrepareTransfers() {
// optionally define a message and tag
transfers := bundle.Transfers{
{
// must be 90 trytes long (inlcude the checksum)
Address: "ASDEF...",
Value: 80,
},
Expand All @@ -423,6 +424,7 @@ func ExamplePrepareTransfers() {
// create inputs for the transfer
inputs := []api.Input{
{
// must be 90 trytes long (inlcude the checksum)
Address: "BCEDFA...",
Security: consts.SecurityLevelMedium,
KeyIndex: 0,
Expand All @@ -433,7 +435,7 @@ func ExamplePrepareTransfers() {
// create an address for the remainder.
// in this case we will have 20 iotas as the remainder, since we spend 100 from our input
// address and only send 80 to the recipient.
remainderAddress, err := address.GenerateAddress(seed, 1, consts.SecurityLevelMedium)
remainderAddress, err := address.GenerateAddress(seed, 1, consts.SecurityLevelMedium, true)
if err != nil {
// handle error
return
Expand Down
5 changes: 4 additions & 1 deletion api/integration/find_transaction_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
. "github.com/iotaledger/iota.go/api"
_ "github.com/iotaledger/iota.go/api/integration/gocks"
. "github.com/iotaledger/iota.go/api/integration/samples"
"github.com/iotaledger/iota.go/checksum"
. "github.com/iotaledger/iota.go/consts"
. "github.com/iotaledger/iota.go/trinary"
. "github.com/onsi/ginkgo"
Expand All @@ -19,8 +20,10 @@ var _ = Describe("FindTransactionObjects()", func() {

Context("call", func() {
It("resolves to correct response", func() {
addrWithChecksum, err := checksum.AddChecksum(Bundle[0].Address, true, AddressChecksumTrytesSize)
Expect(err).ToNot(HaveOccurred())
txs, err := api.FindTransactionObjects(FindTransactionsQuery{
Addresses: Hashes{Bundle[0].Address},
Addresses: Hashes{addrWithChecksum},
})
Expect(err).ToNot(HaveOccurred())
Expect(txs[0]).To(Equal(Bundle[0]))
Expand Down
2 changes: 1 addition & 1 deletion api/integration/find_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("FindTransactions()", func() {
Context("address query", func() {

It("resolves to correct response", func() {
hashes, err := api.FindTransactions(FindTransactionsQuery{Addresses: FindTransactionsByAddresses})
hashes, err := api.FindTransactions(FindTransactionsQuery{Addresses: FindTransactionsByAddressesQuery})
Expect(err).ToNot(HaveOccurred())
Expect(hashes).To(Equal(expect))
})
Expand Down
6 changes: 3 additions & 3 deletions api/integration/get_account_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ var _ = Describe("GetAccountData()", func() {
}

accountData := AccountData{
Addresses: SampleAddresses,
Addresses: SampleAddressesWithChecksum,
Transfers: Transfers,
Inputs: []Input{
{
Address: SampleAddresses[2],
Address: SampleAddressesWithChecksum[2],
Balance: 1,
KeyIndex: 2,
Security: SecurityLevelMedium,
},
},
LatestAddress: SampleAddresses[2],
LatestAddress: SampleAddressesWithChecksum[2],
Transactions: nil, // txs addresses (which are 9s) never matched the seed's addresses
Balance: 1,
}
Expand Down
19 changes: 2 additions & 17 deletions api/integration/get_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,7 @@ var _ = Describe("GetBalances()", func() {
Context("call", func() {

It("resolves to correct response", func() {
balances, err := api.GetBalances(SampleAddresses, 100)
Expect(err).ToNot(HaveOccurred())
Expect(*balances).To(Equal(Balances{
Balances: []uint64{99, 0, 1},
Milestone: strings.Repeat("M", 81),
MilestoneIndex: 1,
}))
})

It("removes the checksum from the addresses", func() {
withChecksums := make(Hashes, len(SampleAddresses))
for i := range withChecksums {
withChecksums[i] = SampleAddresses[i] + strings.Repeat("9", 9)
}

balances, err := api.GetBalances(withChecksums, 100)
balances, err := api.GetBalances(SampleAddressesWithChecksum, 100)
Expect(err).ToNot(HaveOccurred())
Expect(*balances).To(Equal(Balances{
Balances: []uint64{99, 0, 1},
Expand All @@ -54,7 +39,7 @@ var _ = Describe("GetBalances()", func() {
})

It("returns an error for invalid threshold", func() {
_, err := api.GetBalances(Hashes{SampleAddresses[0]}, 101)
_, err := api.GetBalances(Hashes{SampleAddressesWithChecksum[0]}, 101)
Expect(errors.Cause(err)).To(Equal(ErrInvalidThreshold))
})
})
Expand Down
4 changes: 2 additions & 2 deletions api/integration/get_bundles_from_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var _ = Describe("GetBundlesFromAddresses()", func() {

Context("call", func() {
It("resolves to correct response", func() {
addresses := make(Hashes, len(SampleAddresses))
copy(addresses, SampleAddresses)
addresses := make(Hashes, len(SampleAddressesWithChecksum))
copy(addresses, SampleAddressesWithChecksum)
bndls, err := api.GetBundlesFromAddresses(addresses, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(bndls)).To(Equal(2))
Expand Down
4 changes: 2 additions & 2 deletions api/integration/get_inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ var _ = Describe("GetInputs()", func() {
var inputs = Inputs{
Inputs: []Input{
{
Address: SampleAddresses[0],
Address: SampleAddressesWithChecksum[0],
Balance: 99,
KeyIndex: 0,
Security: SecurityLevelMedium,
},
{
Address: SampleAddresses[2],
Address: SampleAddressesWithChecksum[2],
Balance: 1,
KeyIndex: 2,
Security: SecurityLevelMedium,
Expand Down
27 changes: 7 additions & 20 deletions api/integration/get_new_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,28 @@ var _ = Describe("GetNewAddress()", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 0})
Expect(err).ToNot(HaveOccurred())
// third address because previous ones are spent or have transactions
Expect(addresses[0]).To(Equal(SampleAddresses[2]))
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[2]))
})

It("resolves to correct addresses with total option", func() {
var total uint64 = 2
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 0, Total: &total})
Expect(err).ToNot(HaveOccurred())
Expect(addresses[0]).To(Equal(SampleAddresses[0]))
Expect(addresses[1]).To(Equal(SampleAddresses[1]))
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[0]))
Expect(addresses[1]).To(Equal(SampleAddressesWithChecksum[1]))
})

It("resolves to correct addresses with return all option", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 1, ReturnAll: true})
Expect(err).ToNot(HaveOccurred())
// index 1 has transactions, 2 is new
Expect(len(addresses)).To(Equal(2))
Expect(addresses[0]).To(Equal(SampleAddresses[1]))
Expect(addresses[1]).To(Equal(SampleAddresses[2]))
})

It("resolves to correct addresses with return checksum option", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 0, Checksum: true})
Expect(err).ToNot(HaveOccurred())
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[2]))
})

It("resolves to correct addresses with return checksum and total option", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 0, ReturnAll: true, Checksum: true})
Expect(err).ToNot(HaveOccurred())
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[0]))
Expect(addresses[1]).To(Equal(SampleAddressesWithChecksum[1]))
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[1]))
Expect(addresses[1]).To(Equal(SampleAddressesWithChecksum[2]))
})

It("resolves to correct addresses with return checksum and total option from different index", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 1, ReturnAll: true, Checksum: true})
It("resolves to correct addresses with total option from different index", func() {
addresses, err := api.GetNewAddress(Seed, GetNewAddressOptions{Index: 1, ReturnAll: true})
Expect(err).ToNot(HaveOccurred())
Expect(addresses[0]).To(Equal(SampleAddressesWithChecksum[1]))
})
Expand Down
1 change: 1 addition & 0 deletions api/integration/gocks/g_find_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
)

var FindTransactionsByAddressesQuery = Hashes{"VIWGTBNSFOZDBZYRUMSFGHUJYURQHNYQMYVWGQOBNONDZRFJG9VQTAHPBMTWEEMRYIMQFRAC9VYBOLJVDBPTIELAWD"}
var FindTransactionsByAddresses = Hashes{"VIWGTBNSFOZDBZYRUMSFGHUJYURQHNYQMYVWGQOBNONDZRFJG9VQTAHPBMTWEEMRYIMQFRAC9VYBOLJVD"}
var FindTransactionsByBundles = DefaultHashes()
var FindTransactionsByTags = []Trytes{strings.Repeat("A", 27), strings.Repeat("B", 27)}
Expand Down
6 changes: 3 additions & 3 deletions api/integration/is_address_used_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ var _ = Describe("IsAddressUsed()", func() {
}

It("returns true for spent address", func() {
used, err := api.IsAddressUsed(SampleAddresses[0])
used, err := api.IsAddressUsed(SampleAddressesWithChecksum[0])
Expect(err).ToNot(HaveOccurred())
Expect(used).To(BeTrue())
})

It("returns true for address with transactions", func() {
used, err := api.IsAddressUsed(SampleAddresses[1])
used, err := api.IsAddressUsed(SampleAddressesWithChecksum[1])
Expect(err).ToNot(HaveOccurred())
Expect(used).To(BeTrue())
})

It("returns false for unused address", func() {
used, err := api.IsAddressUsed(SampleAddresses[2])
used, err := api.IsAddressUsed(SampleAddressesWithChecksum[2])
Expect(err).ToNot(HaveOccurred())
Expect(used).To(BeFalse())
})
Expand Down
12 changes: 8 additions & 4 deletions api/integration/prepare_transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var _ = Describe("PrepareTransfers()", func() {

inputs := []Input{
{
Address: "PERXVBEYBJFPNEVPJNTCLWTDVOTEFWVGKVHTGKEOYRTZWYTPXGJJGZZZ9MQMHUNYDKDNUIBWINWB9JQLD",
Address: SampleAddressesWithChecksum[0],
KeyIndex: 0,
Security: 2,
Balance: 3,
},
{
Address: "VIWGTBNSFOZDBZYRUMSFGHUJYURQHNYQMYVWGQOBNONDZRFJG9VQTAHPBMTWEEMRYIMQFRAC9VYBOLJVD",
Address: SampleAddressesWithChecksum[1],
KeyIndex: 1,
Security: 2,
Balance: 4,
Expand Down Expand Up @@ -58,9 +58,13 @@ var _ = Describe("PrepareTransfers()", func() {
},
}

targetAddr, err := checksum.AddChecksum("OHXRRYM9XAOOXBLWIFWSMMDUYSRVRK9RWHPMNRFDTKUYZWENMPGHPHKBECU9HRJMAYSQM9JRAS9CTGWBN", true, AddressChecksumTrytesSize)
if err != nil {
panic(err)
}
zeroValueTransfer := bundle.Transfers{
{
Address: "OHXRRYM9XAOOXBLWIFWSMMDUYSRVRK9RWHPMNRFDTKUYZWENMPGHPHKBECU9HRJMAYSQM9JRAS9CTGWBN",
Address: targetAddr,
Value: 0,
Tag: "DJBETBPXOIKY",
Message: "K9X",
Expand All @@ -71,7 +75,7 @@ var _ = Describe("PrepareTransfers()", func() {
"K9X999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999OHXRRYM9XAOOXBLWIFWSMMDUYSRVRK9RWHPMNRFDTKUYZWENMPGHPHKBECU9HRJMAYSQM9JRAS9CTGWBN999999999999999999999999999NNCETBPXOIKY999999999999999T9XRIZD99999999999999999999VRBLQHKIUGAFFPBTZROLCDHHCOVPXCJNFBRSNZIOJCCHVZNBSKD99LUOMV9AKLWIKCGBY9UZWCBNPLWYC999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999DJBETBPXOIKY999999999999999999999999999999999999999999999999999999999999999999999",
}

remainderAddress := SampleAddresses[2]
remainderAddress := SampleAddressesWithChecksum[2]

// necessary so that the output is going to be the same no matter when it is run
timestamp := uint64(1522219924)
Expand Down
8 changes: 0 additions & 8 deletions api/integration/were_addresses_spent_from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ var _ = Describe("WereAddressesSpentFrom()", func() {

Context("call", func() {
It("resolves to correct response", func() {
spent, err := api.WereAddressesSpentFrom(SampleAddresses...)
Expect(err).ToNot(HaveOccurred())
Expect(spent[0]).To(BeTrue())
Expect(spent[1]).To(BeFalse())
Expect(spent[2]).To(BeFalse())
})

It("removes checksum from addresses", func() {
spent, err := api.WereAddressesSpentFrom(SampleAddressesWithChecksum...)
Expect(err).ToNot(HaveOccurred())
Expect(spent[0]).To(BeTrue())
Expand Down
7 changes: 3 additions & 4 deletions api/iricalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (api *API) CheckConsistency(hashes ...Hash) (bool, string, error) {

func validateFindTransactions(query *FindTransactionsQuery) error {
if err := Validate(
ValidateHashes(query.Addresses...),
ValidateAddresses(false, query.Addresses...),
ValidateHashes(query.Bundles...),
ValidateTransactionHashes(query.Approvees...),
ValidateTags(query.Tags...),
Expand Down Expand Up @@ -160,7 +160,7 @@ func (api *API) FindTransactions(query FindTransactionsQuery) (Hashes, error) {

// GetBalances fetches confirmed balances of the given addresses at the latest solid milestone.
func (api *API) GetBalances(addresses Hashes, threshold uint64, tips ...Hash) (*Balances, error) {
if err := Validate(ValidateHashes(addresses...)); err != nil {
if err := Validate(ValidateAddresses(false, addresses...)); err != nil {
return nil, err
}

Expand Down Expand Up @@ -337,8 +337,7 @@ func (api *API) StoreTransactions(trytes ...Trytes) ([]Trytes, error) {
// WereAddressesSpentFrom checks whether the given addresses were already spent.
func (api *API) WereAddressesSpentFrom(addresses ...Hash) ([]bool, error) {
if err := Validate(
ValidateNonEmptyStrings(ErrInvalidHash, addresses...),
ValidateHashes(addresses...),
ValidateAddresses(false, addresses...),
); err != nil {
return nil, err
}
Expand Down
15 changes: 0 additions & 15 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/iotaledger/iota.go/bundle"
. "github.com/iotaledger/iota.go/consts"
"github.com/iotaledger/iota.go/transaction"
. "github.com/iotaledger/iota.go/trinary"
"time"
)
Expand Down Expand Up @@ -91,8 +90,6 @@ type GetNewAddressOptions struct {
Index uint64
// The security level used for generating new addresses.
Security SecurityLevel
// Whether to append the checksum to the generated addresses.
Checksum bool
// The total amount of addresses to generate.
Total *uint64
// Whether to return all generated addresses and not just the new address.
Expand Down Expand Up @@ -199,18 +196,6 @@ type SendTransfersOptions struct {
Reference *Hash
}

type prepareTransferProps struct {
Transactions transaction.Transactions
Trytes []Trytes
Transfers bundle.Transfers
Seed Trytes
Security SecurityLevel
Inputs []Input
Timestamp uint64
RemainderAddress *Trytes
HMACKey *Trytes
}

func getPrepareTransfersDefaultOptions(options PrepareTransfersOptions) PrepareTransfersOptions {
if options.Security == 0 {
options.Security = SecurityLevelMedium
Expand Down
Loading

0 comments on commit c276ddc

Please sign in to comment.