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 #153 from nervosnetwork/rc/v0.22.0
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.22.0
  • Loading branch information
ashchan authored Oct 5, 2019
2 parents 2b487a1 + 364c1f5 commit 49a2838
Show file tree
Hide file tree
Showing 36 changed files with 233 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ matrix:
name: Tests with RPC
env:
- SKIP_RPC_TESTS=0
- CKB_VERSION=v0.21.1
- CKB_VERSION=v0.22.0
- CKB_FILENAME=ckb_${CKB_VERSION}_x86_64-apple-darwin
before_script:
- mkdir ckb
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# [v0.22.0](https://github.com/nervosnetwork/ckb-sdk-swift/compare/v0.21.2...v0.22.0) (2019-10-05)


### Features

* Parse header/epoch compact target field as hex string ([5f8f832](https://github.com/nervosnetwork/ckb-sdk-swift/commit/5f8f832))
* Update header and epoch structures ([ec0deaa](https://github.com/nervosnetwork/ckb-sdk-swift/commit/ec0deaa))
* Update Script.args and Transaction.witnesses type ([95e16d2](https://github.com/nervosnetwork/ckb-sdk-swift/commit/95e16d2))


### BREAKING CHANGES

* header and epoch structures are changed as per CKB update.

Note: field compact_target is output from RPC as uint32, but they should be hex string to
follow the convention. Await CKB to fix that.
* Bytes serialization are used for these two structures.
Before this [Bytes] serialization were used. This affects script and transaction
hash calculation and transaction signing.



# [v0.21.2](https://github.com/nervosnetwork/ckb-sdk-swift/compare/v0.21.1...v0.21.2) (2019-09-24)


Expand Down
2 changes: 1 addition & 1 deletion CKB.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CKB"
s.version = "0.21.2"
s.version = "0.22.0"
s.summary = "Swift SDK for Nervos CKB"

s.description = <<-DESC
Expand Down
12 changes: 2 additions & 10 deletions CKB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
1A414AA021C770DD00B28C09 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A414A9F21C770DD00B28C09 /* Utils.swift */; };
1A4A4BAB22A6BF40003DC827 /* CellOutputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BAA22A6BF40003DC827 /* CellOutputTests.swift */; };
1A4A4BAD22A6C154003DC827 /* CellInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BAC22A6C154003DC827 /* CellInputTests.swift */; };
1A4A4BAF22A6C2EB003DC827 /* WitnessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BAE22A6C2EB003DC827 /* WitnessTests.swift */; };
1A4A4BB122A6C445003DC827 /* TransactionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BB022A6C445003DC827 /* TransactionTests.swift */; };
1A4A4BB322A6C5F0003DC827 /* APIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BB222A6C5F0003DC827 /* APIErrorTests.swift */; };
1A4A4BB522A6C64B003DC827 /* APIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4A4BB422A6C64B003DC827 /* APIError.swift */; };
Expand Down Expand Up @@ -122,7 +121,6 @@
1AD3195D225DBA0B00F41790 /* AddressGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD3195C225DBA0B00F41790 /* AddressGenerator.swift */; };
1AD3195F225DBCB300F41790 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD3195E225DBCB300F41790 /* Network.swift */; };
1AD96320228D24FB00684FBB /* SystemScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD9631F228D24FB00684FBB /* SystemScript.swift */; };
1AE7C62C2251BCA9003FA254 /* Witness.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AE7C62B2251BCA9003FA254 /* Witness.swift */; };
2D6EA000E9B8936CB81356CC /* Pods_CKB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6F4BD554CBEED5D384A1C737 /* Pods_CKB.framework */; };
C5FC62F40419407D3FC2BB33 /* Pods_CKBTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FD2179DE52A415B08B8D134 /* Pods_CKBTests.framework */; };
D5B02A91230A8030006B4189 /* CellDep.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B02A90230A8030006B4189 /* CellDep.swift */; };
Expand Down Expand Up @@ -186,7 +184,6 @@
1A414AA421C7713800B28C09 /* UtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = "<group>"; };
1A4A4BAA22A6BF40003DC827 /* CellOutputTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellOutputTests.swift; sourceTree = "<group>"; };
1A4A4BAC22A6C154003DC827 /* CellInputTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellInputTests.swift; sourceTree = "<group>"; };
1A4A4BAE22A6C2EB003DC827 /* WitnessTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WitnessTests.swift; sourceTree = "<group>"; };
1A4A4BB022A6C445003DC827 /* TransactionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionTests.swift; sourceTree = "<group>"; };
1A4A4BB222A6C5F0003DC827 /* APIErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIErrorTests.swift; sourceTree = "<group>"; };
1A4A4BB422A6C64B003DC827 /* APIError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -258,7 +255,6 @@
1AD31960225DBD8A00F41790 /* AddressGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressGeneratorTests.swift; sourceTree = "<group>"; };
1AD9631F228D24FB00684FBB /* SystemScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemScript.swift; sourceTree = "<group>"; };
1AD96321228D2BF400684FBB /* SystemScriptTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemScriptTests.swift; sourceTree = "<group>"; };
1AE7C62B2251BCA9003FA254 /* Witness.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Witness.swift; sourceTree = "<group>"; };
3280C4848C7A8236CA5F4CD8 /* Pods-CKBTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKBTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CKBTests/Pods-CKBTests.debug.xcconfig"; sourceTree = "<group>"; };
5CAEE4335089142E8F3ECC55 /* Pods-CKBTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKBTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CKBTests/Pods-CKBTests.release.xcconfig"; sourceTree = "<group>"; };
5CD9DB4E09DDB6450D81BC6F /* Pods-CKB.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKB.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CKB/Pods-CKB.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -298,7 +294,6 @@
1A28119621D3805800E7CEC5 /* ScriptTests.swift */,
1A4A4BAC22A6C154003DC827 /* CellInputTests.swift */,
1A4A4BAA22A6BF40003DC827 /* CellOutputTests.swift */,
1A4A4BAE22A6C2EB003DC827 /* WitnessTests.swift */,
1A4A4BB022A6C445003DC827 /* TransactionTests.swift */,
);
path = Types;
Expand Down Expand Up @@ -451,7 +446,6 @@
1A414A8D21C3722300B28C09 /* CellInput.swift */,
1A414A8F21C3723E00B28C09 /* CellOutput.swift */,
D5B02A90230A8030006B4189 /* CellDep.swift */,
1AE7C62B2251BCA9003FA254 /* Witness.swift */,
1A414A8721C36FCF00B28C09 /* Header.swift */,
1A414A8921C36FE900B28C09 /* Block.swift */,
1A2CDAE0228171180024CA71 /* Epoch.swift */,
Expand Down Expand Up @@ -854,7 +848,6 @@
1A703C2F231FAF3600EDF2C6 /* DynVecSerializerTests.swift in Sources */,
1A703C43231FB1AA00EDF2C6 /* UnionSerializerTests.swift in Sources */,
1A2D6B3D22A35C6400724E66 /* Blake2bTests.swift in Sources */,
1A4A4BAF22A6C2EB003DC827 /* WitnessTests.swift in Sources */,
1A4A4BAD22A6C154003DC827 /* CellInputTests.swift in Sources */,
1A4A4BB122A6C445003DC827 /* TransactionTests.swift in Sources */,
1A2D6B3C22A35C6400724E66 /* Secp256k1Tests.swift in Sources */,
Expand Down Expand Up @@ -906,7 +899,6 @@
1A85454F23212A4100B26DC0 /* ByteSerializer.swift in Sources */,
1AB7517022BA263C00AC9F63 /* APIClient+Indexer.swift in Sources */,
1A2F5FBB23223DF1000051CC /* TransactionSerializer.swift in Sources */,
1AE7C62C2251BCA9003FA254 /* Witness.swift in Sources */,
1A199DEB22BA3225000C0C34 /* LockHashIndexState.swift in Sources */,
1A36096B2322853F000E67EE /* CellOutputSerializer.swift in Sources */,
1A414A9621C37AA000B28C09 /* Script.swift in Sources */,
Expand Down Expand Up @@ -1144,7 +1136,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.21.2;
MARKETING_VERSION = 0.22.0;
PRODUCT_BUNDLE_IDENTIFIER = org.nervos.CKB;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = macosx;
Expand Down Expand Up @@ -1175,7 +1167,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.21.2;
MARKETING_VERSION = 0.22.0;
PRODUCT_BUNDLE_IDENTIFIER = org.nervos.CKB;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = macosx;
Expand Down
2 changes: 1 addition & 1 deletion Source/Payment/Payment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private extension Payment {
inputs: inputs,
outputs: outputs,
outputsData: outputs.map { _ in "0x" },
witnesses: inputs.map { _ in Witness(data: ["0x"]) }
witnesses: inputs.map { _ in "0x" }
)
}

