Skip to content

Commit

Permalink
[add] admin view
Browse files Browse the repository at this point in the history
  • Loading branch information
daiki.sekiguchi committed Aug 18, 2018
1 parent d3c1495 commit 5e17ef8
Showing 1 changed file with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ class AdminViewController: UIViewController {
@IBAction func executeButton(_ sender: Any) {
// 受け取った運営の使い方
// UTXO集める→識別子を見て適切なものだけを送金



// TODO: targetPubKey をちゃんとハメる
let multisig: Address = BCHHelper().createMultisigAddress(adminPubKey: Constant.adminPubKey, targetPubKey: Constant.adminPubKey)
sendCoins(toAddress: multisig, amount: Constant.voteAmount)
}

private func sendCoins(toAddress: Address, amount: Int64, comment: String) {
private var targetAddress: Address!

private func sendCoins(toAddress: Address, amount: Int64) {
// 1. おつり用のアドレスを決める
let changeAddress: Address = AppController.shared.wallet!.publicKey.toCashaddr()

// 2. UTXOの取得
// TODO: 値の入れ替え
// 運営者の公開鍵
let adminPubKey = Constant.adminPubKey
// 立候補者の公開鍵
let targetPubKey = try! Wallet(wif: "立候補者").publicKey
let legacyAddress: String = BCHHelper().createMultisigAddress(adminPubKey: adminPubKey, targetPubKey: targetPubKey) as! String
let legacyAddress: String = BCHHelper().createMultisigAddress(adminPubKey: Constant.adminPubKey, targetPubKey: targetPubKey) as! String
APIClient().getUnspentOutputs(withAddresses: [legacyAddress], completionHandler: { [weak self] (unspentOutputs: [UnspentOutput]) in
guard let strongSelf = self else {
return
}

let utxos = unspentOutputs.map { $0.asUnspentTransaction() }
let unsignedTx = strongSelf.createUnsignedTx(toAddress: toAddress, amount: amount, changeAddress: changeAddress, utxos: utxos, comment: comment)
let unsignedTx = strongSelf.createUnsignedTx(toAddress: toAddress, amount: amount, changeAddress: changeAddress, utxos: utxos)
let signedTx = strongSelf.signTx(unsignedTx: unsignedTx, keys: [AppController.shared.wallet!.privateKey])
let rawTx = signedTx.serialized().hex

Expand All @@ -56,7 +56,7 @@ class AdminViewController: UIViewController {
})
}

public func createUnsignedTx(toAddress: Address, amount: Int64, changeAddress: Address, utxos: [UnspentTransaction], comment: String) -> UnsignedTransaction {
public func createUnsignedTx(toAddress: Address, amount: Int64, changeAddress: Address, utxos: [UnspentTransaction]) -> UnsignedTransaction {
// 3. 送金に必要なUTXOの選択
let (utxos, fee) = BCHHelper().selectTx(from: utxos, amount: amount)
let totalAmount: Int64 = utxos.reduce(0) { $0 + $1.output.value }
Expand All @@ -68,14 +68,9 @@ class AdminViewController: UIViewController {
let toOutput = TransactionOutput(value: amount, lockingScript: lockScriptTo.data)
let changeOutput = TransactionOutput(value: change, lockingScript: lockScriptChange.data)

let opReturnScript = try! Script()
.append(.OP_RETURN)
.appendData(comment.data(using: .utf8)!)
let opReturnOutput = TransactionOutput(value: 0, lockingScript: opReturnScript.data)

// 5. UTXOとTransactionOutputを合わせて、UnsignedTransactionを作る
let unsignedInputs = utxos.map { TransactionInput(previousOutput: $0.outpoint, signatureScript: Data(), sequence: UInt32.max) }
let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: [opReturnOutput, toOutput, changeOutput], lockTime: 0)
let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: [toOutput, changeOutput], lockTime: 0)

return UnsignedTransaction(tx: tx, utxos: utxos)
}
Expand All @@ -90,24 +85,35 @@ class AdminViewController: UIViewController {
// Signing
let hashType = SighashType.BCH.ALL
for (i, utxo) in unsignedTx.utxos.enumerated() {
let pubkeyHash: Data = Script.getPublicKeyHash(from: utxo.output.lockingScript)
let targetWallet = try! Wallet(wif: "cP1uBo6EsiBayFLu3E5mst5eDg7KNGRJbndbckRfV14votPZu4oU")

let keysOfUtxo: [PrivateKey] = keys.filter { $0.publicKey().pubkeyHash == pubkeyHash }
guard let key = keysOfUtxo.first else {
continue
}
let adminPubKey = Constant.adminPubKey
// TODO: SingしたときのPubKeyを使わないと開かない
// TODO: 値を変更する必要がある
let targetPubKey = targetWallet.publicKey

let redeemScript = Script(publicKeys: [adminPubKey, targetPubKey], signaturesRequired: 2)!

// outputを作り直す
let output = TransactionOutput(value: utxo.output.value, lockingScript: redeemScript.data)

let sighash: Data = transactionToSign.signatureHash(for: utxo.output, inputIndex: i, hashType: SighashType.BCH.ALL)
let signature: Data = try! Crypto.sign(sighash, privateKey: key)
// 作り直したoutputをsighashを作るときに入れる
let sighash: Data = transactionToSign.signatureHash(for: output, inputIndex: i, hashType: SighashType.BCH.ALL)
let adminSig: Data = try! Crypto.sign(sighash, privateKey: AppController.shared.wallet!.privateKey)
let targetSig: Data = try! Crypto.sign(sighash, privateKey: targetWallet.privateKey)
let txin = inputsToSign[i]
let pubkey = key.publicKey()

// unlockScriptを作る
let unlockingScript = Script.buildPublicKeyUnlockingScript(signature: signature, pubkey: pubkey, hashType: hashType)
let unlockingScript = try! Script()
.append(.OP_0)
.appendData(adminSig + UInt8(hashType))
.appendData(targetSig + UInt8(hashType))
.appendData(redeemScript.data)
// 運営側が解除するときはTRUE
.append(.OP_TRUE)

// TODO: sequenceの更新
inputsToSign[i] = TransactionInput(previousOutput: txin.previousOutput, signatureScript: unlockingScript, sequence: txin.sequence)
inputsToSign[i] = TransactionInput(previousOutput: txin.previousOutput, signatureScript: unlockingScript.data, sequence: txin.sequence)
}

return transactionToSign
}

Expand All @@ -118,12 +124,9 @@ class AdminViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

0 comments on commit 5e17ef8

Please sign in to comment.