Skip to content
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
23 changes: 23 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build

on:
pull_request: {}
push:
branches: [ main ]

permissions: {}

jobs:
build:
name: Build (iOS Simulator)
runs-on: macos-latest
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
persist-credentials: false

- name: Build all products for the iOS Simulator
run: xcodebuild build -scheme MagicExtensions-Package -destination 'generic/platform=iOS Simulator'
27 changes: 0 additions & 27 deletions MagicExt-OAuth.podspec

This file was deleted.

27 changes: 0 additions & 27 deletions MagicExt-OIDC.podspec

This file was deleted.

23 changes: 17 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ let package = Package(
name: "MagicExt-OAuth",
targets: ["MagicExt-OAuth"]),
.library(
name: "MagicExt-OIDC",
targets: ["MagicExt-OIDC"]),
name: "MagicExt-Solana",
targets: ["MagicExt-Solana"]),
.library(
name: "MagicExt-Bitcoin",
targets: ["MagicExt-Bitcoin"]),
],
dependencies: [
.package(url: "https://github.com/magiclabs/magic-ios.git", from: "10.1.1")
.package(url: "https://github.com/magiclabs/magic-ios.git", from: "10.4.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -30,15 +33,23 @@ let package = Package(
.product(name: "MagicSDK", package: "magic-ios"),
]),
.target(
name: "MagicExt-OIDC",
name: "MagicExt-Solana",
dependencies: [
.product(name: "MagicSDK", package: "magic-ios"),
]),
.target(
name: "MagicExt-Bitcoin",
dependencies: [
.product(name: "MagicSDK", package: "magic-ios"),
]),
.testTarget(
name: "MagicExt-OAuthTests",
dependencies: ["MagicExt-OAuth"]),
.testTarget(
name: "MagicExt-OIDCTests",
dependencies: ["MagicExt-OIDC"]),
name: "MagicExt-SolanaTests",
dependencies: ["MagicExt-Solana"]),
.testTarget(
name: "MagicExt-BitcoinTests",
dependencies: ["MagicExt-Bitcoin"]),
]
)
158 changes: 127 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,140 @@
**We have decided to temporarily archive this repository and place it into maintenance mode. This decision allows us to focus our efforts and resources on advancing our core product line. During this period, active development and the addition of new features will be paused. We value the contributions of the community and appreciate your understanding as we prioritize our main projects. We look forward to resuming development in the future and will keep you updated on any changes.**
# MagicExt

