Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Removed optional init for IBAN(address) #137
Browse files Browse the repository at this point in the history
Added IBAN.check(address) to check if address is IBAN standard compatible
  • Loading branch information
v57 committed Oct 23, 2018
1 parent 0d5e44f commit 54cf23b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
19 changes: 12 additions & 7 deletions web3swift/KeystoreManager/IBAN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,11 @@ public struct IBAN {
guard IBAN.isValidIBANaddress(matched) else { return nil }
iban = matched
}

public init?(_ address: EthereumAddress) {
let addressString = address.address.lowercased().withoutHex
guard let bigNumber = BigUInt(addressString, radix: 16) else { return nil }
let base36EncodedString = String(bigNumber, radix: 36)
guard base36EncodedString.count <= 30 else { return nil }
let padded = base36EncodedString.leftPadding(toLength: 30, withPad: "0")
public static func check(_ address: EthereumAddress) -> Bool {
return address.addressData.base36.count <= 30
}
public init(_ address: EthereumAddress) {
let padded = address.addressData.base36.leftPadding(toLength: 30, withPad: "0")
let prefix = "XE"
let remainder = IBAN.calculateChecksumMod97(IBAN.decodeToInts(prefix + "00" + padded))
let checkDigits = "0" + String(98 - remainder)
Expand All @@ -135,3 +133,10 @@ public struct IBAN {
iban = fullIban.uppercased()
}
}

private extension Data {
var base36: String {
let bigNumber = BigUInt(self)
return String(bigNumber, radix: 36)
}
}
13 changes: 9 additions & 4 deletions web3swiftTests/MainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,24 @@ class Tests: XCTestCase {
XCTAssert(pres.count == 1)
}

func testIBANcreation() {
func testIBANCreation() {
let iban = "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS"
let native = Web3.Utils.Iban(iban)
let native = IBAN(iban)
XCTAssert(native != nil)
let expectedAddress = "0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8"
let createdAddress = native?.toEthereumAddress()?.address
XCTAssert(createdAddress == expectedAddress)

let address = EthereumAddress("0x03c5496aee77c1ba1f0854206a26dda82a81d6d8")
let fromAddress = Web3.Utils.Iban(address)
let ibn = fromAddress?.iban
let fromAddress = IBAN(address)
let ibn = fromAddress.iban
XCTAssert(ibn == "XE83FUTTUNPK7WZJSGGCWVEBARQWQ8YML4")
}

func testIBANCheckAddress() {
XCTAssertFalse(IBAN.check("0x9e87448bff240dace7cea2e90670b8d6c2c73a6e"))
XCTAssertTrue(IBAN.check("0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8"))
}

func testGenericRPCresponse() {
let hex = "0x1"
Expand Down

0 comments on commit 54cf23b

Please sign in to comment.