Skip to content

Commit

Permalink
complete 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Mar 15, 2024
1 parent 811542b commit 28990d5
Show file tree
Hide file tree
Showing 22 changed files with 3,165 additions and 256 deletions.
13 changes: 9 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ let name: String = "TonSdkSwift"
var packageDependencies: [Package.Dependency] = [
.package(url: "https://github.com/nerzh/swift-regular-expression.git", .upToNextMajor(from: "0.2.3")),
.package(url: "https://github.com/bytehubio/BigInt.git", exact: "5.3.0"),
.package(url: "https://github.com/nerzh/swift-extensions-pack.git", .upToNextMajor(from: "1.16.1")),
.package(url: "https://github.com/nerzh/swift-extensions-pack.git", .upToNextMajor(from: "1.19.1")),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/bytehubio/CryptoSwift", .upToNextMajor(from: "1.8.1")),
.package(url: "https://github.com/nerzh/swift-net-layer", .upToNextMajor(from: "1.3.2")),
]

var mainTarget: [Target.Dependency] = [
.product(name: "SwiftRegularExpression", package: "swift-regular-expression"),
.product(name: "SwiftExtensionsPack", package: "swift-extensions-pack"),
.product(name: "BigInt", package: "BigInt"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "CryptoSwift", package: "CryptoSwift"),
.product(name: "SwiftNetLayer", package: "swift-net-layer"),
]

var testTarget: [Target.Dependency] = mainTarget + [
.init(stringLiteral: name)
]


