Skip to content

Commit

Permalink
Add dids Idenitifer bindings (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Apr 17, 2024
1 parent 57c58eb commit 8d71e4b
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 2 deletions.
131 changes: 131 additions & 0 deletions bindings/kt/src/main/kotlin/web5/sdk/web5.kt
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ internal interface _UniFFILib : Library {
): RustBuffer.ByValue
fun uniffi_web5_fn_method_ed25199_verify(`ptr`: Pointer,`jwk`: Pointer,`payload`: RustBuffer.ByValue,`signature`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus,
): Unit
fun uniffi_web5_fn_free_identifier(`ptr`: Pointer,_uniffi_out_err: RustCallStatus,
): Unit
fun uniffi_web5_fn_constructor_identifier_new(`didUri`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus,
): Pointer
fun uniffi_web5_fn_free_jwk(`ptr`: Pointer,_uniffi_out_err: RustCallStatus,
): Unit
fun uniffi_web5_fn_constructor_jwk_new(`alg`: RustBuffer.ByValue,`kty`: RustBuffer.ByValue,`crv`: RustBuffer.ByValue,`d`: RustBuffer.ByValue,`x`: RustBuffer.ByValue,`y`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus,
Expand Down Expand Up @@ -545,6 +549,8 @@ internal interface _UniFFILib : Library {
): Short
fun uniffi_web5_checksum_constructor_ed25199_new(
): Short
fun uniffi_web5_checksum_constructor_identifier_new(
): Short
fun uniffi_web5_checksum_constructor_jwk_new(
): Short
fun uniffi_web5_checksum_constructor_localjwkmanager_new(
Expand Down Expand Up @@ -596,6 +602,9 @@ private fun uniffiCheckApiChecksums(lib: _UniFFILib) {
if (lib.uniffi_web5_checksum_constructor_ed25199_new() != 35935.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_checksum_constructor_identifier_new() != 54084.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_checksum_constructor_jwk_new() != 31971.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
Expand Down Expand Up @@ -977,6 +986,82 @@ public object FfiConverterTypeEd25199: FfiConverter<Ed25199, Pointer> {



public interface IdentifierInterface {

companion object
}


open class Identifier : FFIObject, IdentifierInterface {

constructor(pointer: Pointer): super(pointer)

/**
* This constructor can be used to instantiate a fake object.
*
* **WARNING: Any object instantiated with this constructor cannot be passed to an actual Rust-backed object.**
* Since there isn't a backing [Pointer] the FFI lower functions will crash.
* @param noPointer Placeholder value so we can have a constructor separate from the default empty one that may be
* implemented for classes extending [FFIObject].
*/
constructor(noPointer: NoPointer): super(noPointer)
constructor(`didUri`: String) :
this(
rustCallWithError(IdentifierException) { _status ->
_UniFFILib.INSTANCE.uniffi_web5_fn_constructor_identifier_new(FfiConverterString.lower(`didUri`),_status)
})

/**
* Disconnect the object from the underlying Rust object.
*
* It can be called more than once, but once called, interacting with the object
* causes an `IllegalStateException`.
*
* Clients **must** call this method once done with the object, or cause a memory leak.
*/
override protected fun freeRustArcPtr() {
this.pointer?.let { ptr ->
rustCall() { status ->
_UniFFILib.INSTANCE.uniffi_web5_fn_free_identifier(ptr, status)
}
}
}




companion object

}

public object FfiConverterTypeIdentifier: FfiConverter<Identifier, Pointer> {

override fun lower(value: Identifier): Pointer {
return value.callWithPointer { it }
}

override fun lift(value: Pointer): Identifier {
return Identifier(value)
}

override fun read(buf: ByteBuffer): Identifier {
// The Rust code always writes pointers as 8 bytes, and will
// fail to compile if they don't fit.
return lift(Pointer(buf.getLong()))
}

override fun allocationSize(value: Identifier) = 8

override fun write(value: Identifier, buf: ByteBuffer) {
// The Rust code always expects pointers written as 8 bytes,
// and will fail to compile if they don't fit.
buf.putLong(Pointer.nativeValue(lower(value)))
}
}




public interface JwkInterface {
fun `computeThumbprint`(): String

Expand Down Expand Up @@ -1313,6 +1398,52 @@ public object FfiConverterTypeCurve: FfiConverterRustBuffer<Curve> {



sealed class IdentifierException(message: String): Exception(message) {
// Each variant is a nested class
// Flat enums carries a string error message, so no special implementation is necessary.
class RegexPatternFailure(message: String) : IdentifierException(message)
class ParseFailure(message: String) : IdentifierException(message)


companion object ErrorHandler : CallStatusErrorHandler<IdentifierException> {
override fun lift(error_buf: RustBuffer.ByValue): IdentifierException = FfiConverterTypeIdentifierError.lift(error_buf)
}
}

public object FfiConverterTypeIdentifierError : FfiConverterRustBuffer<IdentifierException> {
override fun read(buf: ByteBuffer): IdentifierException {

return when(buf.getInt()) {
1 -> IdentifierException.RegexPatternFailure(FfiConverterString.read(buf))
2 -> IdentifierException.ParseFailure(FfiConverterString.read(buf))
else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
}

}

override fun allocationSize(value: IdentifierException): Int {
return 4
}

override fun write(value: IdentifierException, buf: ByteBuffer) {
when(value) {
is IdentifierException.RegexPatternFailure -> {
buf.putInt(1)
Unit
}
is IdentifierException.ParseFailure -> {
buf.putInt(2)
Unit
}
}.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
}

}





sealed class JwkException(message: String): Exception(message) {
// Each variant is a nested class
// Flat enums carries a string error message, so no special implementation is necessary.
Expand Down
Binary file modified bindings/kt/src/main/resources/natives/libweb5.dylib
Binary file not shown.
139 changes: 139 additions & 0 deletions bindings/swift/Sources/UniFFI/web5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,81 @@ public func FfiConverterTypeEd25199_lower(_ value: Ed25199) -> UnsafeMutableRawP



public protocol IdentifierProtocol : AnyObject {

}


public class Identifier:
IdentifierProtocol {
fileprivate let pointer: UnsafeMutableRawPointer

// TODO: We'd like this to be `private` but for Swifty reasons,
// we can't implement `FfiConverter` without making this `required` and we can't
// make it `required` without making it `public`.
required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) {
self.pointer = pointer
}
public convenience init(didUri: String) throws {
self.init(unsafeFromRawPointer: try rustCallWithError(FfiConverterTypeIdentifierError.lift) {
uniffi_web5_fn_constructor_identifier_new(
FfiConverterString.lower(didUri),$0)
})
}

deinit {
try! rustCall { uniffi_web5_fn_free_identifier(pointer, $0) }
}






}

public struct FfiConverterTypeIdentifier: FfiConverter {

typealias FfiType = UnsafeMutableRawPointer
typealias SwiftType = Identifier

public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> Identifier {
return Identifier(unsafeFromRawPointer: pointer)
}

public static func lower(_ value: Identifier) -> UnsafeMutableRawPointer {
return value.pointer
}

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Identifier {
let v: UInt64 = try readInt(&buf)
// The Rust code won't compile if a pointer won't fit in a UInt64.
// We have to go via `UInt` because that's the thing that's the size of a pointer.
let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v))
if (ptr == nil) {
throw UniffiInternalError.unexpectedNullPointer
}
return try lift(ptr!)
}

public static func write(_ value: Identifier, into buf: inout [UInt8]) {
// This fiddling is because `Int` is the thing that's the same size as a pointer.
// The Rust code won't compile if a pointer won't fit in a `UInt64`.
writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value)))))
}
}


public func FfiConverterTypeIdentifier_lift(_ pointer: UnsafeMutableRawPointer) throws -> Identifier {
return try FfiConverterTypeIdentifier.lift(pointer)
}

public func FfiConverterTypeIdentifier_lower(_ value: Identifier) -> UnsafeMutableRawPointer {
return FfiConverterTypeIdentifier.lower(value)
}



public protocol JwkProtocol : AnyObject {
func computeThumbprint() throws -> String

Expand Down Expand Up @@ -851,6 +926,67 @@ extension Curve: Equatable, Hashable {}



public enum IdentifierError {



// Simple error enums only carry a message
case RegexPatternFailure(message: String)

// Simple error enums only carry a message
case ParseFailure(message: String)


fileprivate static func uniffiErrorHandler(_ error: RustBuffer) throws -> Error {
return try FfiConverterTypeIdentifierError.lift(error)
}
}


public struct FfiConverterTypeIdentifierError: FfiConverterRustBuffer {
typealias SwiftType = IdentifierError

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> IdentifierError {
let variant: Int32 = try readInt(&buf)
switch variant {




case 1: return .RegexPatternFailure(
message: try FfiConverterString.read(from: &buf)
)

case 2: return .ParseFailure(
message: try FfiConverterString.read(from: &buf)
)


default: throw UniffiInternalError.unexpectedEnumCase
}
}

public static func write(_ value: IdentifierError, into buf: inout [UInt8]) {
switch value {




case .RegexPatternFailure(_ /* message is ignored*/):
writeInt(&buf, Int32(1))
case .ParseFailure(_ /* message is ignored*/):
writeInt(&buf, Int32(2))


}
}
}


extension IdentifierError: Equatable, Hashable {}

extension IdentifierError: Error { }

public enum JwkError {


Expand Down Expand Up @@ -1070,6 +1206,9 @@ private var initializationResult: InitializationResult {
if (uniffi_web5_checksum_constructor_ed25199_new() != 35935) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_web5_checksum_constructor_identifier_new() != 54084) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_web5_checksum_constructor_jwk_new() != 31971) {
return InitializationResult.apiChecksumMismatch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ RustBuffer uniffi_web5_fn_method_ed25199_sign(void*_Nonnull ptr, void*_Nonnull j
);
void uniffi_web5_fn_method_ed25199_verify(void*_Nonnull ptr, void*_Nonnull jwk, RustBuffer payload, RustBuffer signature, RustCallStatus *_Nonnull out_status
);
void uniffi_web5_fn_free_identifier(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void*_Nonnull uniffi_web5_fn_constructor_identifier_new(RustBuffer did_uri, RustCallStatus *_Nonnull out_status
);
void uniffi_web5_fn_free_jwk(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void*_Nonnull uniffi_web5_fn_constructor_jwk_new(RustBuffer alg, RustBuffer kty, RustBuffer crv, RustBuffer d, RustBuffer x, RustBuffer y, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -236,6 +240,9 @@ uint16_t uniffi_web5_checksum_method_localjwkmanager_sign(void
);
uint16_t uniffi_web5_checksum_constructor_ed25199_new(void

);
uint16_t uniffi_web5_checksum_constructor_identifier_new(void

);
uint16_t uniffi_web5_checksum_constructor_jwk_new(void

Expand Down
Binary file modified bindings/swift/libweb5-rs.xcframework/macos-arm64/libweb5.dylib
Binary file not shown.
1 change: 1 addition & 0 deletions bindings/uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license-file.workspace = true

[dependencies]
crypto = { path = "../../crates/crypto" }
dids = { path = "../../crates/dids" }
jwk = { path = "../../crates/jwk" }
keys = { path = "../../crates/keys" }
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "7cd3aac735e905e1725d350a7a82d57aa50caaa1", features = ["cli"] }
Expand Down
1 change: 0 additions & 1 deletion bindings/uniffi/kotlin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

set -e

cargo build --release
cargo run --bin uniffi-bindgen generate --library ../../target/release/libweb5.dylib --language kotlin --out-dir target/bindgen-kotlin

cp ../../target/release/libweb5.dylib ../kt/src/main/resources/natives
Expand Down
32 changes: 32 additions & 0 deletions bindings/uniffi/src/dids/identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use dids::identifier::{Identifier as InternalIdentifier, IdentifierError};

#[derive(Default)]
pub struct Identifier(InternalIdentifier);

impl From<InternalIdentifier> for Identifier {
fn from(value: InternalIdentifier) -> Self {
Self(value)
}
}

impl From<&Identifier> for InternalIdentifier {
fn from(value: &Identifier) -> Self {
Self {
uri: value.0.uri.clone(),
url: value.0.url.clone(),
method: value.0.method.clone(),
id: value.0.id.clone(),
params: value.0.params.clone(),
path: value.0.path.clone(),
query: value.0.query.clone(),
fragment: value.0.fragment.clone(),
}
}
}

impl Identifier {
pub fn new(did_uri: String) -> Result<Self, IdentifierError> {
let internal_identifier = InternalIdentifier::parse(&did_uri)?;
Ok(Identifier::from(internal_identifier))
}
}
1 change: 1 addition & 0 deletions bindings/uniffi/src/dids/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod identifier;
Loading

0 comments on commit 8d71e4b

Please sign in to comment.