-
Notifications
You must be signed in to change notification settings - Fork 3
PE-1136 Refactor ArConnect Signing #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
e911028
218f6a8
6a70d70
93f4e33
0a8366a
ac76928
db80642
3ced77d
b735c94
3b4e7ee
3f3c72e
a52b706
662eb40
5ea3bf1
b54c3ba
13da7d5
1e47ec1
ad6c18e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,23 @@ import 'dart:convert'; | |
import 'dart:typed_data'; | ||
|
||
import 'package:arweave/src/utils/bundle_tag_parser.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
import '../crypto/crypto.dart'; | ||
import '../utils.dart'; | ||
import 'models.dart'; | ||
|
||
part 'data_item.g.dart'; | ||
|
||
final MIN_BINARY_SIZE = 1044; | ||
|
||
/// ANS-104 [DataItem] | ||
/// Spec: https://github.com/joshbenaron/arweave-standards/blob/ans104/ans/ANS-104.md | ||
@JsonSerializable(explicitToJson: true) | ||
class DataItem implements TransactionBase { | ||
@JsonKey(defaultValue: 1) | ||
final int format = 1; | ||
|
||
@override | ||
String get id => _id; | ||
late String _id; | ||
|
@@ -34,9 +41,15 @@ class DataItem implements TransactionBase { | |
@override | ||
late Uint8List data; | ||
|
||
@JsonKey(name: 'data_size') | ||
String get dataSize => _dataSize; | ||
String _dataSize = '0'; | ||
|
||
@override | ||
String get signature => _signature; | ||
late String _signature; | ||
|
||
@JsonKey(ignore: true) | ||
late ByteBuffer binary; | ||
|
||
/// This constructor is reserved for JSON serialisation. | ||
|
@@ -49,13 +62,18 @@ class DataItem implements TransactionBase { | |
List<Tag>? tags, | ||
String? data, | ||
Uint8List? dataBytes, | ||
String? dataSize, | ||
}) : target = target ?? '', | ||
nonce = nonce ?? '', | ||
_owner = owner ?? '', | ||
data = data != null | ||
? decodeBase64ToBytes(data) | ||
: (dataBytes ?? Uint8List(0)), | ||
_tags = tags ?? []; | ||
_tags = tags ?? [] { | ||
if (dataSize != null) { | ||
_dataSize = dataSize; | ||
} | ||
} | ||
|
||
/// Constructs a [DataItem] with the specified JSON data and appropriate Content-Type tag. | ||
factory DataItem.withJsonData({ | ||
|
@@ -87,6 +105,7 @@ class DataItem implements TransactionBase { | |
nonce: nonce, | ||
tags: tags, | ||
dataBytes: data, | ||
dataSize: data.lengthInBytes.toString(), | ||
); | ||
|
||
@override | ||
|
@@ -106,7 +125,7 @@ class DataItem implements TransactionBase { | |
Future<Uint8List> getSignatureData() => deepHash( | ||
[ | ||
utf8.encode('dataitem'), | ||
utf8.encode('1'), //Transaction format | ||
utf8.encode(format.toString()), //Transaction format | ||
utf8.encode('1'), //Signature type | ||
decodeBase64ToBytes(owner), | ||
decodeBase64ToBytes(target), | ||
|
@@ -119,8 +138,7 @@ class DataItem implements TransactionBase { | |
/// Signs the [DataItem] using the specified wallet and sets the `id` and `signature` appropriately. | ||
@override | ||
Future<Uint8List> sign(Wallet wallet) async { | ||
final signatureData = await getSignatureData(); | ||
final rawSignature = await wallet.sign(signatureData); | ||
final rawSignature = await wallet.sign(this); | ||
|
||
_signature = encodeBytesToBase64(rawSignature); | ||
|
||
|
@@ -287,4 +305,18 @@ class DataItem implements TransactionBase { | |
bytesBuilder.add(data); | ||
return bytesBuilder; | ||
} | ||
|
||
/// Encodes the [DataItem] as JSON with the `data` as the original unencoded [Uint8List]. | ||
@override | ||
Map<String, dynamic> toJson() => _$DataItemToJson(this); | ||
|
||
@override | ||
Map<String, dynamic> toUnsignedJson() => <String, dynamic>{ | ||
'format': format, | ||
'owner': owner, | ||
'tags': tags.map((e) => e.toJson()).toList(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid one-character identifiers such as |
||
'target': target, | ||
'data': data, | ||
'data_size': dataSize, | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.