Skip to content

A Swift/Linux library to interact with the XRP Ledger (XRPL) blockchain

License

Notifications You must be signed in to change notification settings

Transia-RnD/XRPLSwift

Repository files navigation

THIS REPOSITORY IS NOT FINISHED. README IS NOT UP TO DATE. EXPECT DRAGONS. SEE TESTS FOR CURRENT IMPLEMENTATIONS.

XRPLSwift

A pure Swift implementation for interacting with the XRP Ledger, the XRPLSwift library simplifies the hardest parts of XRP Ledger interaction, like serialization and transaction signing, by providing native Swift methods and models for XRP Ledger transactions and core server API (rippled) objects.

// create a network client
let client = try XrplClient(server: "https://s.altnet.rippletest.net:51234/")

// create a wallet on the testnet
let testWallet: Wallet = client.fundWallet()
print(testWallet)
// publicKey: ED3CC1BBD0952A60088E89FA502921895FC81FBD79CAE9109A8FE2D23659AD5D56
// privateKey: -HIDDEN-
// classicAddress: rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo

// look up account info
let acctInfo: AccountInfo = AccountInfo(
    account: "rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo",
    ledgerIndex: "current",
    queue: true,
    strict: true,
)
let response: BaseResponse<AccountInfoResponse> = client.request(req: acctInfo)
let result = response.result
print(result)
// {
//     "Account": "rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo",
//     "Balance": "1000000000",
//     "Flags": 0,
//     "LedgerEntryType": "AccountRoot",
//     "OwnerCount": 0,
//     "PreviousTxnID": "73CD4A37537A992270AAC8472F6681F44E400CBDE04EC8983C34B519F56AB107",
//     "PreviousTxnLgrSeq": 16233962,
//     "Sequence": 16233962,
//     "index": "FD66EC588B52712DCE74831DCB08B24157DC3198C29A0116AA64D310A58512D7"
// }

Contributors

Installation and supported versions

Swift Package Manager

You can use The Swift Package Manager to install XRPLSwift by adding it to your Package.swift file:

// swift-tools-version:5.7
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
    .package(url: "https://github.com/Transia-RnD/XRPLSwift.git", from: "1.0.0"),
    ]
)

Linux Compatibility

One of the goals of this library is to provide cross-platform support for Linux and support server-side Swift, however some features may only be available in iOS/macOS due to a lack of Linux supported libraries (ex. WebSockets). A test_linux.sh file is included that will run tests in a docker container. All contributions must compile on Linux.

The library supports Swift 5.7.1 and later.

Supported Versions Platforms Compatible

Features

Use XRPLSwift to build Swift applications that leverage the XRP Ledger. The library helps with all aspects of interacting with the XRP Ledger, including:

  • Key and wallet management
  • Serialization
  • Transaction Signing

XRPLSwift also provides:

  • A network client — See XrplClient for more information.
  • Methods for inspecting accounts — See XRPL Account Methods for more information.
  • Codecs for encoding and decoding addresses and other objects — See Core Codecs for more information.

See the complete XRPLSwift reference documentation on Read the Docs.

Usage

The following sections describe some of the most commonly used modules in the XRPLSwift library and provide sample code.

Network client

Use the XrplClient library to create a network client for connecting to the XRP Ledger.

let url: String = "wss://s1.ripple.com"
let client: XrplClient = try? XrplClient(server: url)

Manage keys and wallets

Wallet

Use the Wallet module to create a wallet from a given seed or or via a Testnet faucet.

To create a wallet from a seed (in this case, the value generated using Keypairs):

let walletFromSeed: Wallet = Wallet.Wallet(seed: seed)
print(walletFromSeed)
// publicKey: ED46949E414A3D6D758D347BAEC9340DC78F7397FEE893132AAF5D56E4D7DE77B0
// privateKey: -HIDDEN-
// classicAddress: rG5ZvYsK5BPi9f1Nb8mhFGDTNMJhEhufn6

To create a wallet from a Testnet faucet:

let testWallet: Wallet = fundWallet(client: client)
let testAccount: String = testWallet.classicAddress
print("Classic address: \(testAccount)")
// Classic address: rEQB2hhp3rg7sHj6L8YyR4GG47Cb7pfcuw

