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

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzizub committed Jan 9, 2024
0 parents commit c165b4d
Show file tree
Hide file tree
Showing 18 changed files with 522 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files and directories created by pub
.dart_tool/
.packages
pubspec.lock

# Temporary Files
.tmp/

# Android studio and IntelliJ
.idea

# Misc files
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dominik Šimoník

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
### Cashu

[![License: MIT][license_badge]][license_link]

⚠️ **Warning:** This project is work in progress

Library for [Cashu](https://github.com/cashubtc) wallets written in Dart.

## Supported NUTs

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)
- [] [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)
- [] [NUT-05](https://github.com/cashubtc/nuts/blob/main/05.md)
- [] [NUT-06](https://github.com/cashubtc/nuts/blob/main/06.md)
- [] [NUT-07](https://github.com/cashubtc/nuts/blob/main/07.md)
- [] [NUT-08](https://github.com/cashubtc/nuts/blob/main/08.md)
- [] [NUT-09](https://github.com/cashubtc/nuts/blob/main/09.md)
- [] [NUT-10](https://github.com/cashubtc/nuts/blob/main/10.md)
- [] [NUT-11](https://github.com/cashubtc/nuts/blob/main/11.md)
- [] [NUT-12](https://github.com/cashubtc/nuts/blob/main/12.md)

## Contribute

Contributions are very welcome.

If you want to contribute, please open an Issue or a PR.

[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT

1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flutter_lints/flutter.yaml
3 changes: 3 additions & 0 deletions lib/cashu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library;

export 'src/cashu.dart';
19 changes: 19 additions & 0 deletions lib/src/base64.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:convert';

String base64UrlToBase64(String base64Url) {
return base64Url.replaceAll('_', '/').replaceAll('-', '+');
}

String base64ToBase64Url(String base64) {
return base64.replaceAll('+', '-').replaceAll('/', '_');
}

Map<String, dynamic> base64toJson(String base64) {
// Directly decode the Base64 string, which may include padding characters
return jsonDecode(utf8.decode(base64Decode(base64))) as Map<String, dynamic>;
}

String jsonToBase64(Map<String, dynamic> json) {
// Encode the JSON to a UTF-8 byte array, then to a standard Base64 string
return base64Encode(utf8.encode(jsonEncode(json)));
}
38 changes: 38 additions & 0 deletions lib/src/cashu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:cashu/src/base64.dart';
import 'package:cashu/src/constants.dart';
import 'package:cashu/src/model/cashu_token.dart';

class Cashu {
static CashuToken getDecodedToken(String serialized) {
final prefix = serialized.startsWith(kTokenPrefix);

if (!prefix) {
throw Exception('Invalid token prefix');
}

final version =
serialized.substring(kTokenPrefix.length, kTokenPrefix.length + 1);

if (!kSupportedVersions.contains(version)) {
throw Exception('Unsupported token version');
}

final base64Dirty = serialized.substring(kTokenPrefix.length + 1);

final cleanedBase64url = base64UrlToBase64(base64Dirty);

final json = base64toJson(cleanedBase64url);

return CashuToken.fromJson(json);
}

static CashuToken getDecodedTokenUri(Uri uri) {
if (kCashuUriSchemes.contains(uri.scheme)) {
throw Exception('Invalid scheme');
}

final token = uri.host;

return getDecodedToken(token);
}
}
4 changes: 4 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const kTokenPrefix = 'cashu';
const kTokenVersion3 = 'A';
const kSupportedVersions = [kTokenVersion3];
const kCashuUriSchemes = ['cashu:', 'cashu://', 'web+cashu://'];
13 changes: 13 additions & 0 deletions lib/src/helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:core';
import 'dart:typed_data';

import 'package:convert/convert.dart';

Uint8List hexDecode(String input) {
final list = hex.decode(input);
return Uint8List.fromList(list);
}

String hexEncode(Uint8List input) {
return hex.encode(input);
}
11 changes: 11 additions & 0 deletions lib/src/model/blind_signature.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class BlindSignature {
BlindSignature({
required this.amount,
required this.id,
required this.C_,
});

final int amount;
final String id;
final String C_;
}
13 changes: 13 additions & 0 deletions lib/src/model/blinded_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class BlindedMessage {
BlindedMessage({
required this.amount,
required this.id,
required this.B_,
});

final int amount;
final String id;
final String B_;
}

typedef BlindedMessages = List<BlindedMessage>;
46 changes: 46 additions & 0 deletions lib/src/model/cashu_token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:cashu/src/base64.dart';
import 'package:cashu/src/constants.dart';
import 'package:cashu/src/model/token.dart';

class CashuToken {
CashuToken({
required this.token,
this.unit,
this.memo,
});

final String? unit;
final String? memo;
final List<Token> token;

factory CashuToken.fromJson(Map<String, dynamic> json) {
final token = json['token'] as List<dynamic>;

final tokenList =
token.map((e) => Token.fromJson(e as Map<String, dynamic>)).toList();

final unit = json['unit'] as String?;
final memo = json['memo'] as String?;

return CashuToken(
token: tokenList,
unit: unit,
memo: memo,
);
}

Map<String, dynamic> toJson() => {
'token': token.map((e) => e.toJson()).toList(),
if (unit != null) 'unit': unit,
if (memo != null) 'memo': memo,
};

String serialize() {
final json = toJson();

final base64url = jsonToBase64(json);

final token = '$kTokenPrefix$kTokenVersion3$base64url';
return token;
}
}
9 changes: 9 additions & 0 deletions lib/src/model/error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ProtocolError {
ProtocolError({
required this.detail,
required this.code,
});

final String detail;
final int code;
}
31 changes: 31 additions & 0 deletions lib/src/model/proof.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Proof {
Proof({
required this.amount,
required this.id,
required this.secret,
required this.C,
});

final int amount;
final String id;
final String secret;
final String C;

factory Proof.fromJson(Map<String, dynamic> json) {
return Proof(
amount: json['amount'] as int,
id: json['id'] as String,
secret: json['secret'] as String,
C: json['C'] as String,
);
}

Map<String, dynamic> toJson() {
return {
'id': id,
'amount': amount,
'secret': secret,
'C': C,
};
}
}
30 changes: 30 additions & 0 deletions lib/src/model/token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:cashu/src/model/proof.dart';

class Token {
Token({
required this.mint,
required this.proofs,
});

final String mint;
final List<Proof> proofs;

factory Token.fromJson(Map<String, dynamic> json) {
final proofs = json['proofs'] as List<dynamic>;

final proofList =
proofs.map((e) => Proof.fromJson(e as Map<String, dynamic>)).toList();

return Token(
mint: json['mint'] as String,
proofs: proofList,
);
}

Map<String, dynamic> toJson() {
return {
'mint': mint,
'proofs': proofs.map((e) => e.toJson()).toList(),
};
}
}
41 changes: 41 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:typed_data';
import 'package:pointycastle/export.dart';

final secp256k1 = ECDomainParameters('secp256k1');

ECPoint hashToCurve(Uint8List secret) {
ECPoint? point;
while (point == null) {
final hash = sha256(secret);
final hashHex = bytesToHex(hash);
final pointX = '02$hashHex';
try {
point = pointFromHex(pointX);
} catch (e) {
print(e);
}
}
return point;
}

ECPoint pointFromHex(String hex) {
return secp256k1.curve.decodePoint(hexToBytes(hex))!;
}

Uint8List sha256(Uint8List data) {
final digest = SHA256Digest();
return Uint8List.fromList(digest.process(data));
}

String bytesToHex(Uint8List bytes) {
return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
}

Uint8List hexToBytes(String hex) {
final result = Uint8List(hex.length ~/ 2);
for (var i = 0; i < hex.length; i += 2) {
final number = int.parse(hex.substring(i, i + 2), radix: 16);
result[i ~/ 2] = number;
}
return result;
}
17 changes: 17 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: cashu
description: Library for cashu wallets
version: 0.1.0
repository: https://github.com/ryzizub/cashu

environment:
sdk: ^3.2.0

dependencies:
convert: ^3.1.1
crypto: ^3.0.3
pointycastle: ^3.7.3
typed_data: ^1.3.2

dev_dependencies:
flutter_lints: ^3.0.1
test: ^1.14.4
Loading

0 comments on commit c165b4d

Please sign in to comment.