parent
da0f550a79
commit
aff711f505
@ -0,0 +1,103 @@ |
|||||||
|
// Copyright (c) 2022-2025 Niko Bonnieure, Par le Peuple, NextGraph.org developers
|
||||||
|
// All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0
|
||||||
|
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
||||||
|
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
||||||
|
// at your option. All files in the project carrying such
|
||||||
|
// notice may not be copied, modified, or distributed except
|
||||||
|
// according to those terms.
|
||||||
|
|
||||||
|
use std::fs::read; |
||||||
|
|
||||||
|
use async_std::stream::StreamExt; |
||||||
|
#[allow(unused_imports)] |
||||||
|
use nextgraph::local_broker::{ |
||||||
|
app_request, app_request_stream, doc_fetch_repo_subscribe, doc_sparql_update, |
||||||
|
init_local_broker, session_start, session_stop, user_connect, user_disconnect, wallet_close, |
||||||
|
wallet_create_v0, wallet_get, wallet_get_file, wallet_import, wallet_open_with_mnemonic_words, |
||||||
|
wallet_read_file, wallet_was_opened, LocalBrokerConfig, SessionConfig, |
||||||
|
}; |
||||||
|
use nextgraph::net::types::BootstrapContentV0; |
||||||
|
use nextgraph::repo::errors::NgError; |
||||||
|
use nextgraph::repo::log::*; |
||||||
|
use nextgraph::repo::types::PubKey; |
||||||
|
use nextgraph::wallet::types::CreateWalletV0; |
||||||
|
use nextgraph::wallet::{display_mnemonic, emojis::display_pazzle}; |
||||||
|
|
||||||
|
#[async_std::main] |
||||||
|
async fn main() -> std::io::Result<()> { |
||||||
|
// initialize the local_broker with in-memory config.
|
||||||
|
// all sessions will be lost when the program exits
|
||||||
|
init_local_broker(Box::new(|| LocalBrokerConfig::InMemory)).await; |
||||||
|
|
||||||
|
let wallet_file = |
||||||
|
read("/Users/nl/Downloads/wallet-Hr-UITwGtjE1k6lXBoVGzD4FQMiDkM3T6bSeAi9PXt4A.ngw") |
||||||
|
.expect("read wallet file"); |
||||||
|
|
||||||
|
let wallet = wallet_read_file(wallet_file).await?; |
||||||
|
|
||||||
|
let mnemonic_words = vec![ |
||||||
|
"jealous".to_string(), |
||||||
|
"during".to_string(), |
||||||
|
"elevator".to_string(), |
||||||
|
"swallow".to_string(), |
||||||
|
"pen".to_string(), |
||||||
|
"phone".to_string(), |
||||||
|
"like".to_string(), |
||||||
|
"employ".to_string(), |
||||||
|
"myth".to_string(), |
||||||
|
"remember".to_string(), |
||||||
|
"question".to_string(), |
||||||
|
"lemon".to_string(), |
||||||
|
]; |
||||||
|
|
||||||
|
let opened_wallet = wallet_open_with_mnemonic_words(&wallet, &mnemonic_words, [2, 3, 2, 3])?; |
||||||
|
|
||||||
|
let user_id = opened_wallet.personal_identity(); |
||||||
|
let wallet_name = opened_wallet.name(); |
||||||
|
|
||||||
|
let client = wallet_import(wallet.clone(), opened_wallet, true).await?; |
||||||
|
|
||||||
|
let session = session_start(SessionConfig::new_in_memory(&user_id, &wallet_name)).await?; |
||||||
|
|
||||||
|
// let session = session_start(SessionConfig::new_remote(&user_id, &wallet_name, None)).await?;
|
||||||
|
|
||||||
|
// if the user has internet access, they can now decide to connect to its Server Broker, in order to sync data
|
||||||
|
let status = user_connect(&user_id).await?; |
||||||
|
|
||||||
|
let result = doc_sparql_update( |
||||||
|
session.session_id, |
||||||
|
"INSERT DATA { <did:ng:_> <example:predicate> \"An example value10\". }".to_string(), |
||||||
|
Some("did:ng:o:Dn0QpE9_4jhta1mUWRl_LZh1SbXUkXfOB5eu38PNIk4A:v:Z4ihjV3KMVIqBxzjP6hogVLyjkZunLsb7MMsCR0kizQA".to_string()), |
||||||
|
) |
||||||
|
.await; |
||||||
|
|
||||||
|
log_debug!("{:?}", result); |
||||||
|
|
||||||
|
// // a session ID has been assigned to you in `session.session_id` you can use it to fetch a document
|
||||||
|
// let (mut receiver, cancel) = doc_fetch_repo_subscribe(
|
||||||
|
// session.session_id,
|
||||||
|
// "did:ng:o:Dn0QpE9_4jhta1mUWRl_LZh1SbXUkXfOB5eu38PNIk4A".to_string(),
|
||||||
|
// )
|
||||||
|
// .await?;
|
||||||
|
|
||||||
|
// cancel();
|
||||||
|
|
||||||
|
// while let Some(app_response) = receiver.next().await {
|
||||||
|
// let (inserts, removes) =
|
||||||
|
// nextgraph::verifier::read_triples_in_app_response_from_rust(app_response)?;
|
||||||
|
// log_debug!("inserts {:?}", inserts);
|
||||||
|
// log_debug!("removes {:?}", removes);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Then we should disconnect
|
||||||
|
user_disconnect(&user_id).await?; |
||||||
|
|
||||||
|
// stop the session
|
||||||
|
session_stop(&user_id).await?; |
||||||
|
|
||||||
|
// closes the wallet
|
||||||
|
wallet_close(&wallet_name).await?; |
||||||
|
|
||||||
|
Ok(()) |
||||||
|
} |
@ -0,0 +1,3 @@ |
|||||||
|
home = /Applications/Xcode.app/Contents/Developer/usr/bin |
||||||
|
include-system-site-packages = false |
||||||
|
version = 3.7.3 |
@ -0,0 +1,181 @@ |
|||||||
|
# This file is autogenerated by maturin v1.8.2 |
||||||
|
# To update, run |
||||||
|
# |
||||||
|
# maturin generate-ci github |
||||||
|
# |
||||||
|
name: CI |
||||||
|
|
||||||
|
on: |
||||||
|
push: |
||||||
|
branches: |
||||||
|
- main |
||||||
|
- master |
||||||
|
tags: |
||||||
|
- '*' |
||||||
|
pull_request: |
||||||
|
workflow_dispatch: |
||||||
|
|
||||||
|
permissions: |
||||||
|
contents: read |
||||||
|
|
||||||
|
jobs: |
||||||
|
linux: |
||||||
|
runs-on: ${{ matrix.platform.runner }} |
||||||
|
strategy: |
||||||
|
matrix: |
||||||
|
platform: |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: x86_64 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: x86 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: aarch64 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: armv7 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: s390x |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: ppc64le |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- uses: actions/setup-python@v5 |
||||||
|
with: |
||||||
|
python-version: 3.x |
||||||
|
- name: Build wheels |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
with: |
||||||
|
target: ${{ matrix.platform.target }} |
||||||
|
args: --release --out dist --find-interpreter |
||||||
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} |
||||||
|
manylinux: auto |
||||||
|
- name: Upload wheels |
||||||
|
uses: actions/upload-artifact@v4 |
||||||
|
with: |
||||||
|
name: wheels-linux-${{ matrix.platform.target }} |
||||||
|
path: dist |
||||||
|
|
||||||
|
musllinux: |
||||||
|
runs-on: ${{ matrix.platform.runner }} |
||||||
|
strategy: |
||||||
|
matrix: |
||||||
|
platform: |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: x86_64 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: x86 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: aarch64 |
||||||
|
- runner: ubuntu-22.04 |
||||||
|
target: armv7 |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- uses: actions/setup-python@v5 |
||||||
|
with: |
||||||
|
python-version: 3.x |
||||||
|
- name: Build wheels |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
with: |
||||||
|
target: ${{ matrix.platform.target }} |
||||||
|
args: --release --out dist --find-interpreter |
||||||
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} |
||||||
|
manylinux: musllinux_1_2 |
||||||
|
- name: Upload wheels |
||||||
|
uses: actions/upload-artifact@v4 |
||||||
|
with: |
||||||
|
name: wheels-musllinux-${{ matrix.platform.target }} |
||||||
|
path: dist |
||||||
|
|
||||||
|
windows: |
||||||
|
runs-on: ${{ matrix.platform.runner }} |
||||||
|
strategy: |
||||||
|
matrix: |
||||||
|
platform: |
||||||
|
- runner: windows-latest |
||||||
|
target: x64 |
||||||
|
- runner: windows-latest |
||||||
|
target: x86 |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- uses: actions/setup-python@v5 |
||||||
|
with: |
||||||
|
python-version: 3.x |
||||||
|
architecture: ${{ matrix.platform.target }} |
||||||
|
- name: Build wheels |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
with: |
||||||
|
target: ${{ matrix.platform.target }} |
||||||
|
args: --release --out dist --find-interpreter |
||||||
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} |
||||||
|
- name: Upload wheels |
||||||
|
uses: actions/upload-artifact@v4 |
||||||
|
with: |
||||||
|
name: wheels-windows-${{ matrix.platform.target }} |
||||||
|
path: dist |
||||||
|
|
||||||
|
macos: |
||||||
|
runs-on: ${{ matrix.platform.runner }} |
||||||
|
strategy: |
||||||
|
matrix: |
||||||
|
platform: |
||||||
|
- runner: macos-13 |
||||||
|
target: x86_64 |
||||||
|
- runner: macos-14 |
||||||
|
target: aarch64 |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- uses: actions/setup-python@v5 |
||||||
|
with: |
||||||
|
python-version: 3.x |
||||||
|
- name: Build wheels |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
with: |
||||||
|
target: ${{ matrix.platform.target }} |
||||||
|
args: --release --out dist --find-interpreter |
||||||
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }} |
||||||
|
- name: Upload wheels |
||||||
|
uses: actions/upload-artifact@v4 |
||||||
|
with: |
||||||
|
name: wheels-macos-${{ matrix.platform.target }} |
||||||
|
path: dist |
||||||
|
|
||||||
|
sdist: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- name: Build sdist |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
with: |
||||||
|
command: sdist |
||||||
|
args: --out dist |
||||||
|
- name: Upload sdist |
||||||
|
uses: actions/upload-artifact@v4 |
||||||
|
with: |
||||||
|
name: wheels-sdist |
||||||
|
path: dist |
||||||
|
|
||||||
|
release: |
||||||
|
name: Release |
||||||
|
runs-on: ubuntu-latest |
||||||
|
if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }} |
||||||
|
needs: [linux, musllinux, windows, macos, sdist] |
||||||
|
permissions: |
||||||
|
# Use to sign the release artifacts |
||||||
|
id-token: write |
||||||
|
# Used to upload release artifacts |
||||||
|
contents: write |
||||||
|
# Used to generate artifact attestation |
||||||
|
attestations: write |
||||||
|
steps: |
||||||
|
- uses: actions/download-artifact@v4 |
||||||
|
- name: Generate artifact attestation |
||||||
|
uses: actions/attest-build-provenance@v1 |
||||||
|
with: |
||||||
|
subject-path: 'wheels-*/*' |
||||||
|
- name: Publish to PyPI |
||||||
|
if: ${{ startsWith(github.ref, 'refs/tags/') }} |
||||||
|
uses: PyO3/maturin-action@v1 |
||||||
|
env: |
||||||
|
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} |
||||||
|
with: |
||||||
|
command: upload |
||||||
|
args: --non-interactive --skip-existing wheels-*/* |
@ -0,0 +1,72 @@ |
|||||||
|
/target |
||||||
|
|
||||||
|
# Byte-compiled / optimized / DLL files |
||||||
|
__pycache__/ |
||||||
|
.pytest_cache/ |
||||||
|
*.py[cod] |
||||||
|
|
||||||
|
# C extensions |
||||||
|
*.so |
||||||
|
|
||||||
|
# Distribution / packaging |
||||||
|
.Python |
||||||
|
.venv/ |
||||||
|
env/ |
||||||
|
bin/ |
||||||
|
build/ |
||||||
|
develop-eggs/ |
||||||
|
dist/ |
||||||
|
eggs/ |
||||||
|
lib/ |
||||||
|
lib64/ |
||||||
|
parts/ |
||||||
|
sdist/ |
||||||
|
var/ |
||||||
|
include/ |
||||||
|
man/ |
||||||
|
venv/ |
||||||
|
*.egg-info/ |
||||||
|
.installed.cfg |
||||||
|
*.egg |
||||||
|
|
||||||
|
# Installer logs |
||||||
|
pip-log.txt |
||||||
|
pip-delete-this-directory.txt |
||||||
|
pip-selfcheck.json |
||||||
|
|
||||||
|
# Unit test / coverage reports |
||||||
|
htmlcov/ |
||||||
|
.tox/ |
||||||
|
.coverage |
||||||
|
.cache |
||||||
|
nosetests.xml |
||||||
|
coverage.xml |
||||||
|
|
||||||
|
# Translations |
||||||
|
*.mo |
||||||
|
|
||||||
|
# Mr Developer |
||||||
|
.mr.developer.cfg |
||||||
|
.project |
||||||
|
.pydevproject |
||||||
|
|
||||||
|
# Rope |
||||||
|
.ropeproject |
||||||
|
|
||||||
|
# Django stuff: |
||||||
|
*.log |
||||||
|
*.pot |
||||||
|
|
||||||
|
.DS_Store |
||||||
|
|
||||||
|
# Sphinx documentation |
||||||
|
docs/_build/ |
||||||
|
|
||||||
|
# PyCharm |
||||||
|
.idea/ |
||||||
|
|
||||||
|
# VSCode |
||||||
|
.vscode/ |
||||||
|
|
||||||
|
# Pyenv |
||||||
|
.python-version |
@ -0,0 +1,25 @@ |
|||||||
|
[package] |
||||||
|
name = "ng-sdk-python" |
||||||
|
version.workspace = true |
||||||
|
description = "NextGraph python package. Nextgraph is a decentralized, secure and local-first web 3.0 ecosystem based on Semantic Web and CRDTs" |
||||||
|
edition.workspace = true |
||||||
|
license.workspace = true |
||||||
|
authors.workspace = true |
||||||
|
repository.workspace = true |
||||||
|
homepage.workspace = true |
||||||
|
keywords = [ "crdt","e2ee","local-first","p2p","semantic-web" ] |
||||||
|
documentation.workspace = true |
||||||
|
rust-version.workspace = true |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
[lib] |
||||||
|
name = "nextgraph" |
||||||
|
crate-type = ["cdylib"] |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
pyo3 = "0.23.3" |
||||||
|
pyo3-async-runtimes = { version = "0.23", features = ["async-std-runtime"] } |
||||||
|
pythonize = "0.23.0" |
||||||
|
async-std = "1.12.0" |
||||||
|
serde = { version = "1.0.142", features = ["derive"] } |
||||||
|
nextgraph = { path = "../nextgraph" } |
@ -0,0 +1,63 @@ |
|||||||
|
<p align="center"> |
||||||
|
<img src="https://git.nextgraph.org/NextGraph/nextgraph-rs/raw/branch/master/nextgraph/.static/header.png" alt="nextgraph-header" /> |
||||||
|
</p> |
||||||
|
|
||||||
|
# nextgraph |
||||||
|
|
||||||
|
![MSRV][rustc-image] |
||||||
|
[![Apache 2.0 Licensed][license-image]][license-link] |
||||||
|
[![MIT Licensed][license-image2]][license-link2] |
||||||
|
[](https://forum.nextgraph.org) |
||||||
|
[](https://pypi.org/project/nextgraph/) |
||||||
|
|
||||||
|
Python package for NextGraph, implemented in Rust |
||||||
|
|
||||||
|
This repository is in active development at [https://git.nextgraph.org/NextGraph/nextgraph-rs](https://git.nextgraph.org/NextGraph/nextgraph-rs), a Gitea instance. For bug reports, issues, merge requests, and in order to join the dev team, please visit the link above and create an account (you can do so with a github account). The [github repo](https://github.com/nextgraph-org/nextgraph-rs) is just a read-only mirror that does not accept issues. |
||||||
|
|
||||||
|
## NextGraph |
||||||
|
|
||||||
|
> NextGraph brings about the convergence of P2P and Semantic Web technologies, towards a decentralized, secure and privacy-preserving cloud, based on CRDTs. |
||||||
|
> |
||||||
|
> This open source ecosystem provides solutions for end-users (a platform) and software developers (a framework), wishing to use or create **decentralized** apps featuring: **live collaboration** on rich-text documents, peer to peer communication with **end-to-end encryption**, offline-first, **local-first**, portable and interoperable data, total ownership of data and software, security and privacy. Centered on repositories containing **semantic data** (RDF), **rich text**, and structured data formats like **JSON**, synced between peers belonging to permissioned groups of users, it offers strong eventual consistency, thanks to the use of **CRDTs**. Documents can be linked together, signed, shared securely, queried using the **SPARQL** language and organized into sites and containers. |
||||||
|
> |
||||||
|
> More info here [https://nextgraph.org](https://nextgraph.org) |
||||||
|
|
||||||
|
## Support |
||||||
|
|
||||||
|
Documentation can be found here [https://docs.nextgraph.org](https://docs.nextgraph.org) |
||||||
|
|
||||||
|
And our community forum where you can ask questions is here [https://forum.nextgraph.org](https://forum.nextgraph.org) |
||||||
|
|
||||||
|
[](https://fosstodon.org/@nextgraph) |
||||||
|
|
||||||
|
## How to use NextGraph App & Platform |
||||||
|
|
||||||
|
NextGraph is in alpha release! |
||||||
|
|
||||||
|
You can try it online or by installing the apps. Please follow our [Getting started](https://docs.nextgraph.org/en/getting-started/) guide . |
||||||
|
|
||||||
|
You can also subscribe to [our newsletter](https://list.nextgraph.org/subscription/form) to get updates, and support us with a [donation](https://nextgraph.org/donate/). |
||||||
|
|
||||||
|
## NextGraph is also a Framework for App developers |
||||||
|
|
||||||
|
Read our [getting started guide for developers](https://docs.nextgraph.org/en/framework/getting-started/). |
||||||
|
|
||||||
|
## License |
||||||
|
|
||||||
|
Licensed under either of |
||||||
|
|
||||||
|
- Apache License, Version 2.0 ([LICENSE-APACHE2](LICENSE-APACHE2) or http://www.apache.org/licenses/LICENSE-2.0) |
||||||
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
||||||
|
at your option. |
||||||
|
|
||||||
|
`SPDX-License-Identifier: Apache-2.0 OR MIT` |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
NextGraph received funding through the [NGI Assure Fund](https://nlnet.nl/assure) and the [NGI Zero Commons Fund](https://nlnet.nl/commonsfund/), both funds established by [NLnet](https://nlnet.nl/) Foundation with financial support from the European Commission's [Next Generation Internet](https://ngi.eu/) programme, under the aegis of DG Communications Networks, Content and Technology under grant agreements No 957073 and No 101092990, respectively. |
||||||
|
|
||||||
|
[rustc-image]: https://img.shields.io/badge/rustc-1.74+-blue.svg |
||||||
|
[license-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg |
||||||
|
[license-link]: https://git.nextgraph.org/NextGraph/nextgraph-rs/raw/branch/master/LICENSE-APACHE2 |
||||||
|
[license-image2]: https://img.shields.io/badge/license-MIT-blue.svg |
||||||
|
[license-link2]: https://git.nextgraph.org/NextGraph/nextgraph-rs/src/branch/master/LICENSE-MIT |
@ -0,0 +1,17 @@ |
|||||||
|
[build-system] |
||||||
|
requires = ["maturin>=1.8,<2.0"] |
||||||
|
build-backend = "maturin" |
||||||
|
|
||||||
|
[project] |
||||||
|
name = "nextgraph" |
||||||
|
requires-python = ">=3.7.3" |
||||||
|
description = "NextGraph brings about the convergence of P2P and Semantic Web technologies, towards a decentralized, secure and privacy-preserving cloud, based on CRDTs." |
||||||
|
readme = "README.md" |
||||||
|
classifiers = [ |
||||||
|
"Programming Language :: Rust", |
||||||
|
"Programming Language :: Python :: Implementation :: CPython", |
||||||
|
"Programming Language :: Python :: Implementation :: PyPy", |
||||||
|
] |
||||||
|
version = "0.1a1.dev2" |
||||||
|
[tool.maturin] |
||||||
|
features = ["pyo3/extension-module"] |
@ -0,0 +1,142 @@ |
|||||||
|
use pyo3::exceptions::PyTypeError; |
||||||
|
use pyo3::prelude::*; |
||||||
|
use pythonize::{depythonize, pythonize}; |
||||||
|
use serde::{Deserialize, Serialize}; |
||||||
|
|
||||||
|
use std::fs::read; |
||||||
|
|
||||||
|
#[allow(unused_imports)] |
||||||
|
use ::nextgraph::local_broker::{ |
||||||
|
app_request, app_request_stream, doc_fetch_repo_subscribe, init_local_broker, session_start, |
||||||
|
session_stop, user_connect, user_disconnect, wallet_close, wallet_create_v0, wallet_get, |
||||||
|
wallet_get_file, wallet_import, wallet_read_file, wallet_was_opened, LocalBrokerConfig, |
||||||
|
SessionConfig, |
||||||
|
}; |
||||||
|
use ::nextgraph::net::types::BootstrapContentV0; |
||||||
|
use ::nextgraph::repo::errors::NgError; |
||||||
|
use ::nextgraph::repo::log::*; |
||||||
|
use ::nextgraph::repo::types::PubKey; |
||||||
|
use ::nextgraph::wallet::types::{CreateWalletV0, SessionInfo}; |
||||||
|
use ::nextgraph::wallet::{display_mnemonic, emojis::display_pazzle}; |
||||||
|
use async_std::stream::StreamExt; |
||||||
|
|
||||||
|
#[pyfunction] |
||||||
|
fn init_local_broker_in_memory() -> PyResult<()> { |
||||||
|
Ok(()) |
||||||
|
} |
||||||
|
|
||||||
|
struct PyNgError(NgError); |
||||||
|
|
||||||
|
impl From<PyNgError> for PyErr { |
||||||
|
fn from(e: PyNgError) -> PyErr { |
||||||
|
let ioe: std::io::Error = e.0.into(); |
||||||
|
ioe.into() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl From<NgError> for PyNgError { |
||||||
|
fn from(e: NgError) -> PyNgError { |
||||||
|
PyNgError(e) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// Open the wallet with mnemonic and PIN, and returns the wallet_name and the SessionInfo
|
||||||
|
#[pyfunction] |
||||||
|
fn wallet_open_with_mnemonic_words( |
||||||
|
py: Python, |
||||||
|
wallet_file_path: String, |
||||||
|
mnemonic_words: Vec<String>, |
||||||
|
pin: [u8; 4], |
||||||
|
) -> PyResult<Bound<PyAny>> { |
||||||
|
pyo3_async_runtimes::async_std::future_into_py(py, async move { |
||||||
|
init_local_broker(Box::new(|| LocalBrokerConfig::InMemory)).await; |
||||||
|
|
||||||
|
let wallet_file = read(wallet_file_path).expect("read wallet file"); |
||||||
|
|
||||||
|
let wallet = wallet_read_file(wallet_file) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
let opened_wallet = ::nextgraph::local_broker::wallet_open_with_mnemonic_words( |
||||||
|
&wallet, |
||||||
|
&mnemonic_words, |
||||||
|
pin, |
||||||
|
) |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
let user_id = opened_wallet.personal_identity(); |
||||||
|
let wallet_name = opened_wallet.name(); |
||||||
|
|
||||||
|
let _client = wallet_import(wallet.clone(), opened_wallet, true) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
let session = session_start(SessionConfig::new_in_memory(&user_id, &wallet_name)) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
// let session = session_start(SessionConfig::new_remote(&user_id, &wallet_name, None)).await?;
|
||||||
|
|
||||||
|
let _status = user_connect(&user_id) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
let s = Python::with_gil(|py| pythonize(py, &session).unwrap().unbind()); |
||||||
|
Ok((wallet_name, s)) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
||||||
|
struct Sample { |
||||||
|
foo: u64, |
||||||
|
bar: Option<usize>, |
||||||
|
} |
||||||
|
|
||||||
|
#[pyfunction] |
||||||
|
#[pyo3(signature = (session_id, sparql, nuri=None))] |
||||||
|
fn doc_sparql_update( |
||||||
|
py: Python, |
||||||
|
session_id: u64, |
||||||
|
sparql: String, |
||||||
|
nuri: Option<String>, |
||||||
|
) -> PyResult<Bound<PyAny>> { |
||||||
|
pyo3_async_runtimes::async_std::future_into_py(py, async move { |
||||||
|
::nextgraph::local_broker::doc_sparql_update(session_id, sparql, nuri) |
||||||
|
.await |
||||||
|
.map_err(|e| PyTypeError::new_err(e))?; |
||||||
|
Ok(()) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
#[pyfunction] |
||||||
|
fn disconnect_and_close<'a>( |
||||||
|
py: Python<'a>, |
||||||
|
user_id: Bound<'a, PyAny>, |
||||||
|
wallet_name: String, |
||||||
|
) -> PyResult<Bound<'a, PyAny>> { |
||||||
|
let user_id: PubKey = depythonize(&user_id)?; |
||||||
|
pyo3_async_runtimes::async_std::future_into_py(py, async move { |
||||||
|
user_disconnect(&user_id) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
// stop the session
|
||||||
|
session_stop(&user_id) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
|
||||||
|
// closes the wallet
|
||||||
|
wallet_close(&wallet_name) |
||||||
|
.await |
||||||
|
.map_err(|e| Into::<PyNgError>::into(e))?; |
||||||
|
Ok(()) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
#[pymodule] |
||||||
|
fn nextgraph(m: &Bound<'_, PyModule>) -> PyResult<()> { |
||||||
|
m.add_function(wrap_pyfunction!(wallet_open_with_mnemonic_words, m)?)?; |
||||||
|
m.add_function(wrap_pyfunction!(doc_sparql_update, m)?)?; |
||||||
|
m.add_function(wrap_pyfunction!(disconnect_and_close, m)?)?; |
||||||
|
Ok(()) |
||||||
|
} |
@ -0,0 +1,29 @@ |
|||||||
|
import asyncio |
||||||
|
from nextgraph import wallet_open_with_mnemonic_words, doc_sparql_update, disconnect_and_close |
||||||
|
|
||||||
|
async def main(): |
||||||
|
wallet_session = await wallet_open_with_mnemonic_words( |
||||||
|
"/Users/nl/Downloads/wallet-Hr-UITwGtjE1k6lXBoVGzD4FQMiDkM3T6bSeAi9PXt4A.ngw", |
||||||
|
["jealous", |
||||||
|
"during", |
||||||
|
"elevator", |
||||||
|
"swallow", |
||||||
|
"pen", |
||||||
|
"phone", |
||||||
|
"like", |
||||||
|
"employ", |
||||||
|
"myth", |
||||||
|
"remember", |
||||||
|
"question", |
||||||
|
"lemon"], |
||||||
|
[2, 3, 2, 3]) |
||||||
|
wallet_name = wallet_session[0] |
||||||
|
session_info = wallet_session[1] |
||||||
|
print(wallet_name) |
||||||
|
print(session_info) |
||||||
|
await doc_sparql_update(session_info["session_id"], |
||||||
|
"INSERT DATA { <did:ng:_> <example:predicate> \"An example value16\". }", |
||||||
|
"did:ng:o:Dn0QpE9_4jhta1mUWRl_LZh1SbXUkXfOB5eu38PNIk4A:v:Z4ihjV3KMVIqBxzjP6hogVLyjkZunLsb7MMsCR0kizQA") |
||||||
|
await disconnect_and_close(session_info["user"], wallet_name) |
||||||
|
|
||||||
|
asyncio.run(main()) |
Loading…
Reference in new issue