Skip to content

Commit

Permalink
Add stealth address sending support
Browse files Browse the repository at this point in the history
  • Loading branch information
SwenVanZanten committed Mar 7, 2019
1 parent 362cc92 commit b581261
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
12 changes: 9 additions & 3 deletions VergeiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
A33B759E21A5CDD400030CCF /* BitcoinKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A33B759D21A5CDD400030CCF /* BitcoinKit.framework */; };
A33B75C021A61A3500030CCF /* CurrencyInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = A33B75BF21A61A3500030CCF /* CurrencyInput.swift */; };
A33B75C421A8917700030CCF /* Settings.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A33B75C321A8917700030CCF /* Settings.storyboard */; };
A33DB6952231D2010070DBCE /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A33DB6942231D2010070DBCE /* Array+Extensions.swift */; };
A33F6A7020EEE485001492C2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A33F6A6F20EEE485001492C2 /* AppDelegate.swift */; };
A33F6A7220EEE485001492C2 /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A33F6A7120EEE485001492C2 /* WelcomeViewController.swift */; };
A33F6A7520EEE485001492C2 /* WelcomeGuide.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A33F6A7320EEE485001492C2 /* WelcomeGuide.storyboard */; };
Expand Down Expand Up @@ -417,6 +418,7 @@
A33B759D21A5CDD400030CCF /* BitcoinKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = BitcoinKit.framework; path = Carthage/Build/iOS/BitcoinKit.framework; sourceTree = "<group>"; };
A33B75BF21A61A3500030CCF /* CurrencyInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyInput.swift; sourceTree = "<group>"; };
A33B75C321A8917700030CCF /* Settings.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Settings.storyboard; sourceTree = "<group>"; };
A33DB6942231D2010070DBCE /* Array+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Extensions.swift"; sourceTree = "<group>"; };
A33F6A6C20EEE485001492C2 /* VergeiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = VergeiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
A33F6A6F20EEE485001492C2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
A33F6A7120EEE485001492C2 /* WelcomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -680,6 +682,7 @@
isa = PBXGroup;
children = (
B2BD7F498F12AE2C1C892E66 /* AppDelegate+NotificationCenter.swift */,
A33DB6942231D2010070DBCE /* Array+Extensions.swift */,
B2BD74E5640073CDD7246E24 /* Collection+Extensions.swift */,
B2BD7E6BDB05995D9DB9E0D0 /* Data+Extensions.swift */,
A3408E5721768AF4009F0C9D /* Date+Extensions.swift */,
Expand All @@ -689,8 +692,8 @@
A36F8A882146EE0F00304777 /* String+Subs.swift */,
B2BD7945C26A344BBFEAB261 /* UIAlertController+Statics.swift */,
A302D34C21260F1C00EA8091 /* UIScrollView+Pagination.swift */,
A398B20E212B2886004ACB73 /* UIWindow+VisibleViewController.swift */,
CABF1481221061FD00F92CBC /* UIView+Actions.swift */,
A398B20E212B2886004ACB73 /* UIWindow+VisibleViewController.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1674,6 +1677,7 @@
A305DDB22107A1F100911B64 /* PinKeyboard.swift in Sources */,
A316B0AF215454FB00FF5200 /* AddressValidator.swift in Sources */,
A305DDBA2107BB8B00911B64 /* KeyboardDelegate.swift in Sources */,
A33DB6952231D2010070DBCE /* Array+Extensions.swift in Sources */,
A338E6B72110D5D4000D44EE /* TransactionsWalletSlideView.swift in Sources */,
A302D34321220A1E00EA8091 /* Notification+Names.swift in Sources */,
A338E6A2210DD893000D44EE /* FinishSetupViewController.swift in Sources */,
Expand Down Expand Up @@ -1946,6 +1950,7 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CF_BUNDLE_SHORT_VERSION_STRING = 0.17.1;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = S7743EWR3C;
INFOPLIST_FILE = VergeiOSTests/Info.plist;
Expand All @@ -1966,6 +1971,7 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CF_BUNDLE_SHORT_VERSION_STRING = 0.17.1;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = S7743EWR3C;
INFOPLIST_FILE = VergeiOSTests/Info.plist;
Expand Down Expand Up @@ -2024,7 +2030,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CF_BUNDLE_SHORT_VERSION_STRING = 0.17;
CF_BUNDLE_SHORT_VERSION_STRING = 0.17.1;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
Expand Down Expand Up @@ -2087,7 +2093,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CF_BUNDLE_SHORT_VERSION_STRING = 0.17;
CF_BUNDLE_SHORT_VERSION_STRING = 0.17.1;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
Expand Down
23 changes: 23 additions & 0 deletions VergeiOS/Extensions/Array+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Array+Extensions.swift
// VergeiOS
//
// Created by Swen van Zanten on 07/03/2019.
// Copyright © 2019 Verge Currency. All rights reserved.
//

