Skip to content

AergoLite﹢Swift

Bernardo Ramos edited this page Nov 20, 2020 · 1 revision

For Swift we use the AergoLite.swift wrapper

You can follow the instructions on the README to include it on your project.

But the repo lacks the binaries needed to use it.

You need to copy these files to the AergoLite sub-folder:

  • libaergolite.a
  • libuv.a
  • libbinn.a
  • libsecp256k1-vrf.a

You can download pre-compiled static libraries in the releases

Here are the instructions to compile these files:

iOS

On Mac OSX you can clone the AergoLite repo and run:

cd aergolite
./makeios

Mac, Linux and Windows

Clone the AergoLite repo and run:

cd aergolite
make static

Usage

Example code:

import SQLite

let uri = "file:path/to/app.db?blockchain=on&discovery=local:4329&password=test"
let db = try Connection(uri)

let status = try db.scalar("pragma blockchain_status") as! String
print(status)

print("waiting approval...")
while (true) {
    let db_is_ready = try db.scalar("pragma db_is_ready") as! Int64
    if (db_is_ready == 1) {
        print("the database is ready")
        break
    }
    sleep(1)  // use a timer instead
}

let stmt = try db.prepare("INSERT INTO users (email) VALUES (?)")
for email in ["[email protected]", "[email protected]"] {
    try stmt.run(email)
}

for row in try db.prepare("SELECT id, email FROM users") {
    print("id: \(row[0]), email: \(row[1])")
}

Signing a transaction from the blockchain admin:

db.createFunction("on_sign_transaction") { args in
    guard let data = args[0] as? String else { return nil }
    print("txn to be signed: " + data)
    signature = sign(data, privkey)
    return hex(pubkey) + ":" + hex(signature)
}

or:

let on_sign_transaction: (Expression<String>) -> Expression<String> = (
    try db.createFunction("on_sign_transaction") { data in
        print("txn to be signed: " + data)
        signature = sign(data, privkey)
        return hex(pubkey) + ":" + hex(signature)
    }
)

Receiving a notification of a processed transaction:

let on_processed_transaction: (Expression<Int64>, Expression<String>) -> Expression<String> = (
    try db.createFunction("transaction_notification") { nonce, status in
        print("transaction " + str(nonce) + ": " + status)
        return ""
    }
)

Receiving notification of local db update:

let on_db_update: (Expression<Int64>) -> Expression<String> = (
    try db.createFunction("update_notification") { unused in
        print("update received")
        return ""
    }
)
Clone this wiki locally