diff --git a/lib/src/model/transaction.dart b/lib/src/model/transaction.dart index 669e9ba6..ab8fd267 100644 --- a/lib/src/model/transaction.dart +++ b/lib/src/model/transaction.dart @@ -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 authorizedKeysBuffer = [ - Uint8List.fromList([ownership.authorizedPublicKeys!.length]) + Uint8List.fromList([bufAuthKeyLength.length]), + bufAuthKeyLength ]; + for (AuthorizedKey authorizedKey in ownership.authorizedPublicKeys!) { authorizedKeysBuffer .add(Uint8List.fromList(hexToUint8List(authorizedKey.publicKey!))); @@ -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([ encodeInt32(version!), Uint8List.fromList(hexToUint8List(address!)), @@ -434,13 +448,17 @@ class Transaction { Uint8List.fromList(utf8.encode(data!.code!)), bufContentSize, Uint8List.fromList(utf8.encode(data!.content!)), - Uint8List.fromList([data!.ownerships!.length]), + Uint8List.fromList([bufOwnershipLength.length]), + bufOwnershipLength, ownershipsBuffers, - Uint8List.fromList([data!.ledger!.uco!.transfers!.length]), + Uint8List.fromList([bufUCOTransferLength.length]), + bufUCOTransferLength, ucoTransfersBuffers, - Uint8List.fromList([data!.ledger!.token!.transfers!.length]), + Uint8List.fromList([bufTokenTransferLength.length]), + bufTokenTransferLength, tokenTransfersBuffers, - Uint8List.fromList([data!.recipients!.length]), + Uint8List.fromList([bufRecipientLength.length]), + bufRecipientLength, recipients ]); } diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 05254cd0..cba36269 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -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; +} diff --git a/pubspec.lock b/pubspec.lock index 5aa457e0..00185de7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/test/transaction_test.dart b/test/transaction_test.dart index 374c401d..c1a8a380 100644 --- a/test/transaction_test.dart +++ b/test/transaction_test.dart @@ -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([1]), //Nb of ownerships Uint8List.fromList([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([1]), // Nb of authorized keys Uint8List.fromList([1]), // Authorized keys encoding @@ -173,6 +177,8 @@ void main() { Uint8List.fromList(hexToUint8List( '00501fa2db78bcf8ceca129e6139d7e38bf0d61eb905441056b9ebe6f1d1feaf88')) ]), + // Nb of bytes to encode nb of uco transfers + Uint8List.fromList([1]), // Nb of uco transfers Uint8List.fromList([1]), concatUint8List([ @@ -180,6 +186,8 @@ void main() { '0000b1d3750edb9381c96b1a975a55b5b4e4fb37bfab104c10b0b6c9a00433ec4646')), encodeBigInt(toBigInt(0.2020)) ]), + // Nb of byte to encode nb of Token transfers + Uint8List.fromList([1]), // Nb of token transfers Uint8List.fromList([1]), concatUint8List([ @@ -190,6 +198,8 @@ void main() { encodeBigInt(toBigInt(100)), Uint8List.fromList([0]) ]), + // Nb of byte to encode nb of recipients + Uint8List.fromList([1]), // Nb of recipients Uint8List.fromList([1]), Uint8List.fromList(hexToUint8List( @@ -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([1]), //Nb of ownerships Uint8List.fromList([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([1]), // Nb of authorized keys Uint8List.fromList([1]), // Authorized keys encoding @@ -315,6 +329,8 @@ void main() { Uint8List.fromList(hexToUint8List( '00501fa2db78bcf8ceca129e6139d7e38bf0d61eb905441056b9ebe6f1d1feaf88')) ]), + // Nb of bytes to encode nb of uco transfers + Uint8List.fromList([1]), // Nb of uco transfers Uint8List.fromList([1]), concatUint8List([ @@ -322,6 +338,8 @@ void main() { '0000b1d3750edb9381c96b1a975a55b5b4e4fb37bfab104c10b0b6c9a00433ec4646')), encodeBigInt(toBigInt(0.2020)) ]), + // Nb of bytes to encode nb of Token transfers + Uint8List.fromList([1]), // Nb of token transfers Uint8List.fromList([1]), concatUint8List([ @@ -332,6 +350,8 @@ void main() { encodeBigInt(toBigInt(100)), Uint8List.fromList([0]), ]), + // Nb of bytes to encode nb of recipients + Uint8List.fromList([1]), // Nb of recipients Uint8List.fromList([1]), Uint8List.fromList(hexToUint8List( diff --git a/test/utils_test.dart b/test/utils_test.dart index 94a6f29f..5cabef76 100644 --- a/test/utils_test.dart +++ b/test/utils_test.dart @@ -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), [0]); + expect(toByteArray(123), [123]); + expect(toByteArray(258), [1, 2]); + expect(toByteArray(65535), [255, 255]); + expect(toByteArray(65536), [1, 0, 0]); + }); + }); }