Skip to content

Commit

Permalink
Implement VarInt when getting signature payload #25
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jul 21, 2022
1 parent 0c75124 commit 80d593e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
28 changes: 23 additions & 5 deletions lib/src/model/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,14 @@ class Transaction {

Uint8List ownershipsBuffers = Uint8List(0);
for (Ownership ownership in data!.ownerships!) {
final Uint8List bufAuthKeyLength = Uint8List.fromList(
toByteArray(ownership.authorizedPublicKeys!.length));

final List<Uint8List> authorizedKeysBuffer = <Uint8List>[
Uint8List.fromList(<int>[ownership.authorizedPublicKeys!.length])
Uint8List.fromList(<int>[bufAuthKeyLength.length]),
bufAuthKeyLength
];

for (AuthorizedKey authorizedKey in ownership.authorizedPublicKeys!) {
authorizedKeysBuffer
.add(Uint8List.fromList(hexToUint8List(authorizedKey.publicKey!)));
Expand Down Expand Up @@ -426,6 +431,15 @@ class Transaction {
]);
}

final Uint8List bufOwnershipLength =
Uint8List.fromList(toByteArray(data!.ownerships!.length));
final Uint8List bufUCOTransferLength =
Uint8List.fromList(toByteArray(data!.ledger!.uco!.transfers!.length));
final Uint8List bufTokenTransferLength =
Uint8List.fromList(toByteArray(data!.ledger!.token!.transfers!.length));
final Uint8List bufRecipientLength =
Uint8List.fromList(toByteArray(data!.recipients!.length));

return concatUint8List(<Uint8List>[
encodeInt32(version!),
Uint8List.fromList(hexToUint8List(address!)),
Expand All @@ -434,13 +448,17 @@ class Transaction {
Uint8List.fromList(utf8.encode(data!.code!)),
bufContentSize,
Uint8List.fromList(utf8.encode(data!.content!)),
Uint8List.fromList(<int>[data!.ownerships!.length]),
Uint8List.fromList(<int>[bufOwnershipLength.length]),
bufOwnershipLength,
ownershipsBuffers,
Uint8List.fromList(<int>[data!.ledger!.uco!.transfers!.length]),
Uint8List.fromList(<int>[bufUCOTransferLength.length]),
bufUCOTransferLength,
ucoTransfersBuffers,
Uint8List.fromList(<int>[data!.ledger!.token!.transfers!.length]),
Uint8List.fromList(<int>[bufTokenTransferLength.length]),
bufTokenTransferLength,
tokenTransfersBuffers,
Uint8List.fromList(<int>[data!.recipients!.length]),
Uint8List.fromList(<int>[bufRecipientLength.length]),
bufRecipientLength,
recipients
]);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,21 @@ BigInt toBigInt(double? number) {
number = number * pow(10, 8);
return BigInt.from(number.toInt());
}

/// Convert any number into a byte array
Uint8List toByteArray(int value) {
final BigInt byteMask = BigInt.from(0xff);
BigInt number = BigInt.from(value);
// Not handling negative numbers. Decide how you want to do that.
final int size = (number.bitLength + 7) >> 3;
if (size == 0) {
return Uint8List.fromList([0]);
}

Uint8List result = Uint8List(size);
for (int i = 0; i < size; i++) {
result[size - i - 1] = (number & byteMask).toInt();
number = number >> 8;
}
return result;
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ packages:
name: pointycastle
url: "https://pub.dartlang.org"
source: hosted
version: "3.6.0"
version: "3.6.1"
pool:
dependency: transitive
description:
Expand Down
20 changes: 20 additions & 0 deletions test/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,15 @@ void main() {
//Content size
encodeInt32(content.length),
Uint8List.fromList(utf8.encode(content)),
//Nb of bytes to encode nb of ownerships
Uint8List.fromList(<int>[1]),
//Nb of ownerships
Uint8List.fromList(<int>[1]),
//Secret size
encodeInt32(Uint8List.fromList(hexToUint8List(secret)).lengthInBytes),
Uint8List.fromList(Uint8List.fromList(hexToUint8List(secret))),
// Nb of byte to encode nb of authorized keys
Uint8List.fromList(<int>[1]),
// Nb of authorized keys
Uint8List.fromList(<int>[1]),
// Authorized keys encoding
Expand All @@ -173,13 +177,17 @@ void main() {
Uint8List.fromList(hexToUint8List(
'00501fa2db78bcf8ceca129e6139d7e38bf0d61eb905441056b9ebe6f1d1feaf88'))
]),
// Nb of bytes to encode nb of uco transfers
Uint8List.fromList(<int>[1]),
// Nb of uco transfers
Uint8List.fromList(<int>[1]),
concatUint8List(<Uint8List>[
Uint8List.fromList(hexToUint8List(
'0000b1d3750edb9381c96b1a975a55b5b4e4fb37bfab104c10b0b6c9a00433ec4646')),
encodeBigInt(toBigInt(0.2020))
]),
// Nb of byte to encode nb of Token transfers
Uint8List.fromList(<int>[1]),
// Nb of token transfers
Uint8List.fromList(<int>[1]),
concatUint8List(<Uint8List>[
Expand All @@ -190,6 +198,8 @@ void main() {
encodeBigInt(toBigInt(100)),
Uint8List.fromList(<int>[0])
]),
// Nb of byte to encode nb of recipients
Uint8List.fromList(<int>[1]),
// Nb of recipients
Uint8List.fromList(<int>[1]),
Uint8List.fromList(hexToUint8List(
Expand Down Expand Up @@ -301,11 +311,15 @@ void main() {
//Content size
encodeInt32(content.length),
Uint8List.fromList(utf8.encode(content)),
// Nb of byte to encode nb of ownerships
Uint8List.fromList(<int>[1]),
//Nb of ownerships
Uint8List.fromList(<int>[1]),
//Secret size
encodeInt32(Uint8List.fromList(hexToUint8List(secret)).lengthInBytes),
Uint8List.fromList(Uint8List.fromList(hexToUint8List(secret))),
// Nb of bytes to encode nb of authorized key
Uint8List.fromList(<int>[1]),
// Nb of authorized keys
Uint8List.fromList(<int>[1]),
// Authorized keys encoding
Expand All @@ -315,13 +329,17 @@ void main() {
Uint8List.fromList(hexToUint8List(
'00501fa2db78bcf8ceca129e6139d7e38bf0d61eb905441056b9ebe6f1d1feaf88'))
]),
// Nb of bytes to encode nb of uco transfers
Uint8List.fromList(<int>[1]),
// Nb of uco transfers
Uint8List.fromList(<int>[1]),
concatUint8List(<Uint8List>[
Uint8List.fromList(hexToUint8List(
'0000b1d3750edb9381c96b1a975a55b5b4e4fb37bfab104c10b0b6c9a00433ec4646')),
encodeBigInt(toBigInt(0.2020))
]),
// Nb of bytes to encode nb of Token transfers
Uint8List.fromList(<int>[1]),
// Nb of token transfers
Uint8List.fromList(<int>[1]),
concatUint8List(<Uint8List>[
Expand All @@ -332,6 +350,8 @@ void main() {
encodeBigInt(toBigInt(100)),
Uint8List.fromList(<int>[0]),
]),
// Nb of bytes to encode nb of recipients
Uint8List.fromList(<int>[1]),
// Nb of recipients
Uint8List.fromList(<int>[1]),
Uint8List.fromList(hexToUint8List(
Expand Down
10 changes: 10 additions & 0 deletions test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ void main() {
expect(toBigInt(1.234567), BigInt.from(123456700));
});
});

group('toByteArray', () {
test('should encode an integer into a Uint8List', () {
expect(toByteArray(0), <int>[0]);
expect(toByteArray(123), <int>[123]);
expect(toByteArray(258), <int>[1, 2]);
expect(toByteArray(65535), <int>[255, 255]);
expect(toByteArray(65536), <int>[1, 0, 0]);
});
});
}

0 comments on commit 80d593e

Please sign in to comment.