Skip to content
Open
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
35 changes: 35 additions & 0 deletions Sources/DCoreKit/Model/Operations/AnyCustomOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ public struct AnyCustomOperation: CustomOperation {
public let data: String

public var fee: AssetAmount = .unset

/**
Creates AnyCustomOperation
- Parameter id: type of Custom Operation
- Parameter payer: account id of payer
- Parameter requiredAuths: Accounts required to authorize this operation with signature. If not supplied, payer account will be used
- Parameter data: String data payload, i.e. JSON, XML etc.
*/
public init(
id: CustomOperationType,
payer: AccountObjectId,
requiredAuths: [AccountObjectId]? = nil,
data: String
) {
self.init(id: id, payer: payer, requiredAuths: requiredAuths, data: data.asEncoded())
}

/**
Creates AnyCustomOperation
- Parameter id: type of Custom Operation
- Parameter payer: account id of payer
- Parameter requiredAuths: Accounts required to authorize this operation with signature. If not supplied, payer account will be used
- Parameter data: Data payload
*/
public init(
id: CustomOperationType,
payer: AccountObjectId,
requiredAuths: [AccountObjectId]? = nil,
data: Data
) {
self.id = id
self.payer = payer
self.requiredAuths = requiredAuths ?? [payer]
self.data = data.toHex()
}

private enum CodingKeys: String, CodingKey {
case
Expand Down
13 changes: 13 additions & 0 deletions Tests/DCoreKitTests/OperationApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,23 @@ class OperationApiTests: XCTestCase {
).debug().toBlocking().single()
XCTAssertNotNil(confirm)
}

func testAnyCustomOperation() {
let pk = "5Hxwqx6JJUBYWjQNt8DomTNJ6r6YK8wDJym4CMAH1zGctFyQtzt"
let creds = try? Credentials("1.2.27".dcore.objectId()!, wif: pk)

let anyCustomOp = try? wss.broadcast.broadcastWithCallback(AnyCustomOperation(
id: .plaintext, payer: creds!.accountId, data: "some data"
), keypair: creds!.keyPair).debug().toBlocking().single()

XCTAssertNotNil(anyCustomOp)
}

static var allTests = [
("testTransferOperation", testTransferOperation),
("testTransferOperationToObjectIdWithOtherVarInt", testTransferOperationToObjectIdWithOtherVarInt),
("testSubmitAccountOperation", testSubmitAccountOperation),
("testUpdateAccountOperation", testUpdateAccountOperation),
("testAnyCustomOperation", testAnyCustomOperation),
]
}
5 changes: 3 additions & 2 deletions Tests/DCoreKitTests/SerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ final class SerializationTests: XCTestCase {
func testCustomOperationSerialization() {
let payerAcc = try! "1.2.1".asAccountObjectId()
let operation = AnyCustomOperation(
id: .plaintext, payer: payerAcc, requiredAuths: [payerAcc], data: "some data"
id: .plaintext, payer: payerAcc, data: "some data"
)

let expected = "12000000000000000000010101020000"
let expected = "12000000000000000000010101020009736f6d652064617461"
XCTAssertEqual(expected, operation.asData().toHex())
}

Expand All @@ -157,5 +157,6 @@ final class SerializationTests: XCTestCase {
("testMemoSerialization", testMemoSerialization),
("testObjectIdSerialization", testObjectIdSerialization),
("testVoteIdSerialization", testVoteIdSerialization),
("testCustomOperationSerialization", testCustomOperationSerialization),
]
}