Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitstring - fix incorrect bounds check #14

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ target 'ton-swift-example' do
pod 'TonSwift', :path => '../', :testspecs => ['Tests']

end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "11.0"
end
end
end
10 changes: 5 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PODS:
- BigInt (5.2.0)
- TonSwift (0.0.1):
- TonSwift (1.0.3):
- BigInt
- TweetNacl
- TonSwift/Tests (0.0.1):
- TonSwift/Tests (1.0.3):
- BigInt
- TweetNacl
- TweetNacl (1.0.2)
Expand All @@ -23,9 +23,9 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
BigInt: f668a80089607f521586bbe29513d708491ef2f7
TonSwift: 72bbfc1d924a4eebac87c8651afea7d2323771c6
TonSwift: 8263886e6a34c81a721cde1674cc70229a1d0d44
TweetNacl: 3abf4d1d2082b0114e7a67410e300892448951e6

PODFILE CHECKSUM: 4414c00005d2c10cd52b412b3080f767ee316ec4
PODFILE CHECKSUM: adb693e82e588cc090eb72c366a3aff89a95e709

COCOAPODS: 1.12.0
COCOAPODS: 1.15.2
2 changes: 1 addition & 1 deletion Source/TonSwift/Cells/Bitstring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct Bitstring: Hashable {
- returns true if the bit is set, false otherwise
*/
public func at(_ index: Int) throws -> Bit {
guard index <= _length && index >= 0 else {
guard index < _length && index >= 0 else {
throw TonError.indexOutOfBounds(index)
}

Expand Down
55 changes: 55 additions & 0 deletions Source/TonSwift/Stake/StakeDepositMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Foundation
import BigInt

public struct StakeDepositMessage {
public static func whalesInternalMessage(queryId: BigUInt,
poolAddress: Address,
amount: BigUInt,
forwardAmount: BigUInt,
bounce: Bool = true) throws -> MessageRelaxed {
let body = Builder()
try body.store(uint: OpCodes.WHALES_DEPOSIT, bits: 32)
try body.store(uint: queryId, bits: 64)
try body.store(coins: Coins(forwardAmount.magnitude))

return MessageRelaxed.internal(
to: poolAddress,
value: amount,
bounce: bounce,
body: try body.asCell()
)
}

public static func liquidTFInternalMessage(queryId: BigUInt,
poolAddress: Address,
amount: BigUInt,
bounce: Bool = true) throws -> MessageRelaxed {
let body = Builder()
try body.store(uint: OpCodes.LIQUID_TF_DEPOSIT, bits: 32)
try body.store(uint: queryId, bits: 64)
try body.store(uint: 0x000000000005b7ce, bits: 64)

return MessageRelaxed.internal(
to: poolAddress,
value: amount,
bounce: bounce,
body: try body.asCell()
)
}

public static func tfInternalMessage(queryId: BigUInt,
poolAddress: Address,
amount: BigUInt,
bounce: Bool = true) throws -> MessageRelaxed {
let body = Builder()
try body.store(uint: 0, bits: 32)
try body.writeSnakeData(Data("d".utf8))

return MessageRelaxed.internal(
to: poolAddress,
value: amount,
bounce: bounce,
body: try body.asCell()
)
}
}
66 changes: 66 additions & 0 deletions Source/TonSwift/Stake/StakeWithdrawMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Foundation
import BigInt

public struct StakeWithdrawMessage {
public static func whalesInternalMessage(queryId: BigUInt,
poolAddress: Address,
amount: BigUInt,
withdrawFee: BigUInt,
forwardAmount: BigUInt,
bounce: Bool = true) throws -> MessageRelaxed {
let body = Builder()
try body.store(uint: OpCodes.WHALES_WITHDRAW, bits: 32)
try body.store(uint: queryId, bits: 64)
try body.store(coins: Coins(forwardAmount.magnitude))
try body.store(coins: Coins(amount.magnitude))

return MessageRelaxed.internal(
to: poolAddress,
value: withdrawFee,
bounce: bounce,
body: try body.asCell()
)
}

public static func liquidTFInternalMessage(queryId: BigUInt,
amount: BigUInt,
withdrawFee: BigUInt,
jettonWalletAddress: Address,
responseAddress: Address,
bounce: Bool = true) throws -> MessageRelaxed {
let customPayload = Builder()
try customPayload.store(uint: 1, bits: 1)
try customPayload.store(uint: 0, bits: 1)

let body = Builder()
try body.store(uint: OpCodes.LIQUID_TF_BURN, bits: 32)
try body.store(uint: queryId, bits: 64)
try body.store(coins: Coins(amount.magnitude))
try body.store(AnyAddress(responseAddress))
try body.storeMaybe(ref: customPayload.endCell())

return MessageRelaxed.internal(
to: jettonWalletAddress,
value: withdrawFee,
bounce: bounce,
body: try body.asCell()
)
}

public static func tfInternalMessage(queryId: BigUInt,
poolAddress: Address,
amount: BigUInt,
withdrawFee: BigUInt,
bounce: Bool = true) throws -> MessageRelaxed {
let body = Builder()
try body.store(uint: 0, bits: 32)
try body.writeSnakeData(Data("w".utf8))

return MessageRelaxed.internal(
to: poolAddress,
value: withdrawFee,
bounce: bounce,
body: try body.asCell()
)
}
}
4 changes: 4 additions & 0 deletions Source/TonSwift/Util/OpCodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public enum OpCodes {
public static var NFT_TRANSFER: Int32 = 0x5fcc3d14
public static var STONFI_SWAP: Int32 = 0x25938561
public static var CHANGE_DNS_RECORD: Int32 = 0x4eb1f0f9
public static var LIQUID_TF_DEPOSIT: Int32 = 0x47d54391
public static var LIQUID_TF_BURN: Int32 = 0x595f07bc
public static var WHALES_DEPOSIT: Int32 = 2077040623
public static var WHALES_WITHDRAW: UInt32 = 3665837821
}
2 changes: 1 addition & 1 deletion Source/TonSwift/Util/TonError.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum TonError: Error {
public enum TonError: Error, Equatable {
case indexOutOfBounds(Int)
case offsetOutOfBounds(Int)
case custom(String)
Expand Down
16 changes: 16 additions & 0 deletions Tests/TonSwiftTests/Cells/BitStringTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,20 @@ final class BitStringTest: XCTestCase {
XCTAssertEqual(r.toString(), hex)
}
}

func testCorrectBoundsCheckForBitAccess() throws {
let dataHexString = "00240000000054657374205152207369676e6572"
let offset = 16
let length = 144

let bitstring = try Bitstring(
data: Data(hex: dataHexString)!,
offset: offset,
length: length
)

XCTAssertThrowsError(try bitstring.at(144)) { error in
XCTAssertEqual(error as! TonError, TonError.indexOutOfBounds(144))
}
}
}
Loading