Skip to content

Commit

Permalink
expose greenlight device credentials in udl
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed May 24, 2024
1 parent 595433e commit 7cdd002
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 45 deletions.
6 changes: 5 additions & 1 deletion libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ dictionary GreenlightCredentials {
sequence<u8> device_cert;
};

dictionary GreenlightDeviceCredentials {
sequence<u8> device;
};

[Enum]
interface NodeCredentials {
Greenlight(GreenlightCredentials credentials);
Greenlight(GreenlightDeviceCredentials credentials);
};

dictionary GreenlightNodeConfig {
Expand Down
40 changes: 20 additions & 20 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ use breez_sdk_core::{
BuyBitcoinProvider, BuyBitcoinRequest, BuyBitcoinResponse, ChannelState, CheckMessageRequest,
CheckMessageResponse, ClosedChannelPaymentDetails, Config, ConfigureNodeRequest,
ConnectRequest, CurrencyInfo, EnvironmentType, EventListener, FeeratePreset, FiatCurrency,
GreenlightCredentials, GreenlightNodeConfig, HealthCheckStatus, InputType, InvoicePaidDetails,
LNInvoice, ListPaymentsRequest, LnPaymentDetails, LnUrlAuthRequestData, LnUrlCallbackStatus,
LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlPayResult,
LnUrlPaySuccessData, LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult,
LnUrlWithdrawSuccessData, LocaleOverrides, LocalizedName, LogEntry, LogStream, LspInformation,
MaxReverseSwapAmountResponse, MessageSuccessActionData, MetadataFilter, MetadataItem, Network,
NodeConfig, NodeCredentials, NodeState, OnchainPaymentLimitsResponse, OpenChannelFeeRequest,
OpenChannelFeeResponse, OpeningFeeParams, OpeningFeeParamsMenu, PayOnchainRequest,
PayOnchainResponse, Payment, PaymentDetails, PaymentFailedData, PaymentStatus, PaymentType,
PaymentTypeFilter, PrepareOnchainPaymentRequest, PrepareOnchainPaymentResponse,
PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse, PrepareRefundRequest,
PrepareRefundResponse, Rate, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, RecommendedFees, RedeemOnchainFundsRequest, RedeemOnchainFundsResponse,
RefundRequest, RefundResponse, ReportIssueRequest, ReportPaymentFailureDetails,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint,
RouteHintHop, SendOnchainRequest, SendOnchainResponse, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, ServiceHealthCheckResponse, SignMessageRequest,
SignMessageResponse, StaticBackupRequest, StaticBackupResponse, SuccessActionProcessed,
SwapAmountType, SwapInfo, SwapStatus, Symbol, TlvEntry, UnspentTransactionOutput,
UrlSuccessActionData,
GreenlightCredentials, GreenlightDeviceCredentials, GreenlightNodeConfig, HealthCheckStatus,
InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails,
LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest,
LnUrlPayRequestData, LnUrlPayResult, LnUrlPaySuccessData, LnUrlWithdrawRequest,
LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData, LocaleOverrides,
LocalizedName, LogEntry, LogStream, LspInformation, MaxReverseSwapAmountResponse,
MessageSuccessActionData, MetadataFilter, MetadataItem, Network, NodeConfig, NodeCredentials,
NodeState, OnchainPaymentLimitsResponse, OpenChannelFeeRequest, OpenChannelFeeResponse,
OpeningFeeParams, OpeningFeeParamsMenu, PayOnchainRequest, PayOnchainResponse, Payment,
PaymentDetails, PaymentFailedData, PaymentStatus, PaymentType, PaymentTypeFilter,
PrepareOnchainPaymentRequest, PrepareOnchainPaymentResponse, PrepareRedeemOnchainFundsRequest,
PrepareRedeemOnchainFundsResponse, PrepareRefundRequest, PrepareRefundResponse, Rate,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees,
RedeemOnchainFundsRequest, RedeemOnchainFundsResponse, RefundRequest, RefundResponse,
ReportIssueRequest, ReportPaymentFailureDetails, ReverseSwapFeesRequest, ReverseSwapInfo,
ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop, SendOnchainRequest,
SendOnchainResponse, SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest,
ServiceHealthCheckResponse, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SuccessActionProcessed, SwapAmountType, SwapInfo, SwapStatus, Symbol,
TlvEntry, UnspentTransactionOutput, UrlSuccessActionData,
};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
Expand Down
13 changes: 13 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use crate::models::ConfigureNodeRequest;
use crate::models::ConnectRequest;
use crate::models::EnvironmentType;
use crate::models::GreenlightCredentials;
use crate::models::GreenlightDeviceCredentials;
use crate::models::GreenlightNodeConfig;
use crate::models::HealthCheckStatus;
use crate::models::ListPaymentsRequest;
Expand Down Expand Up @@ -1288,6 +1289,18 @@ impl rust2dart::IntoIntoDart<GreenlightCredentials> for GreenlightCredentials {
}
}

