Skip to content

Commit f202e56

Browse files
committed
feat: initial version
0 parents  commit f202e56

17 files changed

+1472
-0
lines changed

.gitignore

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
.idea/
2+
.vscode/
3+
4+
### Linux template
5+
*~
6+
7+
# temporary files which can be created if a process still has a handle open of a deleted file
8+
.fuse_hidden*
9+
10+
# KDE directory preferences
11+
.directory
12+
13+
# Linux trash folder which might appear on any partition or disk
14+
.Trash-*
15+
16+
# .nfs files are created when an open file is removed but is still being accessed
17+
.nfs*
18+
19+
### Windows template
20+
# Windows thumbnail cache files
21+
Thumbs.db
22+
Thumbs.db:encryptable
23+
ehthumbs.db
24+
ehthumbs_vista.db
25+
26+
# Dump file
27+
*.stackdump
28+
29+
# Folder config file
30+
[Dd]esktop.ini
31+
32+
# Recycle Bin used on file shares
33+
$RECYCLE.BIN/
34+
35+
# Windows Installer files
36+
*.cab
37+
*.msi
38+
*.msix
39+
*.msm
40+
*.msp
41+
42+
# Windows shortcuts
43+
*.lnk
44+
45+
### macOS template
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
### Dart template
74+
# See https://www.dartlang.org/guides/libraries/private-files
75+
76+
# Files and directories created by pub
77+
.dart_tool/
78+
.packages
79+
build/
80+
# If you're building an application, you may want to check-in your pubspec.lock
81+
pubspec.lock
82+
83+
# Directory created by dartdoc
84+
# If you don't generate documentation locally you can remove this line.
85+
doc/api/
86+
87+
# dotenv environment variables file
88+
.env*
89+
90+
# Avoid committing generated Javascript files:
91+
*.dart.js
92+
*.info.json # Produced by the --dump-info flag.
93+
*.js # When generated by dart2js. Don't specify *.js if your
94+
# project includes source files written in JavaScript.
95+
*.js_
96+
*.js.deps
97+
*.js.map
98+
99+
.flutter-plugins
100+
.flutter-plugins-dependencies
101+

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial version.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 YogiLiu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# dart_multiaddr
2+
3+
> [multiaddr](https://github.com/multiformats/multiaddr) implementation in Dart
4+
5+
Multiaddr aims to make network addresses future-proof, composable, and efficient.
6+
7+
**Warning**: This is a work in progress. The API is not stable yet.
8+
9+
## Usage
10+
11+
```dart
12+
import 'package:dart_multiaddr/dart_multiaddr.dart';
13+
14+
void main() {
15+
var addr = Multiaddr.fromString('/ip4/1.1.1.1');
16+
print(addr.toString()); // Output: /ip4/1.1.1.1
17+
18+
var anotherAddr = Multiaddr.fromString('/ip4/1.1.1.1');
19+
print(addr == anotherAddr); // Output: true
20+
}
21+
```
22+
23+
## License
24+
25+
[MIT](./LICENSE) © 2023 YogiLiu

analysis_options.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options

example/example.dart

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:dart_multiaddr/dart_multiaddr.dart';
2+
3+
void main() {
4+
var addr = Multiaddr.fromString('/ip4/1.1.1.1');
5+
print(addr.toString()); // Output: /ip4/1.1.1.1
6+
7+
var anotherAddr = Multiaddr.fromString('/ip4/1.1.1.1');
8+
print(addr == anotherAddr); // Output: true
9+
}

lib/dart_multiaddr.dart

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Composable and future-proof network addresses.
2+
///
3+
/// Read more: [multiformats/multiaddr](https://github.com/multiformats/multiaddr)
4+
library;
5+
6+
export 'src/exception.dart';
7+
export 'src/multiaddr.dart';
8+
export 'src/protocol.dart';

lib/src/codec.dart

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
4+
import 'exception.dart';
5+
6+
/// Encode [value] to bytes.
7+
typedef Encoder = Uint8List Function(String value);
8+
9+
/// Decode [bytes] to String.
10+
typedef Decoder = String Function(Uint8List bytes);
11+
12+
////////////////////////////////////////////////////////////////////////////////
13+
// IP4 Encoder and Decoder
14+
// TODO: support Web
15+
////////////////////////////////////////////////////////////////////////////////
16+
Uint8List ip4Encoder(String value) {
17+
InternetAddress addr;
18+
try {
19+
addr = InternetAddress(value);
20+
} catch (_) {
21+
throw EncodeException('invalid ip4 value: $value');
22+
}
23+
return addr.rawAddress;
24+
}
25+
26+
String ip4Decoder(Uint8List value) {
27+
InternetAddress addr;
28+
try {
29+
addr = InternetAddress.fromRawAddress(value);
30+
} catch (_) {
31+
throw DecodeException('invalid ip4 value: $value');
32+
}
33+
return addr.address;
34+
}
35+
36+
////////////////////////////////////////////////////////////////////////////////
37+
// TCP Encoder and Decoder
38+
////////////////////////////////////////////////////////////////////////////////
39+
Uint8List tcpEncoder(String value) {
40+
var port = int.tryParse(value);
41+
if (port == null || port < 0 || port > 65535) {
42+
throw EncodeException('invalid tcp value: $value');
43+
}
44+
return Uint8List.fromList([port >> 8, port & 0xff]);
45+
}
46+
47+
String tcpDecoder(Uint8List value) {
48+
if (value.length != 2) {
49+
throw DecodeException('invalid tcp value: $value');
50+
}
51+
return '${value[0] << 8 | value[1]}';
52+
}
53+
54+
////////////////////////////////////////////////////////////////////////////////
55+
// IP6 Encoder and Decoder
56+
// TODO: support Web
57+
////////////////////////////////////////////////////////////////////////////////
58+
Uint8List ip6Encoder(String value) {
59+
// TODO: support IPv4-mapped IPv6 addresses
60+
InternetAddress addr;
61+
try {
62+
addr = InternetAddress(value);
63+
} catch (_) {
64+
throw EncodeException('invalid ip6 value: $value');
65+
}
66+
return addr.rawAddress;
67+
}
68+
69+
String ip6Decoder(Uint8List value) {
70+
InternetAddress addr;
71+
try {
72+
addr = InternetAddress.fromRawAddress(value);
73+
} catch (_) {
74+
throw DecodeException('invalid ip6 value: $value');
75+
}
76+
return addr.address;
77+
}
78+
79+
////////////////////////////////////////////////////////////////////////////////
80+
// DNS* Encoder and Decoder
81+
////////////////////////////////////////////////////////////////////////////////
82+
Uint8List dnsEncoder(String value) => Uint8List.fromList(value.codeUnits);
83+
84+
String dnsDecoder(Uint8List bytes) => String.fromCharCodes(bytes);
85+
86+
////////////////////////////////////////////////////////////////////////////////
87+
// UNIX Encoder and Decoder
88+
////////////////////////////////////////////////////////////////////////////////
89+
Uint8List unixEncoder(String value) => Uint8List.fromList(value.codeUnits);
90+
91+
String unixDecoder(Uint8List bytes) => String.fromCharCodes(bytes);

lib/src/exception.dart

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/// Base exception for all multiaddr exceptions.
2+
abstract class BaseMultiaddrException implements Exception {
3+
/// The type of the exception.
4+
abstract final String _type;
5+
6+
final String message;
7+
8+
BaseMultiaddrException(this.message);
9+
10+
@override
11+
String toString() {
12+
return '$_type: $message';
13+
}
14+
}
15+
16+
/// Base exception for all varint exceptions.
17+
abstract class VarintException extends BaseMultiaddrException {
18+
VarintException(String message) : super(message);
19+
}
20+
21+
/// Varint exception for encoding.
22+
class EncodeVarintException extends VarintException {
23+
@override
24+
final String _type = 'EncodeVarintException';
25+
26+
EncodeVarintException(String message) : super(message);
27+
}
28+
29+
/// Varint exception for decoding.
30+
class DecodeVarintException extends VarintException {
31+
@override
32+
final String _type = 'DecodeVarintException';
33+
34+
DecodeVarintException(String message) : super(message);
35+
}
36+
37+
/// Protocol exception
38+
class ProtocolException extends BaseMultiaddrException {
39+
@override
40+
final String _type = 'ProtocolException';
41+
42+
ProtocolException(String message) : super(message);
43+
}
44+
45+
/// Base encode and decode exception
46+
abstract class CodecException extends BaseMultiaddrException {
47+
CodecException(String message) : super(message);
48+
}
49+
50+
/// Encode exception
51+
class EncodeException extends CodecException {
52+
@override
53+
final String _type = 'EncodeException';
54+
55+
EncodeException(String message) : super(message);
56+
}
57+
58+
/// Decode exception
59+
class DecodeException extends CodecException {
60+
@override
61+
final String _type = 'DecodeException';
62+
63+
DecodeException(String message) : super(message);
64+
}
65+
66+
/// Multiaddr exception
67+
class MultiaddrException extends BaseMultiaddrException {
68+
@override
69+
final String _type = 'MultiaddrException';
70+
71+
MultiaddrException(String message) : super(message);
72+
}

0 commit comments

Comments
 (0)