Skip to content

Commit

Permalink
[update] OP_RETURNをJSON化。detail viewリファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
daiki.sekiguchi committed Aug 19, 2018
1 parent ae7471a commit 37b5520
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions SimpleWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -52,6 +53,7 @@
09B7BF5721288DB100D1232D /* AdminViewController.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = AdminViewController.storyboard; sourceTree = "<group>"; };
09B7BF592128926800D1232D /* Constant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constant.swift; sourceTree = "<group>"; };
09B7BF5B212892E600D1232D /* BCHHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BCHHelper.swift; sourceTree = "<group>"; };
09B7BF5E21290DE200D1232D /* Vote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vote.swift; sourceTree = "<group>"; };
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 = "<group>"; };
0C26AB812126C59F00170732 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -191,6 +193,7 @@
0C26AB932126C5BB00170732 /* Transaction.swift */,
0C26AB8D2126C5BB00170732 /* UnspentOutput.swift */,
6E61F59F2128595B007C696D /* Post.swift */,
09B7BF5E21290DE200D1232D /* Vote.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions SimpleWallet/Modules/Models/Vote.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand All @@ -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 }
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -150,6 +158,8 @@ class DetailViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)

// 暫定で locktime をセット
// 本来は選挙時間をセットする
lockTimeString = "2018-08-20 18:00:00"
}

Expand Down

0 comments on commit 37b5520

Please sign in to comment.