Expand Down
6 changes: 1 addition & 5 deletions Source/Serialization/TypeSerializers/ScriptSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ extension ScriptHashType {

public final class ScriptSerializer: TableSerializer<Script> {
public required init(value: Script) {
let normalizedArgs: [[Byte]] = value.args.map { (arg) in
// TODO: check if Data(hex: arg) needs to left pad arg string
return Data(hex: arg).bytes
}
super.init(
value: value,
fieldSerializers: [
Byte32Serializer(value: value.codeHash)!,
ByteSerializer(value: value.hashType.byte),
DynVecSerializer<[Byte], FixVecSerializer<Byte, ByteSerializer>>(value: normalizedArgs)
FixVecSerializer<Byte, ByteSerializer>(value: Data(hex: value.args).bytes)
]
)
}
Expand Down
7 changes: 3 additions & 4 deletions Source/Signers/Transaction+Sign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ public extension Transaction {

let txHash: H256 = tx.computeHash()

let signedWitnesses = try tx.witnesses.map { witness -> Witness in
let message: Data = ([txHash] + witness.data).map { Data(hex: $0) }.reduce(Data(), +)
let signedWitnesses = try tx.witnesses.map { witness -> HexString in
let message: Data = [txHash, witness].map { Data(hex: $0) }.reduce(Data(), +)
guard let messageHash = Blake2b().hash(data: message) else {
throw Error.failToHashWitnessesData
}
guard let signature = Secp256k1.signRecoverable(privateKey: privateKey, data: messageHash) else {
throw Error.failToSignWitnessesData
}
let data = [ signature.toHexString() ] + witness.data
return Witness(data: data.map { Utils.prefixHex($0) })
return Utils.prefixHex(signature.toHexString()) + Utils.removeHexPrefix(witness)
}

return Transaction(
Expand Down
6 changes: 3 additions & 3 deletions Source/Types/Epoch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public struct Epoch: Codable {
public let number: EpochNumber
public let startNumber: BlockNumber
public let length: BlockNumber
public let difficulty: HexNumber
public let compactTarget: UInt32

enum CodingKeys: String, CodingKey {
case number
case startNumber = "start_number"
case length
case difficulty
case compactTarget = "compact_target"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
number = EpochNumber(hexString: try container.decode(String.self, forKey: .number))!
startNumber = BlockNumber(hexString: try container.decode(String.self, forKey: .startNumber))!
length = BlockNumber(hexString: try container.decode(String.self, forKey: .length))!
difficulty = try container.decode(HexNumber.self, forKey: .difficulty)
compactTarget = UInt32(hexString: try container.decode(String.self, forKey: .compactTarget))!
}
}
16 changes: 5 additions & 11 deletions Source/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ public struct Header: Codable {
public let epoch: EpochNumber
public let transactionsRoot: H256
public let proposalsHash: H256
public let witnessesRoot: H256
public let difficulty: HexNumber
public let unclesHash: H256
public let unclesCount: UInt32
public let dao: String
public let nonce: UInt64
public let nonce: String // 128 bits
public let compactTarget: UInt32
public let hash: H256

enum CodingKeys: String, CodingKey {
Expand All @@ -30,12 +28,10 @@ public struct Header: Codable {
case epoch
case transactionsRoot = "transactions_root"
case proposalsHash = "proposals_hash"
case witnessesRoot = "witnesses_root"
case difficulty
case unclesHash = "uncles_hash"
case unclesCount = "uncles_count"
case dao
case nonce
case compactTarget = "compact_target"
case hash
}

Expand All @@ -48,12 +44,10 @@ public struct Header: Codable {
epoch = EpochNumber(hexString: try container.decode(String.self, forKey: .epoch))!
transactionsRoot = try container.decode(H256.self, forKey: .transactionsRoot)
proposalsHash = try container.decode(H256.self, forKey: .proposalsHash)
witnessesRoot = try container.decode(H256.self, forKey: .witnessesRoot)
difficulty = try container.decode(HexString.self, forKey: .difficulty)
unclesHash = try container.decode(H256.self, forKey: .unclesHash)
unclesCount = UInt32(hexString: try container.decode(String.self, forKey: .unclesCount))!
dao = try container.decode(String.self, forKey: .dao)
nonce = UInt64(hexString: try container.decode(String.self, forKey: .nonce))!
nonce = try container.decode(String.self, forKey: .nonce)
compactTarget = UInt32(hexString: try container.decode(String.self, forKey: .compactTarget))!
hash = try container.decode(H256.self, forKey: .hash)
}
}
4 changes: 2 additions & 2 deletions Source/Types/Script.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum ScriptHashType: String, Codable {
public struct Script: Codable, Param {
public let codeHash: H256
public let hashType: ScriptHashType
public let args: [HexString]
public let args: HexString

enum CodingKeys: String, CodingKey {
case codeHash = "code_hash"
Expand All @@ -30,7 +30,7 @@ public struct Script: Codable, Param {
]
}

public init(args: [HexString] = [], codeHash: H256 = H256.zeroHash, hashType: ScriptHashType = .data) {
public init(args: HexString = "0x", codeHash: H256 = H256.zeroHash, hashType: ScriptHashType = .data) {
self.codeHash = Utils.prefixHex(codeHash)
self.hashType = hashType
self.args = args
Expand Down
8 changes: 4 additions & 4 deletions Source/Types/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct Transaction: Codable, Param {
public let inputs: [CellInput]
public let outputs: [CellOutput]
public let outputsData: [HexString]
public let witnesses: [Witness]
public let witnesses: [HexString]
public let hash: H256

public init(
Expand All @@ -23,7 +23,7 @@ public struct Transaction: Codable, Param {
inputs: [CellInput] = [],
outputs: [CellOutput] = [],
outputsData: [HexString] = [],
witnesses: [Witness] = [],
witnesses: [HexString] = [],
hash: H256 = ""
) {
self.version = version
Expand Down Expand Up @@ -55,7 +55,7 @@ public struct Transaction: Codable, Param {
inputs = try container.decode([CellInput].self, forKey: .inputs)
outputs = try container.decode([CellOutput].self, forKey: .outputs)
outputsData = try container.decode([HexString].self, forKey: .outputsData)
witnesses = try container.decode([Witness].self, forKey: .witnesses)
witnesses = try container.decode([HexString].self, forKey: .witnesses)
hash = try container.decode(H256.self, forKey: .hash)
}

Expand All @@ -67,7 +67,7 @@ public struct Transaction: Codable, Param {
CodingKeys.inputs.rawValue: inputs.map { $0.param },
CodingKeys.outputs.rawValue: outputs.map { $0.param },
CodingKeys.outputsData.rawValue: outputsData,
CodingKeys.witnesses.rawValue: witnesses.map { $0.param }
CodingKeys.witnesses.rawValue: witnesses
]
}
}
Expand Down
21 changes: 0 additions & 21 deletions Source/Types/Witness.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Source/Utils/SystemScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public struct SystemScript {
}

public func lock(for publicKeyHash: String) -> Script {
return Script(args: [Utils.prefixHex(publicKeyHash)], codeHash: secp256k1TypeHash, hashType: .type)
return Script(args: Utils.prefixHex(publicKeyHash), codeHash: secp256k1TypeHash, hashType: .type)
}
}
4 changes: 4 additions & 0 deletions Source/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ extension Utils {
public static func prefixHex(_ string: String) -> String {
return string.hasPrefix("0x") ? string : "0x" + string
}

public static func removeHexPrefix(_ string: String) -> String {
return string.hasPrefix("0x") ? String(string.dropFirst(2)) : string
}
}
14 changes: 7 additions & 7 deletions Tests/API/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,22 @@ class APIClientTests: RPCTestSkippable {

func testComputeTransactionHash() throws {
let tx = Transaction(
cellDeps: [CellDep(outPoint: OutPoint(txHash: "0x29f94532fb6c7a17f13bcde5adb6e2921776ee6f357adf645e5393bd13442141", index: 0), depType: .code)],
headerDeps: ["0xeca4e06e75df81c0247365f864a08c7ef0eec8a5c7d182a25e6c086408a97cd2"],
inputs: [CellInput(previousOutput: OutPoint(txHash: "0x5ba156200c6310bf140fbbd3bfe7e8f03d4d5f82b612c1a8ec2501826eaabc17", index: 0), since: 0)],
outputs: [CellOutput(capacity: 100000000000, lock: Script(args: [], codeHash: "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", hashType: .data))],
cellDeps: [CellDep(outPoint: OutPoint(txHash: "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3", index: 0), depType: .code)],
headerDeps: ["0x3ed784b863bc13dbff3f49f4efee16fd0f5b764af7707ab81ae738b7f8475846"],
inputs: [CellInput(previousOutput: OutPoint(txHash: "0x5169e6406ebed886ea1be802da474e3a46922556f06b1d88b23613f55630fcb4", index: 0), since: 0)],
outputs: [CellOutput(capacity: 100000000000, lock: Script(args: "0x", codeHash: "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", hashType: .data))],
outputsData: ["0x"]
)
let result = try client.computeTransactionHash(transaction: tx)
XCTAssertNotNil(result)
XCTAssertEqual("0x13ebb4a177fbbbef800f9988cc1763d313cbe76c3aed3f15c6fa93b723d1a070", result)
XCTAssertEqual("0xac963a60263aeb26f3089f0caa58fc081c87f6eb0b8483d840869ae8f34e9559", result)
}

func testComputeScriptHash() throws {
let script = Script(args: [], codeHash: "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", hashType: .data)
let script = Script(args: "0x", codeHash: "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", hashType: .data)
let result = try client.computeScriptHash(script: script)
XCTAssertNotNil(result)
XCTAssertEqual("0xd8753dd87c7dd293d9b64d4ca20d77bb8e5f2d92bf08234b026e2d8b1b00e7e9", result)
XCTAssertEqual("0x4ceaa32f692948413e213ce6f3a83337145bde6e11fd8cb94377ce2637dcc412", result)
}

func testDryRunTransaction() throws {
Expand Down
Loading

0 comments on commit 49a2838

Please sign in to comment.