import Foundation

extension Array {
func sortByIndices(indices: [Int]) -> Array {
var array: Array<Element> = []

for index in indices {
if self.indices.contains(index) {
array.append(self[index])
}
}

return array
}
}
4 changes: 4 additions & 0 deletions VergeiOS/Http/TorClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class TorClient {
func restart() {
resign()

if !isOperational {
return
}

while controller.isConnected {
print("Disconnecting Tor...")
}
Expand Down
2 changes: 2 additions & 0 deletions VergeiOS/Models/TxOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public struct TxOutput: Decodable {
public let message: String?
public let encryptedMessage: String?
public let toAddress: String
public let ephemeralPrivKey: String?
public let stealth: Bool?

}

Expand Down
2 changes: 2 additions & 0 deletions VergeiOS/Wallet/TxTransponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class TxTransponder {

public func send(txp: TxProposalResponse, completion: @escaping completionType) {
self.completion = completion
self.step = .publish
self.previousTxp = nil

// Publish the tx proposal and start the sequence.
walletClient.publishTxProposal(txp: txp, completion: completionHandler)
Expand Down
37 changes: 24 additions & 13 deletions VergeiOS/Wallet/WalletClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class WalletClient {
case invalidMessageData(message: String)
case invalidWidHex(id: String)
case invalidAddressReceived(address: AddressInfo?)
case noOutputFound()
}

public static let shared = WalletClient(
Expand Down Expand Up @@ -609,8 +610,12 @@ public class WalletClient {
}

private func getUnsignedTx(txp: TxProposalResponse) throws -> UnsignedTransaction {
guard let output = txp.outputs.first else {
throw WalletClientError.noOutputFound()
}

let changeAddress: Address = try AddressFactory.create(txp.changeAddress.address)
let toAddress: Address = try getToAddress(address: txp.outputs.first!.toAddress)
let toAddress: Address = try AddressFactory.create(output.toAddress)

let unspentOutputs = txp.inputs
let unspentTransactions: [UnspentTransaction] = try unspentOutputs.map { output in
Expand All @@ -637,11 +642,25 @@ public class WalletClient {

var outputs = [toOutput]
if change > 0 {
outputs = [toOutput, changeOutput]
if txp.outputOrder == [1, 0] {
outputs = [changeOutput, toOutput]
}
outputs.append(changeOutput)
}

if output.stealth == true {
let ephemeral = PrivateKey(
data: Data(hex: output.ephemeralPrivKey!),
network: .mainnetXVG,
isPublicKeyCompressed: true
)

let opReturnMeta = try Script()
.append(.OP_RETURN)
.appendData(ephemeral.publicKey().data)

let opReturnOutput = TransactionOutput(value: 0, lockingScript: opReturnMeta.data)
outputs.insert(opReturnOutput, at: 0)
}

outputs = outputs.sortByIndices(indices: txp.outputOrder.map { Int($0) })

let tx = Transaction(
version: 1,
Expand Down Expand Up @@ -692,12 +711,4 @@ public class WalletClient {

return hexes
}

private func getToAddress(address: String) throws -> Address {
do {
return try AddressFactory.create(address)
} catch {
return try StealthAddress(address)
}
}
}

0 comments on commit b581261

Please sign in to comment.