impl support::IntoDart for GreenlightDeviceCredentials {
fn into_dart(self) -> support::DartAbi {
vec![self.device.into_into_dart().into_dart()].into_dart()
}
}
impl support::IntoDartExceptPrimitive for GreenlightDeviceCredentials {}
impl rust2dart::IntoIntoDart<GreenlightDeviceCredentials> for GreenlightDeviceCredentials {
fn into_into_dart(self) -> Self {
self
}
}

impl support::IntoDart for GreenlightNodeConfig {
fn into_dart(self) -> support::DartAbi {
vec![
Expand Down
5 changes: 2 additions & 3 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,8 @@ impl NodeAPI for Greenlight {
self.persister.clone(),
)?
.map(|credentials| NodeCredentials::Greenlight {
credentials: GreenlightCredentials {
device_cert: credentials.cert,
device_key: credentials.key,
credentials: GreenlightDeviceCredentials {
device: credentials.to_bytes(),
},
}))
}
Expand Down
10 changes: 9 additions & 1 deletion libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,9 @@ pub enum NodeConfig {

#[derive(Clone, Serialize)]
pub enum NodeCredentials {
Greenlight { credentials: GreenlightCredentials },
Greenlight {
credentials: GreenlightDeviceCredentials,
},
}

#[derive(Clone)]
Expand All @@ -566,6 +568,12 @@ pub struct GreenlightCredentials {
pub device_cert: Vec<u8>,
}

/// Device credentials used to authenticate to Greenlight with the current device.
#[derive(Clone, Serialize, Deserialize)]
pub struct GreenlightDeviceCredentials {
pub device: Vec<u8>,
}

/// The different supported bitcoin networks
#[derive(Clone, Copy, Debug, Display, Eq, PartialEq, Serialize, Deserialize)]
pub enum Network {
Expand Down
25 changes: 23 additions & 2 deletions libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,15 @@ class GreenlightCredentials {
});
}

/// Device credentials used to authenticate to Greenlight with the current device.
class GreenlightDeviceCredentials {
final Uint8List device;

const GreenlightDeviceCredentials({
required this.device,
});
}

class GreenlightNodeConfig {
final GreenlightCredentials? partnerCredentials;
final String? inviteCode;
Expand Down Expand Up @@ -1184,7 +1193,7 @@ sealed class NodeConfig with _$NodeConfig {
@freezed
sealed class NodeCredentials with _$NodeCredentials {
const factory NodeCredentials.greenlight({
required GreenlightCredentials credentials,
required GreenlightDeviceCredentials credentials,
}) = NodeCredentials_Greenlight;
}

Expand Down Expand Up @@ -3231,6 +3240,10 @@ class BreezSdkCoreImpl implements BreezSdkCore {
return _wire2api_greenlight_credentials(raw);
}

GreenlightDeviceCredentials _wire2api_box_autoadd_greenlight_device_credentials(dynamic raw) {
return _wire2api_greenlight_device_credentials(raw);
}

GreenlightNodeConfig _wire2api_box_autoadd_greenlight_node_config(dynamic raw) {
return _wire2api_greenlight_node_config(raw);
}
Expand Down Expand Up @@ -3450,6 +3463,14 @@ class BreezSdkCoreImpl implements BreezSdkCore {
);
}

GreenlightDeviceCredentials _wire2api_greenlight_device_credentials(dynamic raw) {
final arr = raw as List<dynamic>;
if (arr.length != 1) throw Exception('unexpected arr length: expect 1 but see ${arr.length}');
return GreenlightDeviceCredentials(
device: _wire2api_uint_8_list(arr[0]),
);
}

GreenlightNodeConfig _wire2api_greenlight_node_config(dynamic raw) {
final arr = raw as List<dynamic>;
if (arr.length != 2) throw Exception('unexpected arr length: expect 2 but see ${arr.length}');
Expand Down Expand Up @@ -3811,7 +3832,7 @@ class BreezSdkCoreImpl implements BreezSdkCore {
switch (raw[0]) {
case 0:
return NodeCredentials_Greenlight(
credentials: _wire2api_box_autoadd_greenlight_credentials(raw[1]),
credentials: _wire2api_box_autoadd_greenlight_device_credentials(raw[1]),
);
default:
throw Exception("unreachable");
Expand Down
28 changes: 14 additions & 14 deletions libs/sdk-flutter/lib/bridge_generated.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4692,20 +4692,20 @@ abstract class NodeConfig_Greenlight implements NodeConfig {

/// @nodoc
mixin _$NodeCredentials {
GreenlightCredentials get credentials => throw _privateConstructorUsedError;
GreenlightDeviceCredentials get credentials => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(GreenlightCredentials credentials) greenlight,
required TResult Function(GreenlightDeviceCredentials credentials) greenlight,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(GreenlightCredentials credentials)? greenlight,
TResult? Function(GreenlightDeviceCredentials credentials)? greenlight,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(GreenlightCredentials credentials)? greenlight,
TResult Function(GreenlightDeviceCredentials credentials)? greenlight,
required TResult orElse(),
}) =>
throw _privateConstructorUsedError;
Expand Down Expand Up @@ -4735,7 +4735,7 @@ abstract class $NodeCredentialsCopyWith<$Res> {
factory $NodeCredentialsCopyWith(NodeCredentials value, $Res Function(NodeCredentials) then) =
_$NodeCredentialsCopyWithImpl<$Res, NodeCredentials>;
@useResult
$Res call({GreenlightCredentials credentials});
$Res call({GreenlightDeviceCredentials credentials});
}

/// @nodoc
Expand All @@ -4757,7 +4757,7 @@ class _$NodeCredentialsCopyWithImpl<$Res, $Val extends NodeCredentials>
credentials: null == credentials
? _value.credentials
: credentials // ignore: cast_nullable_to_non_nullable
as GreenlightCredentials,
as GreenlightDeviceCredentials,
) as $Val);
}
}
Expand All @@ -4769,7 +4769,7 @@ abstract class _$$NodeCredentials_GreenlightImplCopyWith<$Res> implements $NodeC
__$$NodeCredentials_GreenlightImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({GreenlightCredentials credentials});
$Res call({GreenlightDeviceCredentials credentials});
}

/// @nodoc
Expand All @@ -4789,7 +4789,7 @@ class __$$NodeCredentials_GreenlightImplCopyWithImpl<$Res>
credentials: null == credentials
? _value.credentials
: credentials // ignore: cast_nullable_to_non_nullable
as GreenlightCredentials,
as GreenlightDeviceCredentials,
));
}
}
Expand All @@ -4800,7 +4800,7 @@ class _$NodeCredentials_GreenlightImpl implements NodeCredentials_Greenlight {
const _$NodeCredentials_GreenlightImpl({required this.credentials});

@override
final GreenlightCredentials credentials;
final GreenlightDeviceCredentials credentials;

@override
String toString() {
Expand All @@ -4827,23 +4827,23 @@ class _$NodeCredentials_GreenlightImpl implements NodeCredentials_Greenlight {
@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(GreenlightCredentials credentials) greenlight,
required TResult Function(GreenlightDeviceCredentials credentials) greenlight,
}) {
return greenlight(credentials);
}

@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(GreenlightCredentials credentials)? greenlight,
TResult? Function(GreenlightDeviceCredentials credentials)? greenlight,
}) {
return greenlight?.call(credentials);
}

