Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
feat: add NUT-01 mint getKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzizub committed Feb 11, 2024
1 parent c792e23 commit c1e66f0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Library for [Cashu](https://github.com/cashubtc) wallets written in Dart.

Implemented [NUTs](https://github.com/cashubtc/nuts/):

- [] [NUT-00](https://github.com/cashubtc/nuts/blob/main/00.md)
- [] [NUT-01](https://github.com/cashubtc/nuts/blob/main/01.md)
- [x] [NUT-00](https://github.com/cashubtc/nuts/blob/main/00.md)
- [x] [NUT-01](https://github.com/cashubtc/nuts/blob/main/01.md)
- [] [NUT-02](https://github.com/cashubtc/nuts/blob/main/02.md)
- [] [NUT-03](https://github.com/cashubtc/nuts/blob/main/03.md)
- [] [NUT-04](https://github.com/cashubtc/nuts/blob/main/04.md)
Expand Down
28 changes: 28 additions & 0 deletions lib/src/mint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:cashu_dart/src/model/response/get_keys.dart';
import 'package:cashu_dart/src/utils/request.dart';

/// Class that handles Cashu Mint API.
class CashuMint {
/// Takes [String] url as a parameter
CashuMint({
required this.url,
});

final String url;

Future<GetKeysResponse?> getKeys({
String? keysetId,
}) async {
var endpoint = '$url/v1/keys';

if (keysetId != null) {
endpoint = '$endpoint/$keysetId';
}

final requestedData = await request(endpoint, 'GET', null);

final respon =
requestedData != null ? GetKeysResponse.fromJson(requestedData) : null;
return respon;
}
}
37 changes: 37 additions & 0 deletions lib/src/model/response/get_keys.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:cashu_dart/src/model/types.dart';

class GetKeysResponse {
GetKeysResponse({
required this.keysets,
});

final List<KeySet> keysets;

factory GetKeysResponse.fromJson(Map<String, dynamic> json) {
return GetKeysResponse(
keysets: List<KeySet>.from(
json['keysets'].map((x) => KeySet.fromJson(x)),
),
);
}
}

class KeySet {
KeySet({
required this.id,
required this.unit,
required this.keys,
});

final String id;
final String unit;
final MintKeys keys;

factory KeySet.fromJson(Map<String, dynamic> json) {
return KeySet(
id: json['id'] as String,
unit: json['unit'] as String,
keys: Map<String, String>.from(json['keys'] as Map<String, dynamic>),
);
}
}
39 changes: 39 additions & 0 deletions lib/src/utils/request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'dart:io';

import 'package:dio/dio.dart';

Future<Map<String, dynamic>?> request(
String url,
String method,
String? body,
) async {
if (method == 'GET') {
return await _getRequest(url);
} else if (method == 'POST') {
return await _postRequest(url, body);
} else {
return null;
}
}

Future<Map<String, dynamic>?> _getRequest(String url) async {
final dio = Dio();
final response = await dio.get<Map<String, dynamic>>(url);

if (response.statusCode == HttpStatus.ok) {
return response.data;
} else {
return null;
}
}

Future<Map<String, dynamic>?> _postRequest(String url, String? body) async {
final dio = Dio();
final response = await dio.post<Map<String, dynamic>>(url, data: body);

if (response.statusCode == HttpStatus.ok) {
return response.data;
} else {
return null;
}
}
9 changes: 9 additions & 0 deletions lib/src/wallet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:cashu_dart/src/mint.dart';

class CashuWallet {
CashuWallet({
required this.cashuMint,
});

final CashuMint cashuMint;
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
dependencies:
convert: ^3.1.1
crypto: ^3.0.3
dio: ^5.4.0
pointycastle: ^3.7.3
typed_data: ^1.3.2

Expand Down

0 comments on commit c1e66f0

Please sign in to comment.