Skip to content
This repository was archived by the owner on Jan 25, 2021. It is now read-only.
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
14 changes: 0 additions & 14 deletions O3/New Group/Clients/Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,21 +495,7 @@ public class Wallet {
let txid = self.unsignedPayloadToTransactionId(rawTransactionData)
return (txid, finalPayload)
}


private func buildNEP5TransferScript(scriptHash: String, decimals: Int, fromAddress: String,
toAddress: String, amount: Double) -> [UInt8] {

let amountToSend = Int(amount * pow(10, Double(decimals)))
let fromAddressHash = fromAddress.hashFromAddress()
let toAddressHash = toAddress.hashFromAddress()
let scriptBuilder = ScriptBuilder()
scriptBuilder.pushContractInvoke(scriptHash: scriptHash, operation: "transfer",
args: [amountToSend, toAddressHash, fromAddressHash])
let script = scriptBuilder.rawBytes
return [UInt8(script.count)] + script
}

enum InvokeError: Error {
case invokeFailed
}
Expand Down
67 changes: 9 additions & 58 deletions O3/New Group/Contracts/ScriptBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ScriptBuilder {
rawBytes.append(rawValue)
default:
let intBytes = toByteArray(intValue)
pushData(intBytes.fullHexString)
pushHexString(intBytes.fullHexString)
}
}

Expand All @@ -61,14 +61,6 @@ public class ScriptBuilder {
}
}

private func pushArray(_ arrayValue: [Any?]) {
for elem in arrayValue {
pushData(elem)
}
pushInt(arrayValue.count)
pushOPCode(.PACK)
}

private func pushTypedArray(_ arrayValue: [dAppProtocol.Arg?]) {
for elem in arrayValue {
pushTypedData(elem)
Expand All @@ -77,26 +69,6 @@ public class ScriptBuilder {
pushOPCode(.PACK)
}

public func resetScript() {
rawBytes.removeAll()
}

public func pushData(_ data: Any?) {
if let boolValue = data as? Bool {
pushBool(boolValue)
} else if let intValue = data as? Int {
pushInt(intValue)
} else if let stringValue = data as? String {
pushHexString(stringValue)
} else if let arrayValue = data as? [Any?] {
pushArray(arrayValue)
} else if data == nil {
pushBool(false)
} else {
fatalError("Unsupported Data Type Pushed on stack")
}
}

public func pushTypedData(_ data: dAppProtocol.Arg?) {
guard let unwrappedData = data else {
pushBool(false)
Expand All @@ -121,45 +93,24 @@ public class ScriptBuilder {
}
}

public func pushContractInvoke(scriptHash: String, operation: String? = nil, args: Any? = nil, useTailCall: Bool = false) {
pushData(args)

if let operation = operation {
let hex = operation.unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined()
pushData(hex)
}
if scriptHash.count != 40 {
fatalError("Attempting to invoke invalid contract")
}
if useTailCall {
pushOPCode(.TAILCALL)
} else {
pushOPCode(.APPCALL)
let toAppendBytes = scriptHash.dataWithHexString().bytes.reversed()
rawBytes += toAppendBytes
}
}

public func pushTypedContractInvoke(scriptHash: String, operation: String? = nil, args: [dAppProtocol.Arg?], useTailCall: Bool = false) {
public func pushTypedContractInvoke(scriptHash: String, operation: String? = nil, args: [dAppProtocol.Arg?]) {
if (!args.isEmpty) {
pushTypedArray(args.reversed())
} else {
pushBool(false)
}

if let operation = operation {
let hex = operation.unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined()
pushData(hex)
pushHexString(hex)
}

if scriptHash.count != 40 {
fatalError("Attempting to invoke invalid contract")
}
if useTailCall {
pushOPCode(.TAILCALL)
} else {
pushOPCode(.APPCALL)
let toAppendBytes = scriptHash.dataWithHexString().bytes.reversed()
rawBytes += toAppendBytes
}

pushOPCode(.APPCALL)
let toAppendBytes = scriptHash.dataWithHexString().bytes.reversed()
rawBytes += toAppendBytes
}
}