|
| 1 | +--- |
| 2 | +title: Ethereum Signing Provider |
| 3 | + |
| 4 | +description: "Ethereum Signing Provider for EVM Compatible Chains | Documentation - Web3Auth" |
| 5 | +--- |
| 6 | + |
| 7 | +import TabItem from "@theme/TabItem"; |
| 8 | +import Tabs from "@theme/Tabs"; |
| 9 | + |
| 10 | +import SPMProvider from "@site/src/common/sdk/mpc-core-kit/swift/_spm-installation-provider.mdx"; |
| 11 | +import CocoapodsProvider from "@site/src/common/sdk/mpc-core-kit/swift/_cocoapods-installation-provider.mdx"; |
| 12 | + |
| 13 | +The [MpcProviderSwift](https://github.com/tkey/MpcProviderSwift) package provides an Ethereum |
| 14 | +compatible signing provider with TSS Signing capabilities. This comes in handy by providing you with |
| 15 | +a standard way of retriving user's address and interacting with blockchain. |
| 16 | + |
| 17 | +Please note, this package only supports Ethereum. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +<Tabs defaultValue="swift-package-manager" |
| 22 | +values={[ |
| 23 | + { label: "Swift Package Manager", value: "swift-package-manager" }, |
| 24 | + { label: "Cocoapods", value: "cocoapods" }, |
| 25 | +]}> |
| 26 | +<TabItem value="swift-package-manager"> |
| 27 | + |
| 28 | +<SPMProvider /> |
| 29 | +</TabItem> |
| 30 | + |
| 31 | +<TabItem value="cocoapods"> |
| 32 | + |
| 33 | +<CocoapodsProvider /> |
| 34 | +</TabItem> |
| 35 | +</Tabs> |
| 36 | + |
| 37 | +## Set up the Extension |
| 38 | + |
| 39 | +The `MPCEthereumProvider` constructor accepts an `EVMSigner` object. The `EVMSigner` includes a |
| 40 | +`sign` method for signing raw messages and a `publicKey` property to retrieve the uncompressed |
| 41 | +public key. |
| 42 | + |
| 43 | +We will create an extension for `MPCCoreKit` that conforms to the `EVMSigner` protocol. Once the |
| 44 | +extension is implemented, an `MPCCoreKit` instance can be used to construct the |
| 45 | +`MPCEthereumProvider` instance. |
| 46 | + |
| 47 | +### Example |
| 48 | + |
| 49 | +```swift |
| 50 | +import Foundation |
| 51 | +import mpc_core_kit_swift |
| 52 | +import tkey |
| 53 | +import MpcProviderSwift |
| 54 | +import web3 |
| 55 | + |
| 56 | +extension MpcCoreKit : EvmSigner { |
| 57 | + public func sign(message: Data) throws -> Data { |
| 58 | + // Sign the message using MPC TSS |
| 59 | + let data = try self.tssSignSync(message: message) |
| 60 | + return data |
| 61 | + } |
| 62 | + |
| 63 | + public var publicKey: Data { |
| 64 | + // Get the uncompressed public key |
| 65 | + let fullAddress = try! KeyPoint( |
| 66 | + address: self.getTssPubKey().hexString |
| 67 | + ).getPublicKey(format: .FullAddress) |
| 68 | + |
| 69 | + |
| 70 | + return Data(hex: fullAddress)?.suffix(64) ?? Data() |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Initialisation |
| 77 | + |
| 78 | +Once the extension is implemented, we can initialize the `MPCEthereumProvider` by passing an |
| 79 | +instance of `MPCCoreKit` to the constructor. |
| 80 | + |
| 81 | +```swift |
| 82 | +import MpcProviderSwift |
| 83 | + |
| 84 | +// Use the existing MPC Core Kit instance to initialize the Ethereum Provider |
| 85 | +let mpcEthereumProvider = MPCEthereumProvider(evmSigner: mpcCoreKit) |
| 86 | +``` |
| 87 | + |
| 88 | +## Get Account |
| 89 | + |
| 90 | +To retrieve the user's address, you can use the `address` property of the `MPCEthereumProvider` |
| 91 | +instance. |
| 92 | + |
| 93 | +### Example |
| 94 | + |
| 95 | +```swift |
| 96 | +import web3 |
| 97 | + |
| 98 | +// Use the existing MPCEthereumProvider instance |
| 99 | +let address: EthereumAddress = mpcEthereumProvider.address |
| 100 | +``` |
| 101 | + |
| 102 | +## Sign Personal Message |
| 103 | + |
| 104 | +To sign a personl message, you can use the `signMessage` method. The method takes message to be |
| 105 | +signed in `Data` format as an argument. Please note, that you don't need to add the prefix to the |
| 106 | +message. The method will add the prefix internally. |
| 107 | + |
| 108 | +### Example |
| 109 | + |
| 110 | +```swift |
| 111 | +do { |
| 112 | + let signature = try mpcEthereumProvider.signMessage( |
| 113 | + message: "Web3Auth is awesome!".data(using: .utf8)! |
| 114 | + ) |
| 115 | +} catch { |
| 116 | + // Handle the error |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Sign Transaction |
| 121 | + |
| 122 | +To sign a Ethereum transaction, you can use the `signTransaction` method. The methods takes |
| 123 | +`EthereumTransaction` object as an argument. |
| 124 | + |
| 125 | +### Example |
| 126 | + |
| 127 | +```swift |
| 128 | +do { |
| 129 | + let address: EthereumAddress = mpcEthereumProvider.address |
| 130 | + |
| 131 | + // Self Transfer 0.000001 ETH on Ethereum Mainnet |
| 132 | + let transaction = EthereumTransaction( |
| 133 | + from: address, |
| 134 | + to: address, |
| 135 | + // 0.000001 ETH in Wei format |
| 136 | + value: 1000000000000, |
| 137 | + data: Data.init(hex: "0x00")!, |
| 138 | + nonce: "ADDRESS_NONCE", |
| 139 | + gasPrice: gasPrice.multiplied(by: .init(stringLiteral: "2")), |
| 140 | + gasLimit: gasLimit, |
| 141 | + // Chain ID for Ethereum Mainnet |
| 142 | + chainId: 1 |
| 143 | + ) |
| 144 | + |
| 145 | + // Sign the transaction |
| 146 | + let signedTransaction = try mpcEthereumProvider.signTransaction( |
| 147 | + transaction: transaction |
| 148 | + ) |
| 149 | +} catch { |
| 150 | + // Handle the error |
| 151 | +} |
| 152 | +``` |
0 commit comments