Skip to content

Commit

Permalink
wallet contract renames for brevity
Browse files Browse the repository at this point in the history
  • Loading branch information
oleganza committed Apr 1, 2023
1 parent 33e657c commit 7742e2a
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The focus of the library is type safety and serialization. It does not support c
- [x] TON mnemonic and key pairs
- [x] Bitstrings
- [x] Cells
- [x] Hashmap (aka “dictionary”)
- [x] Hashmaps (aka “dictionary”)
- [x] Contract
- [x] StateInit
- [x] CommonMsgInfo
Expand Down
9 changes: 5 additions & 4 deletions Source/TonSwift/Wallets/WalletContract.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import BigInt
import Foundation

/// All wallets implement a compatible interface for sending messages
public protocol WalletContract: Contract {
func createTransfer(args: WalletTransferData) throws -> Cell
}

public struct WalletTransferData {
public let seqno: UInt64
public let secretKey: Data
public let messages: [MessageRelaxed]
public let sendMode: SendMode
public let timeout: UInt64?
}

public protocol WalletContract: Contract {
func createTransfer(args: WalletTransferData) throws -> Cell
}
97 changes: 0 additions & 97 deletions Source/TonSwift/Wallets/WalletContractV4.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum WalletContractV1Revision {
case r1, r2, r3
}

