From 37b5520c922636e42e4ffd68f43d75123d86913d Mon Sep 17 00:00:00 2001 From: "daiki.sekiguchi" Date: Sun, 19 Aug 2018 11:45:07 +0900 Subject: [PATCH] =?UTF-8?q?[update]=20OP=5FRETURN=E3=82=92JSON=E5=8C=96?= =?UTF-8?q?=E3=80=82detail=20view=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleWallet.xcodeproj/project.pbxproj | 4 ++++ SimpleWallet/Modules/Models/Vote.swift | 23 +++++++++++++++++++ .../DetailView/DetailViewController.swift | 20 ++++++++++++---- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 SimpleWallet/Modules/Models/Vote.swift diff --git a/SimpleWallet.xcodeproj/project.pbxproj b/SimpleWallet.xcodeproj/project.pbxproj index 481e32f..94d6d9b 100644 --- a/SimpleWallet.xcodeproj/project.pbxproj +++ b/SimpleWallet.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 09B7BF5821288DB100D1232D /* AdminViewController.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 09B7BF5721288DB100D1232D /* AdminViewController.storyboard */; }; 09B7BF5A2128926800D1232D /* Constant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09B7BF592128926800D1232D /* Constant.swift */; }; 09B7BF5C212892E600D1232D /* BCHHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09B7BF5B212892E600D1232D /* BCHHelper.swift */; }; + 09B7BF5F21290DE200D1232D /* Vote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09B7BF5E21290DE200D1232D /* Vote.swift */; }; 0C26AB7B2126C59E00170732 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C26AB7A2126C59E00170732 /* AppDelegate.swift */; }; 0C26AB822126C59F00170732 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C26AB812126C59F00170732 /* Assets.xcassets */; }; 0C26AB852126C59F00170732 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0C26AB832126C59F00170732 /* LaunchScreen.storyboard */; }; @@ -52,6 +53,7 @@ 09B7BF5721288DB100D1232D /* AdminViewController.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = AdminViewController.storyboard; sourceTree = ""; }; 09B7BF592128926800D1232D /* Constant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constant.swift; sourceTree = ""; }; 09B7BF5B212892E600D1232D /* BCHHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BCHHelper.swift; sourceTree = ""; }; + 09B7BF5E21290DE200D1232D /* Vote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vote.swift; sourceTree = ""; }; 0C26AB772126C59E00170732 /* SimpleWallet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SimpleWallet.app; sourceTree = BUILT_PRODUCTS_DIR; }; 0C26AB7A2126C59E00170732 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 0C26AB812126C59F00170732 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -191,6 +193,7 @@ 0C26AB932126C5BB00170732 /* Transaction.swift */, 0C26AB8D2126C5BB00170732 /* UnspentOutput.swift */, 6E61F59F2128595B007C696D /* Post.swift */, + 09B7BF5E21290DE200D1232D /* Vote.swift */, ); path = Models; sourceTree = ""; @@ -457,6 +460,7 @@ 0C26AB9A2126C5BB00170732 /* HomeViewController.swift in Sources */, A04EA51521284A5C00709549 /* AddChoiceViewController.swift in Sources */, 0C26AB952126C5BB00170732 /* Extension.swift in Sources */, + 09B7BF5F21290DE200D1232D /* Vote.swift in Sources */, 6E5E3C8021284F87005042F2 /* WalletViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SimpleWallet/Modules/Models/Vote.swift b/SimpleWallet/Modules/Models/Vote.swift new file mode 100644 index 0000000..627e0ae --- /dev/null +++ b/SimpleWallet/Modules/Models/Vote.swift @@ -0,0 +1,23 @@ +// +// Vote.swift +// SimpleWallet +// +// Created by Daiki Sekiguchi on 2018/08/19. +// Copyright © 2018年 Akifumi Fujita. All rights reserved. +// + +import Foundation + +struct Vote: Codable { + let hash: String + let message: String + + func toJson() -> String { + let encoder = JSONEncoder() + let data = try! encoder.encode(self) + let json: String = String(data: data, encoding: .utf8)! + + return json + } +} + diff --git a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift index a853c89..f56e127 100644 --- a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift +++ b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift @@ -39,10 +39,10 @@ class DetailViewController: UIViewController { // 運営と投票される側でマルチシグを行う let multisig: Address = BCHHelper().createMultisigAddress(adminPubKey: Constant.adminPubKey, targetPubKey: post.choices[0].pubKey) //決め打ち - sendCoins(toAddress: multisig, amount: Constant.voteAmount, comment: commentText.text!) + sendCoins(toAddress: multisig, amount: Constant.voteAmount) } - private func sendCoins(toAddress: Address, amount: Int64, comment: String) { + private func sendCoins(toAddress: Address, amount: Int64) { // おつり用のアドレスを決める let changeAddress: Address = AppController.shared.wallet!.publicKey.toCashaddr() @@ -53,7 +53,7 @@ class DetailViewController: UIViewController { 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 @@ -69,7 +69,7 @@ class DetailViewController: 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 { // UTXOの選択 let (utxos, fee) = BCHHelper().selectTx(from: utxos) let totalAmount: Int64 = utxos.reduce(0) { $0 + $1.output.value } @@ -98,12 +98,20 @@ class DetailViewController: UIViewController { .append(.OP_EQUALVERIFY) .append(.OP_CHECKSIG) + // TODO: hash値検討 + let opReturnJson = Vote(hash: "hoge", message: commentText.text!) + + let opReturnScript = try! Script() + .append(.OP_RETURN) + .appendData(opReturnJson.toJson().data(using: .utf8)!) + let toOutput = TransactionOutput(value: amount, lockingScript: lockScriptTo.data) let changeOutput = TransactionOutput(value: change, lockingScript: lockScriptChange.data) + let opReturnOutput = TransactionOutput(value: 0, lockingScript: opReturnScript.data) // UTXOとTransactionOutputを合わせて、UnsignedTransactionを作る let unsignedInputs = utxos.map { TransactionInput(previousOutput: $0.outpoint, signatureScript: Data(), sequence: UInt32.max) } - let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: [toOutput, changeOutput], lockTime: 0) + let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: [toOutput, changeOutput, opReturnOutput], lockTime: 0) return UnsignedTransaction(tx: tx, utxos: utxos) } @@ -150,6 +158,8 @@ class DetailViewController: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) + // 暫定で locktime をセット + // 本来は選挙時間をセットする lockTimeString = "2018-08-20 18:00:00" }