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

Commit

Permalink
include protocol as part of MoneyAddress (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe authored Jun 25, 2024
1 parent f05f43d commit b2fd785
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
25 changes: 20 additions & 5 deletions lib/src/money_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
/// More info [here](https://github.com/TBD54566975/dap?tab=readme-ov-file#money-address)
class MoneyAddress {
final String currency;
final String css;
final String protocol;
final String pss;

String get urn => 'urn:$currency:$css';
String get urn => 'urn:$currency:$protocol:$pss';

MoneyAddress({required this.currency, required this.css});
MoneyAddress({
required this.currency,
required this.protocol,
required this.pss,
});

factory MoneyAddress.parse(String input) {
if (!input.startsWith('urn:')) {
throw FormatException('Expected urn. Got $input');
}

final urnless = input.substring(4);
final delimIdx = urnless.indexOf(':');
var delimIdx = urnless.indexOf(':');
if (delimIdx == -1) {
throw FormatException(
'Invalid money address. Expected urn:[currency]:[css]. Got $input');
Expand All @@ -26,9 +31,19 @@ class MoneyAddress {
final nid = urnless.substring(0, delimIdx);
final nss = urnless.substring(delimIdx + 1);

delimIdx = nss.indexOf(':');
if (delimIdx == -1) {
throw FormatException(
'Invalid money address. Expected nss to contain protocol. Got $nss');
}

final protocol = nss.substring(0, delimIdx);
final pss = nss.substring(delimIdx + 1);

return MoneyAddress(
currency: nid,
css: nss,
protocol: protocol,
pss: pss,
);
}

Expand Down
20 changes: 6 additions & 14 deletions test/money_address_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,26 @@ void main() {
final testCases = [
Vector(
input: 'urn:usdc:eth:0x2345y7432',
expected: MoneyAddress(
currency: 'usdc',
css: 'eth:0x2345y7432',
),
expected:
MoneyAddress(currency: 'usdc', protocol: 'eth', pss: '0x2345y7432'),
err: false,
),
Vector(
input: 'urn:btc:addr:m12345677axcv2345',
expected: MoneyAddress(
currency: 'btc',
css: 'addr:m12345677axcv2345',
),
currency: 'btc', protocol: 'addr', pss: 'm12345677axcv2345'),
err: false,
),
Vector(
input: 'urn:btc:lnurl:https://someurl.com',
expected: MoneyAddress(
currency: 'btc',
css: 'lnurl:https://someurl.com',
),
currency: 'btc', protocol: 'lnurl', pss: 'https://someurl.com'),
err: false,
),
Vector(
input: 'urn:btc:spaddr:sp1234abcd5678',
expected: MoneyAddress(
currency: 'btc',
css: 'spaddr:sp1234abcd5678',
),
currency: 'btc', protocol: 'spaddr', pss: 'sp1234abcd5678'),
err: false,
),
];
Expand All @@ -54,7 +46,7 @@ void main() {
} else {
final actual = MoneyAddress.parse(testCase.input);
expect(actual.currency, testCase.expected.currency);
expect(actual.css, testCase.expected.css);
expect(actual.protocol, testCase.expected.protocol);
expect(actual.urn, testCase.input);
}
});
Expand Down

0 comments on commit b2fd785

Please sign in to comment.