@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(GreenlightCredentials credentials)? greenlight,
TResult Function(GreenlightDeviceCredentials credentials)? greenlight,
required TResult orElse(),
}) {
if (greenlight != null) {
Expand Down Expand Up @@ -4882,11 +4882,11 @@ class _$NodeCredentials_GreenlightImpl implements NodeCredentials_Greenlight {
}

abstract class NodeCredentials_Greenlight implements NodeCredentials {
const factory NodeCredentials_Greenlight({required final GreenlightCredentials credentials}) =
const factory NodeCredentials_Greenlight({required final GreenlightDeviceCredentials credentials}) =
_$NodeCredentials_GreenlightImpl;

@override
GreenlightCredentials get credentials;
GreenlightDeviceCredentials get credentials;
@override
@JsonKey(ignore: true)
_$$NodeCredentials_GreenlightImplCopyWith<_$NodeCredentials_GreenlightImpl> get copyWith =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,39 @@ fun asGreenlightCredentialsList(arr: ReadableArray): List<GreenlightCredentials>
return list
}

fun asGreenlightDeviceCredentials(greenlightDeviceCredentials: ReadableMap): GreenlightDeviceCredentials? {
if (!validateMandatoryFields(
greenlightDeviceCredentials,
arrayOf(
"device",
),
)
) {
return null
}
val device = greenlightDeviceCredentials.getArray("device")?.let { asUByteList(it) }!!
return GreenlightDeviceCredentials(
device,
)
}

fun readableMapOf(greenlightDeviceCredentials: GreenlightDeviceCredentials): ReadableMap {
return readableMapOf(
"device" to readableArrayOf(greenlightDeviceCredentials.device),
)
}

fun asGreenlightDeviceCredentialsList(arr: ReadableArray): List<GreenlightDeviceCredentials> {
val list = ArrayList<GreenlightDeviceCredentials>()
for (value in arr.toArrayList()) {
when (value) {
is ReadableMap -> list.add(asGreenlightDeviceCredentials(value)!!)
else -> throw SdkException.Generic(errUnexpectedType("${value::class.java.name}"))
}
}
return list
}

fun asGreenlightNodeConfig(greenlightNodeConfig: ReadableMap): GreenlightNodeConfig? {
if (!validateMandatoryFields(
greenlightNodeConfig,
Expand Down Expand Up @@ -4259,7 +4292,7 @@ fun asNodeCredentials(nodeCredentials: ReadableMap): NodeCredentials? {
val type = nodeCredentials.getString("type")

if (type == "greenlight") {
return NodeCredentials.Greenlight(nodeCredentials.getMap("credentials")?.let { asGreenlightCredentials(it) }!!)
return NodeCredentials.Greenlight(nodeCredentials.getMap("credentials")?.let { asGreenlightDeviceCredentials(it) }!!)
}
return null
}
Expand Down
Loading

0 comments on commit 7cdd002

Please sign in to comment.