Keypairs

Use the Keypairs module to generate seeds and derive keypairs and addresses from those seed values.

Here's an example of how to generate a seed value and derive an XRP Ledger "classic" address from that seed.

let seed: String = keypairs.generateSeed()
let (public, private) = Keypairs.deriveKeypair(seed: seed)
let testAccount: String = Keypairs.deriveClassicAddress(publicKey: public)
print("Here's the public key: \(public)")
print("Here's the private key: \(private)")
print("Store this in a secure place!")
// Here's the public key: ED3CC1BBD0952A60088E89FA502921895FC81FBD79CAE9109A8FE2D23659AD5D56
// Here's the private key: EDE65EE7882847EF5345A43BFB8E6F5EEC60F45461696C384639B99B26AAA7A5CD
// Store this in a secure place!

Note: You can use Keypairs.sign to sign transactions but XRPLSwift also provides explicit methods for safely signing and submitting transactions. See Transaction Signing and XRPL Transaction Methods for more information.

Subscribe to ledger updates

You can send subscribe and unsubscribe requests only using the WebSocket network client. These request methods allow you to be alerted of certain situations as they occur, such as when a new ledger is declared.

url = "wss://s.altnet.rippletest.net/"
req = Subscribe(streams=[StreamParameter.LEDGER])
// NOTE: this code will run forever without a timeout, until the process is killed
with WebsocketClient(url) as client:
    client.send(req)
    for message in client:
        print(message)
// {'result': {'fee_base': 10, 'fee_ref': 10, 'ledger_hash': '7CD50477F23FF158B430772D8E82A961376A7B40E13C695AA849811EDF66C5C0', 'ledger_index': 18183504, 'ledger_time': 676412962, 'reserve_base': 20000000, 'reserve_inc': 5000000, 'validated_ledgers': '17469391-18183504'}, 'status': 'success', 'type': 'response'}
// {'fee_base': 10, 'fee_ref': 10, 'ledger_hash': 'BAA743DABD168BD434804416C8087B7BDEF7E6D7EAD412B9102281DD83B10D00', 'ledger_index': 18183505, 'ledger_time': 676412970, 'reserve_base': 20000000, 'reserve_inc': 5000000, 'txn_count': 0, 'type': 'ledgerClosed', 'validated_ledgers': '17469391-18183505'}
// {'fee_base': 10, 'fee_ref': 10, 'ledger_hash': 'D8227DAF8F745AE3F907B251D40B4081E019D013ABC23B68C0B1431DBADA1A46', 'ledger_index': 18183506, 'ledger_time': 676412971, 'reserve_base': 20000000, 'reserve_inc': 5000000, 'txn_count': 0, 'type': 'ledgerClosed', 'validated_ledgers': '17469391-18183506'}
// {'fee_base': 10, 'fee_ref': 10, 'ledger_hash': 'CFC412B6DDB9A402662832A781C23F0F2E842EAE6CFC539FEEB287318092C0DE', 'ledger_index': 18183507, 'ledger_time': 676412972, 'reserve_base': 20000000, 'reserve_inc': 5000000, 'txn_count': 0, 'type': 'ledgerClosed', 'validated_ledgers': '17469391-18183507'}

Encode addresses

Use AddressCodec to encode and decode addresses into and from the "classic" and X-address formats.

// convert classic address to x-address
testnetXAddress = try AddressCodec.classicAddressToXaddress(
    classicAddress: "rMPUKmzmDWEX1tQhzQ8oGFNfAEhnWNFwz",
    tag: 0,
    isTest: true
)
print(testnetXAddress)
// T7QDemmxnuN7a52A62nx2fxGPWcRahLCf3qaswfrsNW9Lps

Contributing

If you want to contribute to this project, see CONTRIBUTING.md.

Mailing Lists

We have a low-traffic mailing list for announcements of new XRPLSwift releases. (About 1 email per week)

If you're using the XRP Ledger in production, you should run a rippled server and subscribe to the ripple-server mailing list as well.

Report an issue

Experienced an issue? Report it here.

License

The XRPLSwift library is licensed under the ISC License. See LICENSE for more information.

Packages

No packages published

Languages