public final class WalletContractV1: WalletContract {
public final class WalletV1: WalletContract {
public let workchain: Int8
public let stateInit: StateInit
public let publicKey: Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum WalletContractV2Revision {
case r1, r2
}

public final class WalletContractV2: WalletContract {
public final class WalletV2: WalletContract {
public let workchain: Int8
public let stateInit: StateInit
public let publicKey: Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum WalletContractV3Revision {
case r1, r2
}

public final class WalletContractV3: WalletContract {
public final class WalletV3: WalletContract {
public let workchain: Int8
public let stateInit: StateInit
public let publicKey: Data
Expand Down
103 changes: 103 additions & 0 deletions Source/TonSwift/Wallets/WalletV4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import Foundation
import BigInt
import TweetNacl

public class WalletV4R1: WalletV4 {
public init(seqno: Int64 = 0,
workchain: Int8 = 0,
publicKey: Data,
walletId: UInt32? = nil,
plugins: Set<Address> = []
) {
let code = try! Cell.fromBoc(src: Data(base64Encoded: "te6cckECFQEAAvUAART/APSkE/S88sgLAQIBIAIDAgFIBAUE+PKDCNcYINMf0x/THwL4I7vyY+1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8REhMUA+7QAdDTAwFxsJFb4CHXScEgkVvgAdMfIYIQcGx1Z70ighBibG5jvbAighBkc3RyvbCSXwPgAvpAMCD6RAHIygfL/8nQ7UTQgQFA1yH0BDBcgQEI9ApvoTGzkl8F4ATTP8glghBwbHVnupEx4w0kghBibG5juuMABAYHCAIBIAkKAFAB+gD0BDCCEHBsdWeDHrFwgBhQBcsFJ88WUAP6AvQAEstpyx9SEMs/AFL4J28ighBibG5jgx6xcIAYUAXLBSfPFiT6AhTLahPLH1Iwyz8B+gL0AACSghBkc3Ryuo41BIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UghBkc3Rygx6xcIAYUATLBVjPFiL6AhLLassfyz+UEDRfBOLJgED7AAIBIAsMAFm9JCtvaiaECAoGuQ+gIYRw1AgIR6STfSmRDOaQPp/5g3gSgBt4EBSJhxWfMYQCAVgNDgARuMl+1E0NcLH4AD2ynftRNCBAUDXIfQEMALIygfL/8nQAYEBCPQKb6ExgAgEgDxAAGa3OdqJoQCBrkOuF/8AAGa8d9qJoQBBrkOuFj8AAbtIH+gDU1CL5AAXIygcVy//J0Hd0gBjIywXLAiLPFlAF+gIUy2sSzMzJcfsAyEAUgQEI9FHypwIAbIEBCNcYyFQgJYEBCPRR8qeCEG5vdGVwdIAYyMsFywJQBM8WghAF9eEA+gITy2oSyx/JcfsAAgBygQEI1xgwUgKBAQj0WfKn+CWCEGRzdHJwdIAYyMsFywJQBc8WghAF9eEA+gIUy2oTyx8Syz/Jc/sAAAr0AMntVEap808=")!)[0]
super.init(code:code, seqno: seqno, workchain: workchain, publicKey: publicKey, walletId: walletId, plugins: plugins)
}
}

public class WalletV4R2: WalletV4 {
public init(seqno: Int64 = 0,
workchain: Int8 = 0,
publicKey: Data,
walletId: UInt32? = nil,
plugins: Set<Address> = []
) {
let code = try! Cell.fromBoc(src: Data(base64Encoded: "te6ccgECFAEAAtQAART/APSkE/S88sgLAQIBIAIDAgFIBAUE+PKDCNcYINMf0x/THwL4I7vyZO1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8QERITAubQAdDTAyFxsJJfBOAi10nBIJJfBOAC0x8hghBwbHVnvSKCEGRzdHK9sJJfBeAD+kAwIPpEAcjKB8v/ydDtRNCBAUDXIfQEMFyBAQj0Cm+hMbOSXwfgBdM/yCWCEHBsdWe6kjgw4w0DghBkc3RyupJfBuMNBgcCASAICQB4AfoA9AQw+CdvIjBQCqEhvvLgUIIQcGx1Z4MesXCAGFAEywUmzxZY+gIZ9ADLaRfLH1Jgyz8gyYBA+wAGAIpQBIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UAXKwjiOCEGRzdHKDHrFwgBhQBcsFUAPPFiP6AhPLassfyz/JgED7AJJfA+ICASAKCwBZvSQrb2omhAgKBrkPoCGEcNQICEekk30pkQzmkD6f+YN4EoAbeBAUiYcVnzGEAgFYDA0AEbjJftRNDXCx+AA9sp37UTQgQFA1yH0BDACyMoHy//J0AGBAQj0Cm+hMYAIBIA4PABmtznaiaEAga5Drhf/AABmvHfaiaEAQa5DrhY/AAG7SB/oA1NQi+QAFyMoHFcv/ydB3dIAYyMsFywIizxZQBfoCFMtrEszMyXP7AMhAFIEBCPRR8qcCAHCBAQjXGPoA0z/IVCBHgQEI9FHyp4IQbm90ZXB0gBjIywXLAlAGzxZQBPoCFMtqEssfyz/Jc/sAAgBsgQEI1xj6ANM/MFIkgQEI9Fnyp4IQZHN0cnB0gBjIywXLAlAFzxZQA/oCE8tqyx8Syz/Jc/sAAAr0AMntVA==")!)[0]
super.init(code:code, seqno: seqno, workchain: workchain, publicKey: publicKey, walletId: walletId, plugins: plugins)
}
}

/// Internal WalletV4 implementation. Use specific revision `WalletV4R1` instead.
public class WalletV4: WalletContract {
public let seqno: Int64
public let workchain: Int8
public let publicKey: Data
public let walletId: UInt32
public let plugins: Set<Address>
public let code: Cell

fileprivate init(code: Cell,
seqno: Int64 = 0,
workchain: Int8 = 0,
publicKey: Data,
walletId: UInt32? = nil,
plugins: Set<Address> = []
) {
self.code = code
self.seqno = seqno
self.workchain = workchain
self.publicKey = publicKey

if let walletId {
self.walletId = walletId
} else {
self.walletId = 698983191 + UInt32(workchain)
}
self.plugins = plugins
}

public var stateInit: StateInit {
let data = try! Builder()
.store(uint: 0, bits: 32) // initial seqno = 0
.store(uint: self.walletId, bits: 32)
.store(data: publicKey)
.store(dict: Set<CompactAddress>()) // initial plugins list = []
.endCell()

return StateInit(code: self.code, data: data)
}

func pluginsCompact() -> Set<CompactAddress> {
Set(self.plugins.map{ a in CompactAddress(a) })
}

public func createTransfer(args: WalletTransferData) throws -> Cell {
guard args.messages.count <= 4 else {
throw TonError.custom("Maximum number of messages in a single transfer is 4")
}

let signingMessage = try Builder().store(uint: walletId, bits: 32)
if args.seqno == 0 {
for _ in 0..<32 {
try signingMessage.store(bit: 1)
}
} else {
let defaultTimeout = UInt64(Date().timeIntervalSince1970) + 60 // Default timeout: 60 seconds
try signingMessage.store(uint: args.timeout ?? defaultTimeout, bits: 32)
}

try signingMessage.store(uint: args.seqno, bits: 32)
try signingMessage.store(uint: 0, bits: 8) // Simple order
for message in args.messages {
try signingMessage.store(uint: UInt64(args.sendMode.rawValue), bits: 8)
try signingMessage.store(ref: try Builder().store(message))
}

let signature = try NaclSign.sign(message: signingMessage.endCell().hash(), secretKey: args.secretKey)

let body = Builder()
try body.store(data: signature)
try body.store(signingMessage)

return try body.endCell()
}
}
6 changes: 3 additions & 3 deletions Tests/TonSwiftTests/Wallets/WalletContractV1Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class WalletContractV1Test: XCTestCase {
}

private func testR1() throws {
let contractR1 = try WalletContractV1(workchain: 0, publicKey: publicKey, revision: .r1)
let contractR1 = try WalletV1(workchain: 0, publicKey: publicKey, revision: .r1)

XCTAssertEqual(try contractR1.address(), try Address.parse("EQCtW_zzk6n82ebaVQFq8P_04wOemYhtwqMd3NuArmPODRvD"))
XCTAssertEqual(try contractR1.stateInit.data?.toString(), "x{000000005754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand All @@ -26,7 +26,7 @@ final class WalletContractV1Test: XCTestCase {
}

private func testR2() throws {
let contractR2 = try WalletContractV1(workchain: 0, publicKey: publicKey, revision: .r2)
let contractR2 = try WalletV1(workchain: 0, publicKey: publicKey, revision: .r2)

XCTAssertEqual(try contractR2.address(), try Address.parse("EQATDkvcCA2fFWbSTHMpGCrjkNGqgEywES15ZS11HHY3UuxK"))
XCTAssertEqual(try contractR2.stateInit.data?.toString(), "x{000000005754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand All @@ -37,7 +37,7 @@ final class WalletContractV1Test: XCTestCase {
}

private func testR3() throws {
let contractR3 = try WalletContractV1(workchain: 0, publicKey: publicKey, revision: .r3)
let contractR3 = try WalletV1(workchain: 0, publicKey: publicKey, revision: .r3)

XCTAssertEqual(try contractR3.address(), try Address.parse("EQBRRPBUtgzq5om6O4rtxwPW4hyDxiXYeIko27tvsm97kUw3"))
XCTAssertEqual(try contractR3.stateInit.data?.toString(), "x{000000005754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand Down
4 changes: 2 additions & 2 deletions Tests/TonSwiftTests/Wallets/WalletContractV2Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class WalletContractV2Test: XCTestCase {
}

private func testR1() throws {
let contractR1 = try WalletContractV2(workchain: 0, publicKey: publicKey, revision: .r1)
let contractR1 = try WalletV2(workchain: 0, publicKey: publicKey, revision: .r1)

XCTAssertEqual(try contractR1.address(), try Address.parse("EQD3ES67JiTYq5y2eE1-fivl5kANn-gKDDjvpbxNCQWPzs4D"))
XCTAssertEqual(try contractR1.stateInit.data?.toString(), "x{000000005754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand All @@ -28,7 +28,7 @@ final class WalletContractV2Test: XCTestCase {
}

private func testR2() throws {
let contractR2 = try WalletContractV2(workchain: 0, publicKey: publicKey, revision: .r2)
let contractR2 = try WalletV2(workchain: 0, publicKey: publicKey, revision: .r2)

XCTAssertEqual(try contractR2.address(), try Address.parse("EQAkAcNLtzCHudScK9Hsk9I_7SrunBWf_9VrA2xJmGebwEsl"))
XCTAssertEqual(try contractR2.stateInit.data?.toString(), "x{000000005754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand Down
4 changes: 2 additions & 2 deletions Tests/TonSwiftTests/Wallets/WalletContractV3Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class WalletContractV3Test: XCTestCase {
}

private func testR1() throws {
let contractR1 = try WalletContractV3(workchain: 0, publicKey: publicKey, revision: .r1)
let contractR1 = try WalletV3(workchain: 0, publicKey: publicKey, revision: .r1)

XCTAssertEqual(try contractR1.address(), try Address.parse("EQBJp7j5N40GXJbAqFSnfTV1Af4ZTyHIMpRbKcudNhWJbbNO"))
XCTAssertEqual(try contractR1.stateInit.data?.toString(), "x{0000000029A9A3175754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand All @@ -28,7 +28,7 @@ final class WalletContractV3Test: XCTestCase {
}

private func testR2() throws {
let contractR2 = try WalletContractV3(workchain: 0, publicKey: publicKey, revision: .r2)
let contractR2 = try WalletV3(workchain: 0, publicKey: publicKey, revision: .r2)

XCTAssertEqual(try contractR2.address(), try Address.parse("EQA0D_5WdusaCB-SpnoE6l5TzdBmgOkzTcXrdh0px6g3zJSk"))
XCTAssertEqual(try contractR2.stateInit.data?.toString(), "x{0000000029A9A3175754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C522}")
Expand Down
4 changes: 2 additions & 2 deletions Tests/TonSwiftTests/Wallets/WalletContractV4Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class WalletContractV4Test: XCTestCase {
}

func testR1() throws {
let contractR1 = WalletContractV4(workchain: 0, publicKey: publicKey, code: .R1())
let contractR1 = WalletV4R1(workchain: 0, publicKey: publicKey)

XCTAssertEqual(try contractR1.address(), try Address.parse("EQAimhPwOYc5Z1JP_pddxo82SHOl67T0Lklw91pKtSX2Q094"))
XCTAssertEqual(try contractR1.stateInit.data?.toString(), "x{0000000029A9A3175754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C5224_}")
Expand All @@ -29,7 +29,7 @@ final class WalletContractV4Test: XCTestCase {
}

func testR2() throws {
let contractR2 = WalletContractV4(workchain: 0, publicKey: publicKey, code: .R2())
let contractR2 = WalletV4R2(workchain: 0, publicKey: publicKey)

XCTAssertEqual(try contractR2.address(), try Address.parse("EQDnBF4JTFKHTYjulEJyNd4dstLGH1m51UrLdu01_tw4z2Au"))
XCTAssertEqual(try contractR2.stateInit.data?.toString(), "x{0000000029A9A3175754865E86D0ADE1199301BBB0319A25ED6B129C4B0A57F28F62449B3DF9C5224_}")
Expand Down

0 comments on commit 7742e2a

Please sign in to comment.