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

Commit

Permalink
feat: Add public key hash to lock script util
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Sep 3, 2019
1 parent 8dd1844 commit c4096d2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Examples/Example-iOS/Example-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ private extension ViewController {

// Generate lock script for the receiver's address
let toAddress = "ckt..."
let addressHash = Utils.prefixHex(AddressGenerator(network: .testnet).publicKeyHash(for: toAddress)!)
let lockScript = Script(args: [addressHash], codeHash: systemScript.secp256k1TypeHash, hashType: .type)
let publicKeyHash = Utils.prefixHex(AddressGenerator(network: .testnet).publicKeyHash(for: toAddress)!)
let lockScript = systemScript.lock(for: publicKeyHash)
// Construct the outputs
let outputs = [CellOutput(capacity: 500_00_000_000.description, lock: lockScript, type: nil)]

// Generate the transaction
let tx = Transaction(cellDeps: deps, inputs: inputs, outputs: outputs, witnesses: [Witness(data: [])])
let tx = Transaction(cellDeps: deps, inputs: inputs, outputs: outputs, outputsData: ["0x"], witnesses: [Witness(data: [])])
// For now we need to call the `computeTransactionHash` to get the tx hash
let apiClient = APIClient(url: nodeUrl)
let txHash = try apiClient.computeTransactionHash(transaction: tx)
let signedTx = try Transaction.sign(tx: tx, with: privateKey, txHash: txHash)

// Now send out the capacity
let hash = try apiClient.sendTransaction(transaction: signedTx)
print(hash) // hash should equal to txHash
print(hash) // hash should be equal to txHash
}
}
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ print(height) // "10420"
### Send Capacity Example

```swift
// Fetch system script which we'll use to generate lock for address
let systemScript = try SystemScript.loadSystemScript(nodeUrl: nodeUrl)
// Fill in the sender's private key
let privateKey: Data = Data(hex: "your private key (hex string)")
Expand All @@ -80,21 +81,21 @@ let inputs: [CellInput] = [/*...*/]

// Generate lock script for the receiver's address
let toAddress = "ckt..."
let addressHash = Utils.prefixHex(AddressGenerator(network: .testnet).publicKeyHash(for: toAddress)!)
let lockScript = Script(args: [addressHash], codeHash: systemScript.secp256k1TypeHash, hashType: .type)
let publicKeyHash = Utils.prefixHex(AddressGenerator(network: .testnet).publicKeyHash(for: toAddress)!)
let lockScript = systemScript.lock(for: publicKeyHash)
// Construct the outputs
let outputs = [CellOutput(capacity: 500_00_000_000.description, lock: lockScript, type: nil)]

// Generate the transaction
let tx = Transaction(cellDeps: deps, inputs: inputs, outputs: outputs, witnesses: [Witness(data: [])])
let tx = Transaction(cellDeps: deps, inputs: inputs, outputs: outputs, outputsData: ["0x"], witnesses: [Witness(data: [])])
// For now we need to call the `computeTransactionHash` to get the tx hash
let apiClient = APIClient(url: nodeUrl)
let txHash = try apiClient.computeTransactionHash(transaction: tx)
let signedTx = try Transaction.sign(tx: tx, with: privateKey, txHash: txHash)

// Now send out the capacity
let hash = try apiClient.sendTransaction(transaction: signedTx)
print(hash) // hash should equal to txHash
print(hash) // hash should be equal to txHash
```

## Getting Help
Expand Down
10 changes: 7 additions & 3 deletions Source/Utils/SystemScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public struct SystemScript {
return SystemScript(depOutPoint: depOutPoint, secp256k1TypeHash: secp256k1TypeHash)
}

public func lock(for publicKey: String) -> Script {
let pubkeyHash = Utils.prefixHex(AddressGenerator().hash(for: Data(hex: publicKey)).toHexString())
return Script(args: [pubkeyHash], codeHash: secp256k1TypeHash, hashType: .type)
public func lock(for publicKey: Data) -> Script {
let publicKeyHash = Utils.prefixHex(AddressGenerator().hash(for: publicKey).toHexString())
return lock(for: publicKeyHash)
}

public func lock(for publicKeyHash: String) -> Script {
return Script(args: [publicKeyHash], codeHash: secp256k1TypeHash, hashType: .type)
}
}
9 changes: 8 additions & 1 deletion Tests/Utils/SystemScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ class SystemScriptTests: RPCTestSkippable {
let privateKey = Data(hex: "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3")
let publicKey = Utils.privateToPublic(privateKey)
let systemScript = try SystemScript.loadSystemScript(nodeUrl: APIClient.defaultLocalURL)
let lockScript = systemScript.lock(for: publicKey.toHexString())
let lockScript = systemScript.lock(for: publicKey)
XCTAssertEqual("0x024b0fd0c4912e98aab6808f6474cacb1969255d526b3cac5d3bdd15962a8818", lockScript.hash)
}

func x_testPublicKeyHashToScript() throws {
let systemScript = try SystemScript.loadSystemScript(nodeUrl: APIClient.defaultLocalURL)
let publicKeyHash = "0x36c329ed630d6ce750712a477543672adab57f4c"
let lockScript = systemScript.lock(for: publicKeyHash)
XCTAssertEqual("0x024b0fd0c4912e98aab6808f6474cacb1969255d526b3cac5d3bdd15962a8818", lockScript.hash)
}
}

0 comments on commit c4096d2

Please sign in to comment.