Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove network from Hotwallet #208

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ public struct HotAccount: Account {
Create Hot Account.

```swift
let account = HotAccount(network: .mainnetBeta)
let account = HotAccount()
```

Create Hot Account from the seed phrase.

```swift
let phrase12 = "miracle pizza supply useful steak border same again youth silver access hundred".components(separatedBy: " ")
let account12 = HotAccount(phrase: phrase12, network: .mainnetBeta)
let account12 = HotAccount(phrase: phrase12)
```

Create a Hot Account from bip32Deprecated("m/501'") seed phrase. Yes, we support Wallet Index and several accounts from the same Mnemonic. This is helpful for wallet creation.

```swift
let phrase24 = "hint begin crowd dolphin drive render finger above sponsor prize runway invest dizzy pony bitter trial ignore crop please industry hockey wire use side".components(separatedBy: " ")
let account24 = HotAccount(phrase: phrase24, network: .mainnetBeta, derivablePath: DerivablePath(
let account24 = HotAccount(phrase: phrase24, derivablePath: DerivablePath(
type: .bip32Deprecated,
walletIndex: 0,
accountIndex: 0
Expand All @@ -125,7 +125,7 @@ To create a new seed phrase only use `Mnemonic()`. It will create a 256 strength

```swift
let phrase= Mnemonic())
let account = HotAccount(phrase: phrase, network: .mainnetBeta)
let account = HotAccount(phrase: phrase)
```

## RPC API calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension Action {
return
}
// create new account for token
guard let newAccount = HotAccount(network: self.router.endpoint.network) else {
guard let newAccount = HotAccount() else {
onComplete(.failure(SolanaError.unauthorized))
return
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Solana/Actions/swap/swap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension Action {
var destination = destination

// add userTransferAuthority
guard let userTransferAuthority = HotAccount(network: self.router.endpoint.network) else {
guard let userTransferAuthority = HotAccount() else {
return .failure(SolanaError.other("Unsupported swapping tokens"))
}

Expand Down Expand Up @@ -217,7 +217,7 @@ extension Action {
signers: inout [Account],
minimumBalanceForRentExemption: UInt64
) -> Result<Account, Error> {
guard let newAccount = HotAccount(network: self.router.endpoint.network) else {
guard let newAccount = HotAccount() else {
return .failure(SolanaError.invalidRequest(reason: "Could not create new Account"))
}

Expand Down Expand Up @@ -258,7 +258,7 @@ extension Action {
signers: inout [Account],
minimumBalanceForRentExemption: UInt64
) -> Result<Account, Error> {
guard let newAccount = HotAccount(network: self.router.endpoint.network) else {
guard let newAccount = HotAccount() else {
return .failure(SolanaError.invalidRequest(reason: "Could not create new Account"))
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Solana/Models/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct HotAccount: Codable, Hashable, Account {
return data
}

public init?(phrase: [String] = [], network: Network, derivablePath: DerivablePath? = nil) {
public init?(phrase: [String] = [], derivablePath: DerivablePath? = nil) {
let mnemonic: Mnemonic
var phrase = phrase.filter {!$0.isEmpty}
if !phrase.isEmpty,
Expand All @@ -32,7 +32,7 @@ public struct HotAccount: Codable, Hashable, Account {

switch derivablePath.type {
case .bip32Deprecated:
guard let keychain = try? Keychain(seedString: phrase.joined(separator: " "), network: network.cluster) else {
guard let keychain = try? Keychain(seedString: phrase.joined(separator: " ")) else {
return nil
}
guard let seed = try? keychain.derivedKeychain(at: derivablePath.rawValue).privateKey else {
Expand Down
12 changes: 5 additions & 7 deletions Sources/Solana/Vendor/AskCoin-HD/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let BTCMasterKeychainPath = "m"
let BTCKeychainHardenedSymbol = "'"
let BTCKeychainPathSeparator = "/"

public class Keychain: NSObject {
class Keychain: NSObject {

enum KeyDerivationError: Error {
case indexInvalid
Expand All @@ -32,8 +32,7 @@ public class Keychain: NSObject {
private var chainCode: Data?

fileprivate var isMasterKey = false
var isTestnet = false


var depth: UInt8 = 0
var hardened = false
var index: UInt32 = 0
Expand All @@ -42,7 +41,7 @@ public class Keychain: NSObject {

}

public convenience init?(seedString: String, network: String) throws {
public convenience init?(seedString: String) throws {
guard let seedData = Mnemonic(phrase: seedString.components(separatedBy: " ")) else {
return nil
}
Expand All @@ -54,7 +53,6 @@ public class Keychain: NSObject {
}
self.init(hmac: hmac.bytes)
isMasterKey = true
isTestnet = network == "devnet" || network == "testnet"
}

public init(hmac: [UInt8]) {
Expand Down Expand Up @@ -97,7 +95,7 @@ public class Keychain: NSObject {

var toReturn = Data()

let version = !isTestnet ? BTCKeychainMainnetPrivateVersion : BTCKeychainTestnetPrivateVersion
let version = BTCKeychainMainnetPrivateVersion
toReturn += self.extendedKeyPrefix(with: version)

toReturn += UInt8(0).ask_hexToData()
Expand All @@ -121,7 +119,7 @@ public class Keychain: NSObject {

var toReturn = Data()

let version = !isTestnet ? BTCKeychainMainnetPublicVersion : BTCKeychainTestnetPublicVersion
let version = BTCKeychainMainnetPublicVersion
toReturn += self.extendedKeyPrefix(with: version)

if let pubkey = self.publicKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class closeTokenAccount: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solanaSDK = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testCloseAccountInstruction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class createAssociatedTokenAccount: XCTestCase {
let wallet: TestsWallet = .devnet
networkRouterMock = NetworkingRouterMock()
solana = Solana(router: networkRouterMock)
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

override func tearDownWithError() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class createAssociatedTokenAccountAsync: XCTestCase {
try super.setUpWithError()
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: .devnetSolana))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testGetOrCreateAssociatedTokenAccount() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class createTokenAccount: XCTestCase {
let wallet: TestsWallet = .devnet
networkRouterMock = NetworkingRouterMock()
solana = Solana(router: networkRouterMock)
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

override func tearDownWithError() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@ import Solana
@available(macOS 10.15, *)
class createTokenAccountAsync: XCTestCase {
var endpoint = RPCEndpoint.devnetSolana
var networkRouterMock: NetworkingRouterMock!
var solana: Solana!
var account: Account!

override func setUpWithError() throws {
try super.setUpWithError()
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
networkRouterMock = NetworkingRouterMock()
solana = Solana(router: networkRouterMock)
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testCreateTokenAccount() async throws {
// arrange
let mintAddress = "6AUM4fSvCAxCugrbJPFxTqYFp9r3axYx973yoSyzDYVH"
let account: (signature: String, newPubkey: String)? = try await solana.action.createTokenAccount( mintAddress: mintAddress, payer: account)
XCTAssertNotNil(account)
networkRouterMock.expectedResults.append(contentsOf: [
.success(.json(filename: "getRecentBlockhash")),
.success(.json(filename: "getMinimumBalanceForRentExemption")),
.success(.json(filename: "sendTransaction"))
])

// act
let account: (signature: String, newPubkey: String)? = try! await solana.action.createTokenAccount( mintAddress: mintAddress, payer: self.account)

// assert
XCTAssertEqual(networkRouterMock.requestCalled.count, 3)
XCTAssertEqual(account?.signature,
"TWui8GhF8vp8BtiGXhvxgti7LJGpVu7hx4tXzi3pqSycipNbJokDcFayw8a9YdcKJ789fSRjU6CRwPJ2zNp52eB")
XCTAssertNotNil(account?.newPubkey)
}

func testCreateAccountInstruction() {
let instruction = SystemProgram.createAccountInstruction(from: PublicKey.programId, toNewPubkey: PublicKey.systemProgramId, lamports: 2039280, space: 165, programPubkey: PublicKey.systemProgramId)
let instruction = SystemProgram.createAccountInstruction(from: PublicKey.systemProgramId, toNewPubkey: PublicKey.systemProgramId, lamports: 2039280, space: 165, programPubkey: PublicKey.systemProgramId)

XCTAssertEqual("11119os1e9qSs2u7TsThXqkBSRUo9x7kpbdqtNNbTeaxHGPdWbvoHsks9hpp6mb2ed1NeB", Base58.encode(instruction.data))
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Actions/getMintData/getMintData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class getMintData: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testGetMintData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class getMintDataAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testGetMintData() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class getTokenWallets: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .getWallets
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testsGetTokenWallets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class getTokenWalletsAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .getWallets
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testsGetTokenWallets() async throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Actions/sendSOL/sendSOL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class sendSOL: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)! // 5Zzguz4NsSRFxGkHfM4FmsFpGZiCDtY72zH2jzMcqkJx
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))! // 5Zzguz4NsSRFxGkHfM4FmsFpGZiCDtY72zH2jzMcqkJx
}

func testSendSOLFromBalance() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Actions/sendSOL/sendSOLAsync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class sendSOLAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)! // 5Zzguz4NsSRFxGkHfM4FmsFpGZiCDtY72zH2jzMcqkJx
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))! // 5Zzguz4NsSRFxGkHfM4FmsFpGZiCDtY72zH2jzMcqkJx
}

func testSendSOLFromBalance() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class sendSPLTokens: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
_ = try solana.api.requestAirdrop(account: account.publicKey.base58EncodedString, lamports: 100.toLamport(decimals: 9))?.get()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class sendSPLTokensAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testSendSPLTokenWithFee() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class serializeAndSendWithFee: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testSimulationSerializeAndSend() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class serializeAndSendWithFeeAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testSimulationSerializeAndSend() async throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Actions/swap/swap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class swap: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

/*func testSwapToken() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Api/Methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Methods: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testGetPureAccountInfo() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/Api/MethodsAsync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MethodsAsync: XCTestCase {
override func setUpWithError() throws {
let wallet: TestsWallet = .devnet
solana = Solana(router: NetworkingRouter(endpoint: endpoint))
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testGetAccountInfo() async throws {
Expand Down
6 changes: 3 additions & 3 deletions Tests/SolanaTests/Models/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ class PublicKeyTests: XCTestCase {
func testRestoreAccountFromSeedPhrase() {
let phrase12 = "miracle pizza supply useful steak border same again youth silver access hundred"
.components(separatedBy: " ")
let account12 = HotAccount(phrase: phrase12, network: .mainnetBeta)!
let account12 = HotAccount(phrase: phrase12)!
XCTAssertEqual(account12.publicKey.base58EncodedString, "HnXJX1Bvps8piQwDYEYC6oea9GEkvQvahvRj3c97X9xr")

let phrase24 = "budget resource fluid mutual ankle salt demise long burst sting doctor ozone risk magic wrap clap post pole jungle great update air interest abandon"
.components(separatedBy: " ")
let account24 = HotAccount(phrase: phrase24, network: .mainnetBeta)!
let account24 = HotAccount(phrase: phrase24)!
XCTAssertEqual(account24.publicKey.base58EncodedString, "9avcmC97zLPwHKXiDz6GpXyjvPn9VcN3ggqM5gsRnjvv")
}

func testRestoreAccountFromSeedPhraseBip32Deprecated() {
let phrase24 = "hint begin crowd dolphin drive render finger above sponsor prize runway invest dizzy pony bitter trial ignore crop please industry hockey wire use side"
.components(separatedBy: " ")
let account24 = HotAccount(phrase: phrase24, network: .mainnetBeta, derivablePath: DerivablePath(
let account24 = HotAccount(phrase: phrase24, derivablePath: DerivablePath(
type: .bip32Deprecated,
walletIndex: 0,
accountIndex: 0
Expand Down
2 changes: 1 addition & 1 deletion Tests/SolanaTests/TokenInfo/TokenInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TokenInfoTests: XCTestCase {
let wallet: TestsWallet = .devnet
let tokenProvider = try! TokenListProvider(path: getFileFrom("TokenInfo/mainnet-beta.tokens"))
solanaSDK = Solana(router: NetworkingRouter(endpoint: endpoint), tokenProvider: tokenProvider)
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "), network: endpoint.network)!
account = HotAccount(phrase: wallet.testAccount.components(separatedBy: " "))!
}

func testCloseAccountInstruction() {
Expand Down