Files
nym/sdk/ffi/go/src/lib.rs
mx ae6c80f0cd FFI share lib + initial uniffi-bindgen-go implementation (#4394)
* fixed rebase conflict with cargo.lock

* shared cleanup

* moved returncode to shared

* first pass at Go binding structure

* minor cleanup

* working on custom type udl

* trying to get LDFLAG script working

* commit before changing alias -> proper types

* converted CCallbacks from aliases to Struct

* cleanup comments

* temp

* push to share

* cleanup

* trait Lift not implemented for *const i8 issue

* test of refactor:
* move c-specific var casting out of shared/ into cpp/
* error returning in go/ over ffi boundary with uniffi

*  _internal functions ffi wrapper agnostic
* moved lang-specific type conversions to cpp / go bindings and out of
  shared
* got send_message working in c/c++ & go
* split out c/c++-specific types to mod

* cont. with making _internal fns lang agnostic
* working on final fn for C and shared (listening for incoming messages)

* fixed return err on listen_for_incoming

* got full example run running again after shared/ refactor

* removed unused struct

* code comments

* got first runthrough of go example code

* script cleanup

* clean up readme instructions

* clippy

* removed unused imports

* rustfmt

* Update sdk/ffi/go/README.md with link to example file

Co-authored-by: Mark Sinclair <14054343+mmsinclair@users.noreply.github.com>

* updated readme with extra build and usage info

* renamed binding outer directory for nicer path
* moved example file from ffi/main.go -> ./example.go

* updated README with new example file name

---------

Co-authored-by: mfahampshire <mfahampshire@pm.me>
Co-authored-by: Mark Sinclair <14054343+mmsinclair@users.noreply.github.com>
2024-03-18 15:15:59 +01:00

83 lines
2.6 KiB
Rust

// Copyright 2023-2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use nym_sdk::mixnet::Recipient;
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag;
uniffi::include_scaffolding!("bindings");
#[derive(Debug, thiserror::Error)]
enum GoWrapError {
#[error("Couldn't init client")]
ClientInitError {},
#[error("Client is uninitialised: init client first")]
ClientUninitialisedError {},
#[error("Error getting self address")]
SelfAddrError {},
#[error("Error sending message")]
SendMsgError {},
#[error("Error sending reply")]
ReplyError {},
#[error("Could not start listening")]
ListenError {},
}
#[no_mangle]
fn init_logging() {
nym_bin_common::logging::setup_logging();
}
#[no_mangle]
fn init_ephemeral() -> Result<(), GoWrapError> {
match nym_ffi_shared::init_ephemeral_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ClientInitError {}),
}
}
#[no_mangle]
fn get_self_address() -> Result<String, GoWrapError> {
match nym_ffi_shared::get_self_address_internal() {
Ok(addr) => Ok(addr),
Err(..) => Err(GoWrapError::SelfAddrError {}),
}
}
#[no_mangle]
fn send_message(recipient: String, message: String) -> Result<(), GoWrapError> {
let nym_recipient_type = Recipient::try_from_base58_string(recipient).unwrap();
match nym_ffi_shared::send_message_internal(nym_recipient_type, &message) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::SendMsgError {}),
}
}
#[no_mangle]
fn reply(recipient: Vec<u8>, message: String) -> Result<(), GoWrapError> {
let mut sized_array: [u8; 16] = [0; 16];
sized_array.copy_from_slice(&recipient[..16]);
let anon_recipient_type: AnonymousSenderTag = AnonymousSenderTag::from_bytes(sized_array);
match nym_ffi_shared::reply_internal(anon_recipient_type, &message) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ReplyError {}),
}
}
pub struct IncomingMessage {
message: String,
sender: Vec<u8>,
}
#[no_mangle]
fn listen_for_incoming() -> Result<IncomingMessage, GoWrapError> {
match nym_ffi_shared::listen_for_incoming_internal() {
Ok(received) => {
let message = String::from_utf8_lossy(&received.message).to_string();
// maybe change this to raw bytes to send over TODO
let sender = received.sender_tag.unwrap().to_bytes().to_vec(); //.to_base58_string();
let incoming = IncomingMessage { message, sender };
Ok(incoming)
}
Err(_) => Err(GoWrapError::ListenError {}),
}
}