# MagicExt-OAuth
[![CI Status](https://img.shields.io/travis/Ethella/MagicExt-OAuth.svg?style=flat)](https://travis-ci.org/Ethella/MagicExt-OAuth)
[![Version](https://img.shields.io/cocoapods/v/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth)
[![License](https://img.shields.io/cocoapods/l/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth)
[![Platform](https://img.shields.io/cocoapods/p/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth)
Official extensions for the [Magic iOS SDK](https://github.com/magiclabs/magic-ios), shipped as Swift Package Manager products:

Cocoapods
---
## Set up the local development env
1. To start the demo app with local development SDK, download following projects
```bash
# demo app
$ git clone https://github.com/magiclabs/magic-ios-demo
# ios SDK
$ git clone https://github.com/magiclabs/magic-ios
$ git clone https://github.com/magiclabs/magic-ios-ext
- `MagicExt-Solana`
- `MagicExt-Bitcoin`
- `MagicExt-OAuth`

Requires iOS 13 or later. CocoaPods is not supported.

## Solana and Bitcoin

Solana and Bitcoin work like the `@magic-ext/solana` and `@magic-ext/bitcoin` packages in magic-js. They need [magic-ios](https://github.com/magiclabs/magic-ios) 10.4.0 or later, which has `Magic(apiKey:extensions:)`.

Add the package with Swift Package Manager, then add the products you need to your target: `MagicExt-Solana`, `MagicExt-Bitcoin`, or both.

```swift
.package(url: "https://github.com/magiclabs/magic-ios-ext.git", from: "X.Y.Z") // the first release with MagicExt-Solana and MagicExt-Bitcoin
```

2. To enable the demo use the local development SDK. Navigate to `magic-ios-demo/Podfile` and edit the following lines.
This will make pod file install local dependencies instead of the ones distributed.
Turn them on by passing a configuration when you create `Magic`:

```swift
import MagicSDK
import MagicExt_Solana
import MagicExt_Bitcoin

```ruby
# Distributed Library on Cocoapods
# pod 'MagicSDK', '~> 3.0'
# pod 'MagicExt-OAuth', '~> 1.0'

# Local development library
pod 'MagicSDK', :path => '../magic-ios/MagicSDK.podspec'
pod 'MagicExt-OAuth', :path => '../magic-ios-ext/MagicExt-OAuth.podspec'
Magic.shared = Magic(
apiKey: "YOUR_PUBLISHABLE_API_KEY",
extensions: [
SolanaConfiguration(rpcUrl: "https://api.devnet.solana.com"),
BitcoinConfiguration(rpcUrl: "https://blockstream.info/testnet/api", network: "testnet")
]
)
```

```bash
$ cd /YOUR/PATH/TO/magic-ios-demo
`Magic.shared.solana` and `Magic.shared.bitcoin` are `nil` until you pass the matching configuration. The user must be logged in first.

Both extensions have these methods. Each one has a completion-handler version and a PromiseKit version.

- `getPublicAddress` returns the user's address on that chain.
- `revealPrivateKey` shows Magic's UI for exporting the key. The key never reaches your app.

```swift
Magic.shared.solana?.getPublicAddress { response in
print(response.result ?? response.error?.localizedDescription ?? "")
}
```

### Sign a Solana message

```swift
// A String is signed as UTF-8. You can also pass Data.
Magic.shared.solana?.signMessage("Sign in to Example") { response in
// response.result is the 64-byte ed25519 signature, as Data
}
```

### Sign a Solana transaction

The SDK has no Solana dependency, so you give it the serialized transaction bytes and say which kind it is. Build the bytes with a Solana library or by hand. In `@solana/web3.js` this is `Transaction.serialize({ requireAllSignatures: false })` for a legacy transaction, or `VersionedTransaction.serialize()` for a version 0 transaction.

- `.legacy` is for a legacy `Transaction`.
- `.v0` is for a `VersionedTransaction`.

```swift
let serialized: Data = ... // the serialized, unsigned transaction

# Install dependencies
$ pod install
Magic.shared.solana?.signTransaction(serialized, type: .legacy) { response in
if let signed = response.result {
let rawTransaction: Data = signed.rawTransaction
// Send rawTransaction to a Solana node, for example with sendRawTransaction.
} else if let error = response.error {
print(error.localizedDescription)
}
}
```

3. Open `/YOUR/PATH/TO/magic-ios-demo/magic-ios-demo.xcworkspace` with XCode and try it out!
Use `type: .v0` for a version 0 transaction.

### Partly sign a Solana transaction

Use `partialSignTransaction` when other people also have to sign, such as a fee payer. It adds the user's signature and leaves the other slots empty.

```swift
// serializeConfig only applies to legacy transactions, as in magic-js.
let config = SolanaSerializeConfig(requireAllSignatures: false, verifySignatures: false)

Magic.shared.solana?.partialSignTransaction(serialized, type: .legacy, serializeConfig: config) { response in
let partlySigned = response.result?.rawTransaction
// Pass it on to the next signer.
}
```

### Sign a Bitcoin transaction

You choose the inputs (the UTXOs to spend), the outputs and the change. Magic signs the result and returns a transaction that is ready to broadcast.

- Amounts are in BTC, not satoshis, with at most eight decimal places.
- `txNum` is the output index (vout) of the UTXO in its source transaction.
- Each input needs its `value`, because the signature covers the amount being spent.
- The fee is whatever is left after the outputs. Total outputs must not be more than total inputs. There is no minimum or maximum fee, so check it yourself.

```swift
let inputs = [
BitcoinTransactionInput(txid: "<txid of the UTXO>", txNum: 0, value: 0.001)
]
let outputs = [
BitcoinTransactionOutput(address: "<destination address>", value: 0.0009) // 0.0001 BTC fee
]

Magic.shared.bitcoin?.signTransaction(inputs: inputs, outputs: outputs) { response in
if let signed = response.result {
print(signed.signedTransaction) // hex, ready to broadcast
print(signed.transactionHash) // txid
} else if let error = response.error {
print(error.localizedDescription)
}
}
```

Work out fees and change in whole satoshis, then convert to BTC for the call. Signing does not mean the network will accept the transaction.

### Before you broadcast

The signed result comes back through Magic's relayer. Decode `rawTransaction` or `signedTransaction` and check the amounts and addresses before you send it.

---

## Develop locally

Clone the SDK, this repo and the demo app side by side:

```bash
git clone https://github.com/magiclabs/magic-ios
git clone https://github.com/magiclabs/magic-ios-ext
git clone https://github.com/magiclabs/magic-ios-demo
```

Open `magic-ios-demo/magic-ios-spm-demo.xcodeproj`. It uses the local `magic-ios` and `magic-ios-ext` folders, so your changes show up in the demo. If Xcode says a package product is missing, run File > Packages > Resolve Package Versions.
87 changes: 87 additions & 0 deletions Sources/MagicExt-Bitcoin/BitcoinConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// BitcoinConfiguration.swift
// MagicSDK
//

import Foundation
import MagicSDK

internal enum BitcoinMethod: String {
case btc_signTransaction
}

/// Enables the Bitcoin extension. Mirrors `BitcoinConfig` in `@magic-ext/bitcoin`.
public struct BitcoinConfiguration: ExtensionConfiguration {
public let name = "bitcoin"
public let rpcUrl: String
public let network: String

public init(rpcUrl: String, network: String) {
self.rpcUrl = rpcUrl
self.network = network
}

enum CodingKeys: String, CodingKey {
case rpcUrl, chainType, options
}

enum OptionsKeys: String, CodingKey {
case network
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(rpcUrl, forKey: .rpcUrl)
try container.encode(BitcoinExtension.chainType, forKey: .chainType)
var options = container.nestedContainer(keyedBy: OptionsKeys.self, forKey: .options)
try options.encode(network, forKey: .network)
}
}

/// A UTXO to spend. `value` is in BTC (not satoshis), with at most eight decimal
/// places. The enclave needs this value to compute its segwit (BIP143) sighash.
/// `txNum` is the output index (vout) of the UTXO within its source transaction.
public struct BitcoinTransactionInput: Codable {
public let txid: String
public let txNum: Int
public let value: Double
public let address: String?

public init(txid: String, txNum: Int, value: Double, address: String? = nil) {
self.txid = txid
self.txNum = txNum
self.value = value
self.address = address
}

enum CodingKeys: String, CodingKey {
case txid
case txNum = "tx_num"
case value, address
}
}

/// A transaction output. `value` is in BTC with at most eight decimal places.
public struct BitcoinTransactionOutput: Codable {
public let address: String
public let value: Double

public init(address: String, value: Double) {
self.address = address
self.value = value
}
}

/// The result of `signTransaction`.
public struct BitcoinSignedTransaction: MagicResponse {
/// The broadcast-ready, fully-signed transaction as hex.
public let signedTransaction: String
/// The transaction hash (txid).
public let transactionHash: String
}

/// Params for `btc_signTransaction`.
struct BitcoinTransactionParams: Codable {
let inputs: [BitcoinTransactionInput]
let outputs: [BitcoinTransactionOutput]
}
Loading
Loading