Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #54 from nervosnetwork/batch-variable-rename
Browse files Browse the repository at this point in the history
Batch variable rename
  • Loading branch information
ashchan authored Apr 25, 2019
2 parents bf717aa + 3fd6158 commit 0456e54
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 39 deletions.
18 changes: 3 additions & 15 deletions Source/Types/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,11 @@ import Foundation
public struct Block: Codable {
public let header: Header
public let uncles: [UncleBlock]
public let commitTransactions: [Transaction]
public let proposalTransactions: [ProposalShortId]

enum CodingKeys: String, CodingKey {
case header
case uncles
case commitTransactions = "commit_transactions"
case proposalTransactions = "proposal_transactions"
}
public let transactions: [Transaction]
public let proposals: [ProposalShortId]
}

public struct UncleBlock: Codable {
public let header: Header
public let proposalTransactions: [ProposalShortId]

enum CodingKeys: String, CodingKey {
case header
case proposalTransactions = "proposal_transactions"
}
public let proposals: [ProposalShortId]
}
9 changes: 6 additions & 3 deletions Source/Types/CellInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import Foundation
public struct CellInput: Codable, Param {
public let previousOutput: OutPoint
public let args: [HexString]
public let since: String

public init(previousOutput: OutPoint, args: [HexString]) {
public init(previousOutput: OutPoint, args: [HexString], since: String) {
self.previousOutput = previousOutput
self.args = args
self.since = since
}

enum CodingKeys: String, CodingKey {
case previousOutput = "previous_output"
case args
case args, since
}

public var param: [String: Any] {
return [
CodingKeys.previousOutput.rawValue: previousOutput.param,
CodingKeys.args.rawValue: args
CodingKeys.args.rawValue: args,
CodingKeys.since.rawValue: since
]
}
}
8 changes: 4 additions & 4 deletions Source/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public struct Header: Codable {
public let parentHash: H256
public let timestamp: String
public let number: BlockNumber
public let txsCommit: H256
public let txsProposal: H256
public let transactionsRoot: H256
public let proposalsRoot: H256
public let witnessesRoot: H256
public let difficulty: UInt256
public let unclesHash: H256
Expand All @@ -33,8 +33,8 @@ public struct Header: Codable {
case parentHash = "parent_hash"
case timestamp
case number
case txsCommit = "txs_commit"
case txsProposal = "txs_proposal"
case transactionsRoot = "transactions_root"
case proposalsRoot = "proposals_root"
case witnessesRoot = "witnesses_root"
case difficulty
case unclesHash = "uncles_hash"
Expand Down
13 changes: 9 additions & 4 deletions Source/Types/OutPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import Foundation

public struct OutPoint: Codable, Param {
public let hash: H256
public let txHash: H256
public let index: UInt32

public init(hash: String, index: UInt32) {
self.hash = hash
enum CodingKeys: String, CodingKey {
case txHash = "tx_hash"
case index
}

public init(txHash: H256, index: UInt32) {
self.txHash = txHash
self.index = index
}

public var param: [String: Any] {
return [
"hash": hash,
"tx_hash": txHash,
"index": index
]
}
Expand Down
18 changes: 9 additions & 9 deletions Source/Types/Script.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import Foundation

public struct Script: Codable, Param {
public let args: [HexString]
public var binaryHash: H256
public var codeHash: H256

enum CodingKeys: String, CodingKey {
case args
case binaryHash = "binary_hash"
case codeHash = "code_hash"
}

static let alwaysSuccessHash: H256 = "0000000000000000000000000000000000000000000000000000000000000001"
public static var alwaysSuccess: Script {
return Script(args: [], binaryHash: alwaysSuccessHash)
return Script(args: [], codeHash: alwaysSuccessHash)
}

public var typeHash: String {
var bytes = [UInt8]()
bytes.append(contentsOf: Data(hex: binaryHash).bytes)
bytes.append(contentsOf: Data(hex: codeHash).bytes)
args.forEach { (arg) in
bytes.append(contentsOf: Data(hex: arg).bytes)
}
Expand All @@ -35,25 +35,25 @@ public struct Script: Codable, Param {
public var param: [String: Any] {
return [
CodingKeys.args.rawValue: args,
CodingKeys.binaryHash.rawValue: binaryHash
CodingKeys.codeHash.rawValue: codeHash
]
}

public init(args: [HexString] = [], binaryHash: H256 = H256.zeroHash) {
public init(args: [HexString] = [], codeHash: H256 = H256.zeroHash) {
self.args = args
self.binaryHash = binaryHash
self.codeHash = codeHash
}
}

public extension Script {
static func verifyScript(for publicKey: String, binaryHash: String) -> Script {
static func verifyScript(for publicKey: String, codeHash: String) -> Script {
let args = [
Utils.prefixHex(publicKey.data(using: .utf8)!.toHexString())
// Although public key itself is a hex string, when loaded as binary the format is ignored.
]
return Script(
args: args,
binaryHash: binaryHash
codeHash: codeHash
)
}
}
4 changes: 2 additions & 2 deletions Source/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public struct Utils {

public static func publicToAddress(_ publicKey: String) -> String {
// TODO: save/load (cell) binary hash info
let binaryHash = "0x828d1e109a79964521bf5fbbedb4f6e695a9c4b6b674a58887f30c7398e93a76"
return Script.verifyScript(for: publicKey, binaryHash: binaryHash).typeHash
let codeHash = "0x828d1e109a79964521bf5fbbedb4f6e695a9c4b6b674a58887f30c7398e93a76"
return Script.verifyScript(for: publicKey, codeHash: codeHash).typeHash
}

public static func privateToAddress(_ privateKey: String) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Tests/API/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class APIClientTests: XCTestCase {

func testGetTransaction() throws {
let genesisBlock = try client.genesisBlock()
if let tx = genesisBlock.commitTransactions.first {
if let tx = genesisBlock.transactions.first {
let result = try client.getTransaction(hash: tx.hash)
XCTAssertNotNil(result)
XCTAssertEqual(tx.hash, result.hash)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Types/ScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScriptTests: XCTestCase {
func testScriptTypeHash() throws {
let script = Script(
args: ["0x01"],
binaryHash: H256.zeroHash
codeHash: H256.zeroHash
)
XCTAssertEqual("0xdade0e507e27e2a5995cf39c8cf454b6e70fa80d03c1187db7a4cb2c9eab79da", script.typeHash)
}
Expand Down

0 comments on commit 0456e54

Please sign in to comment.