let package = Package(
name: name,
platforms: [
.macOS(.v11),
.iOS(.v13),
.macOS(.v13),
.iOS(.v13)
],
products: [
.library(
Expand All @@ -47,3 +50,5 @@ let package = Package(
),
]
)


17 changes: 15 additions & 2 deletions Sources/TonSdkSwift/Boc/CellBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,26 @@ open class CellBuilder {
}

@discardableResult
public func storeDict(_ hashmap: Cell? = nil) throws -> Self {
public func storeDict<K,V>(_ hashmap: Hashmap<K,V>? = nil) throws -> Self {
guard let hashmap = hashmap else {
try storeBit(.b0)
return self
}

let slice = hashmap.parse()
let slice = try hashmap.cell().parse()
try storeSlice(slice)

return self
}

@discardableResult
public func storeDict<K,V>(_ hashmap: HashmapE<K,V>? = nil) throws -> Self {
guard let hashmap = hashmap else {
try storeBit(.b0)
return self
}

let slice = try hashmap.cell().parse()
try storeSlice(slice)

return self
Expand Down
121 changes: 69 additions & 52 deletions Sources/TonSdkSwift/Boc/CellSlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import BigInt

public struct CellSlice {
open class CellSlice: Equatable {
public var bits: [Bit]
public var refs: [Cell]

Expand All @@ -17,8 +17,13 @@ public struct CellSlice {
self.refs = refs
}

public static func == (lhs: CellSlice, rhs: CellSlice) -> Bool {
lhs.bits == rhs.bits &&
lhs.refs == rhs.refs
}

@discardableResult
public mutating func skipBits(size: Int) throws -> Self {
public func skipBits(size: Int) throws -> Self {
if bits.count < size {
throw ErrorTonSdkSwift("Slice: bits overflow.")
}
Expand All @@ -27,7 +32,7 @@ public struct CellSlice {
return self
}

public mutating func skipRefs(size: Int) throws -> Self {
public func skipRefs(size: Int) throws -> Self {
if refs.count < size {
throw ErrorTonSdkSwift("Slice: refs overflow.")
}
Expand All @@ -36,17 +41,17 @@ public struct CellSlice {
return self
}

public mutating func skipDict() throws -> Self {
public func skipDict() throws -> Self {
let isEmpty = try loadBit().rawValue == 0
return isEmpty ? try skipRefs(size: 1) : self
}

@discardableResult
public mutating func skip(size: Int) throws -> Self {
public func skip(size: Int) throws -> Self {
try skipBits(size: size)
}

public mutating func loadRef() throws -> Cell {
public func loadRef() throws -> Cell {
if refs.isEmpty {
throw ErrorTonSdkSwift("Slice: refs overflow.")
}
Expand All @@ -62,15 +67,15 @@ public struct CellSlice {
return refs[0]
}

public mutating func loadMaybeRef() throws -> Cell? {
public func loadMaybeRef() throws -> Cell? {
try loadBit().rawValue == 1 ? try loadRef() : nil
}

public func preloadMaybeRef() throws -> Cell? {
try preloadBit().rawValue == 1 ? try preloadRef() : nil
}

public mutating func loadBit() throws -> Bit {
public func loadBit() throws -> Bit {
if bits.isEmpty {
throw ErrorTonSdkSwift("Slice: bits overflow.")
}
Expand All @@ -86,7 +91,7 @@ public struct CellSlice {
return bits[0]
}

public mutating func loadBits(size: Int) throws -> [Bit] {
public func loadBits(size: Int) throws -> [Bit] {
if size < 0 || bits.count < size {
throw ErrorTonSdkSwift("Slice: bits overflow.")
}
Expand All @@ -102,7 +107,7 @@ public struct CellSlice {
return copyBits.shift(size)
}

public mutating func loadBigInt(size: Int) throws -> BigInt {
public func loadBigInt(size: Int) throws -> BigInt {
let bits = try loadBits(size: size)
return bits.toBigInt()
}
Expand All @@ -112,7 +117,7 @@ public struct CellSlice {
return bits.toBigInt()
}

public mutating func loadBigUInt(size: Int) throws -> BigUInt {
public func loadBigUInt(size: Int) throws -> BigUInt {
let bits = try loadBits(size: size)
return bits.toBigUInt()
}
Expand All @@ -122,7 +127,7 @@ public struct CellSlice {
return bits.toBigUInt()
}

public mutating func loadVarBigInt(length: Int) throws -> BigInt {
public func loadVarBigInt(length: Int) throws -> BigInt {
let size = Int(ceil(log2(Double(length))))
let sizeBytes = try preloadBigUInt(size: size)
let sizeBits = sizeBytes * 8
Expand All @@ -144,7 +149,7 @@ public struct CellSlice {
return [Bit](bits).toBigInt()
}

public mutating func loadVarBigUInt(length: Int) throws -> BigUInt {
public func loadVarBigUInt(length: Int) throws -> BigUInt {
let size = Int(ceil(log2(Double(length))))
let sizeBytes = try preloadBigUInt(size: size)
let sizeBits = sizeBytes * 8
Expand All @@ -153,7 +158,10 @@ public struct CellSlice {
throw ErrorTonSdkSwift("Slice: can't perform loadVarBigUint – not enough bits")
}

try skip(size: Int(size))
let bits = try loadBits(size: Int(sizeBits))


return bits.toBigUInt()
}

Expand All @@ -166,7 +174,7 @@ public struct CellSlice {
return [Bit](bits).toBigUInt()
}

public mutating func loadBytes(size: Int) throws -> Data {
public func loadBytes(size: Int) throws -> Data {
let bits = try loadBits(size: size * 8)
return try bits.toBytes()
}
Expand All @@ -176,7 +184,7 @@ public struct CellSlice {
return try bits.toBytes()
}

public mutating func loadString(size: Int? = nil) throws -> String {
public func loadString(size: Int? = nil) throws -> String {
let bytes = size == nil ? try loadBytes(size: bits.count / 8) : try loadBytes(size: size!)
guard let result = String(data: bytes, encoding: .utf8) else {
throw ErrorTonSdkSwift("Slice: can't convert loadString")
Expand All @@ -192,7 +200,17 @@ public struct CellSlice {
return result
}

public mutating func loadAddress() throws -> Address? {
public func loadSlice(size: Int = 0) throws -> CellSlice {
let bits = try loadBits(size: size)
return CellSlice(bits: bits, refs: [])
}

public func preloadSlice(size: Int = 0) throws -> CellSlice {
let bits = try preloadBits(size: size)
return CellSlice(bits: bits, refs: [])
}

public func loadAddress() throws -> Address? {
let flagAddressNo: [Bit] = .init([0, 0])
let flagAddress: [Bit] = .init([1, 0])
let flag = try preloadBits(size: 2)
Expand All @@ -205,12 +223,11 @@ public struct CellSlice {
let size = 2 + 1 + 8 + 256
// Slice 2 because we don't need flag bits
var bits = try loadBits(size: size)[2...]

// Anycast is currently unused
_ = bits.popFirst()

let workchain = [Bit](bits[..<8]).toBigInt()
let hash = try [Bit](bits[8..<264]).toHex()
let workchain = [Bit](bits[3..<3+8]).toBigInt()
let hash = try [Bit](bits[3+8..<3+8+256]).toHex()
let raw = "\(workchain):\(hash)"

return try Address(address: raw)
Expand Down Expand Up @@ -245,7 +262,7 @@ public struct CellSlice {
}
}

public mutating func loadCoins(decimals: Int = 9) throws -> Coins {
public func loadCoins(decimals: Int = 9) throws -> Coins {
let coins = try loadVarBigUInt(length: 16)
return .init(nanoValue: coins, decimals: decimals)
}
Expand All @@ -254,38 +271,38 @@ public struct CellSlice {
let coins = try preloadVarBigUint(length: 16)
return .init(nanoValue: coins, decimals: decimals)
}
#warning("AFTER HASHMAP_E")
// func loadDict(keySize: Int, options: [String: Any] = [:]) -> HashmapE {
// let dictConstructor = loadBit()
// let isEmpty = dictConstructor == 0
//
// if !isEmpty {
// return HashmapE.parse(
// keySize: keySize,
// slice: Slice(bits: [dictConstructor], refs: [loadRef()]),
// options: options
// )
// } else {
// return HashmapE(keySize: keySize, options: options)
// }
// }
//
// func preloadDict(keySize: Int, options: [String: Any] = [:]) -> HashmapE {
// let dictConstructor = preloadBit()
// let isEmpty = dictConstructor == 0
//
// if !isEmpty {
// return HashmapE.parse(
// keySize: keySize,
// slice: Slice(bits: [dictConstructor], refs: [preloadRef()]),
// options: options
// )
// } else {
// return HashmapE(keySize: keySize, options: options)
// }
// }

public static func parse(cell: Cell) -> Self {

public func loadDict<K,V>(keySize: Int, options: HashmapOptions<K,V>? = nil) throws -> HashmapE<K,V> {
let dictConstructor = try loadBit()
let isEmpty = dictConstructor == .b0

if !isEmpty {
return try HashmapE.parse(
keySize: keySize,
slice: CellSlice(bits: [dictConstructor], refs: [loadRef()]),
options: options
)
} else {
return try HashmapE(keySize: keySize, options: options)
}
}

public func preloadDict<K,V>(keySize: Int, options: HashmapOptions<K,V>? = nil) throws -> HashmapE<K,V> {
let dictConstructor = try preloadBit()
let isEmpty = dictConstructor == .b0

if !isEmpty {
return try HashmapE.parse(
keySize: keySize,
slice: CellSlice(bits: [dictConstructor], refs: [preloadRef()]),
options: options
)
} else {
return try HashmapE(keySize: keySize, options: options)
}
}

public static func parse(cell: Cell) -> CellSlice {
.init(bits: cell.bits, refs: cell.refs)
}
}
Loading

0 comments on commit 28990d5

Please sign in to comment.