Skip to content

Commit

Permalink
Manage UTF-16 code units. #24
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jul 18, 2022
1 parent de386f0 commit e20691f
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 175 deletions.
3 changes: 1 addition & 2 deletions lib/src/model/data.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later
// Project imports:
import 'dart:typed_data';
import 'package:archethic_lib_dart/src/model/ledger.dart';
import 'package:archethic_lib_dart/src/model/ownership.dart';

Expand All @@ -20,7 +19,7 @@ class Data {
String? code;

/// Content: free zone for data hosting (string or hexadecimal)
Uint8List? content;
String? content;

/// Ownership: authorization/delegations containing list of secrets and their authorized public keys to proof the ownership
List<Ownership>? ownerships;
Expand Down
7 changes: 3 additions & 4 deletions lib/src/model/keychain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Keychain {
services!.forEach((String serviceName, Service service) {
servicesBuffer = concatUint8List(<Uint8List>[
servicesBuffer,
Uint8List.fromList(<int>[serviceName.codeUnits.length]),
Uint8List.fromList(<int>[serviceName.length]),
Uint8List.fromList(serviceName.codeUnits),
Uint8List.fromList(<int>[service.derivationPath!.codeUnits.length]),
Uint8List.fromList(<int>[service.derivationPath!.length]),
Uint8List.fromList(service.derivationPath!.codeUnits),
Uint8List.fromList(<int>[crypto.curveToID(service.curve!)]),
Uint8List.fromList(<int>[crypto.hashAlgoToID(service.hashAlgo!)])
Expand Down Expand Up @@ -182,8 +182,7 @@ Keychain decodeKeychain(Uint8List binary) {
final int hashAlgoId = binary.sublist(pos, pos + 1)[0].toInt();
pos++;

keychain.addService(
String.fromCharCodes(serviceName), String.fromCharCodes(derivationPath),
keychain.addService(utf8.decode(serviceName), utf8.decode(derivationPath),
curve: crypto.idToCurve(curveId),
hashAlgo: crypto.idToHashAlgo(hashAlgoId));
}
Expand Down
55 changes: 32 additions & 23 deletions lib/src/model/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import 'dart:convert';
import 'dart:typed_data';

// Package imports:
import 'package:hex/hex.dart';

// Project imports:
import 'package:archethic_lib_dart/src/model/authorized_key.dart';
import 'package:archethic_lib_dart/src/model/balance.dart';
Expand All @@ -16,6 +19,7 @@ import 'package:archethic_lib_dart/src/model/transaction_input.dart';
import 'package:archethic_lib_dart/src/model/uco_transfer.dart';
import 'package:archethic_lib_dart/src/model/validation_stamp.dart';
import 'package:archethic_lib_dart/src/utils/utils.dart';

import 'package:archethic_lib_dart/src/utils/crypto.dart' as crypto
show deriveKeyPair, sign, deriveAddress;

Expand Down Expand Up @@ -167,11 +171,10 @@ class Transaction {
throw "'content' must be a string or Uint8List";
}

if (content is String) {
data!.content = Uint8List.fromList(content.codeUnits);
} else {
data!.content = content;
if (content is Uint8List) {
content = utf8.decode(content);
}
data!.content = content;
return this;
}

Expand Down Expand Up @@ -357,32 +360,36 @@ class Transaction {
final Uint8List payloadForPreviousSignature = previousSignaturePayload();
return concatUint8List(<Uint8List>[
payloadForPreviousSignature,
hexToUint8List(previousPublicKey!),
Uint8List.fromList(<int>[hexToUint8List(previousSignature!).length]),
hexToUint8List(previousSignature!),
Uint8List.fromList(hexToUint8List(previousPublicKey!)),
Uint8List.fromList(
<int>[Uint8List.fromList(hexToUint8List(previousSignature!)).length]),
Uint8List.fromList(hexToUint8List(previousSignature!)),
]);
}

/// Generate the payload for the previous signature by encoding address, type and data
Uint8List previousSignaturePayload() {
final Uint8List bufCodeSize = encodeInt32(data!.code!.codeUnits.length);
final Uint8List bufContentSize = encodeInt32(data!.content!.length);
final Uint8List bufCodeSize = encodeInt32(data!.code!.length);
int contentSize = utf8.encode(data!.content!).length;
final Uint8List bufContentSize = encodeInt32(contentSize);

Uint8List ownershipsBuffers = Uint8List(0);
for (Ownership ownership in data!.ownerships!) {
final List<Uint8List> authorizedKeysBuffer = <Uint8List>[
Uint8List.fromList(<int>[ownership.authorizedPublicKeys!.length])
];
for (AuthorizedKey authorizedKey in ownership.authorizedPublicKeys!) {
authorizedKeysBuffer.add(hexToUint8List(authorizedKey.publicKey!));
authorizedKeysBuffer
.add(hexToUint8List(authorizedKey.encryptedSecretKey!));
.add(Uint8List.fromList(hexToUint8List(authorizedKey.publicKey!)));
authorizedKeysBuffer.add(Uint8List.fromList(
hexToUint8List(authorizedKey.encryptedSecretKey!)));
}

ownershipsBuffers = concatUint8List(<Uint8List>[
ownershipsBuffers,
encodeInt32(hexToUint8List(ownership.secret!).lengthInBytes),
hexToUint8List(ownership.secret!),
encodeInt32(Uint8List.fromList(hexToUint8List(ownership.secret!))
.lengthInBytes),
Uint8List.fromList(hexToUint8List(ownership.secret!)),
concatUint8List(authorizedKeysBuffer)
]);
}
Expand All @@ -392,7 +399,7 @@ class Transaction {
for (UCOTransfer ucoTransfer in data!.ledger!.uco!.transfers!) {
ucoTransfersBuffers = concatUint8List(<Uint8List>[
ucoTransfersBuffers,
hexToUint8List(ucoTransfer.to!),
Uint8List.fromList(hexToUint8List(ucoTransfer.to!)),
encodeBigInt(ucoTransfer.amount!)
]);
}
Expand All @@ -403,8 +410,8 @@ class Transaction {
for (TokenTransfer tokenTransfer in data!.ledger!.token!.transfers!) {
tokenTransfersBuffers = concatUint8List(<Uint8List>[
tokenTransfersBuffers,
hexToUint8List(tokenTransfer.token!),
hexToUint8List(tokenTransfer.to!),
Uint8List.fromList(hexToUint8List(tokenTransfer.token!)),
Uint8List.fromList(hexToUint8List(tokenTransfer.to!)),
encodeBigInt(tokenTransfer.amount!),
Uint8List.fromList(<int>[tokenTransfer.tokenId!])
]);
Expand All @@ -413,18 +420,20 @@ class Transaction {

Uint8List recipients = Uint8List(0);
for (String recipient in data!.recipients!) {
recipients =
concatUint8List(<Uint8List>[recipients, hexToUint8List(recipient)]);
recipients = concatUint8List(<Uint8List>[
recipients,
Uint8List.fromList(hexToUint8List(recipient))
]);
}

return concatUint8List(<Uint8List>[
encodeInt32(version!),
hexToUint8List(address!),
Uint8List.fromList(hexToUint8List(address!)),
Uint8List.fromList(<int>[txTypes[type]!]),
bufCodeSize,
Uint8List.fromList(data!.code!.codeUnits),
Uint8List.fromList(utf8.encode(data!.code!)),
bufContentSize,
data!.content!,
Uint8List.fromList(utf8.encode(data!.content!)),
Uint8List.fromList(<int>[data!.ownerships!.length]),
ownershipsBuffers,
Uint8List.fromList(<int>[data!.ledger!.uco!.transfers!.length]),
Expand All @@ -443,7 +452,7 @@ class Transaction {
'address': address == null ? '' : address!,
'type': type,
'data': {
'content': uint8ListToHex(data!.content!),
'content': HEX.encode(Uint16List.fromList(utf8.encode(data!.content!))),
'code': data == null || data!.code == null ? '' : data!.code!,
'ownerships': List<dynamic>.from(data!.ownerships!.map((Ownership x) {
return <String, Object?>{
Expand Down Expand Up @@ -485,7 +494,7 @@ class Transaction {

static Data initData() {
return Data.fromJson(<String, dynamic>{
'content': Uint8List(0),
'content': '',
'code': '',
'ownerships': [],
'ledger': {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/services/api_coins_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later
// Package imports:
import 'package:archethic_lib_dart/src/utils/logs.dart';
import 'package:http/http.dart' as http show Response, get;

// Project imports:
Expand Down Expand Up @@ -45,6 +44,7 @@ import 'package:archethic_lib_dart/src/model/coins/simple_price_response_try.dar
import 'package:archethic_lib_dart/src/model/coins/simple_price_response_twd.dart';
import 'package:archethic_lib_dart/src/model/coins/simple_price_response_usd.dart';
import 'package:archethic_lib_dart/src/model/coins/simple_price_response_zar.dart';
import 'package:archethic_lib_dart/src/utils/logs.dart';

class ApiCoinsService {
/// Get Archethic Coin infos (Prices, Marketcaps, Total Volumes)
Expand Down
9 changes: 5 additions & 4 deletions lib/src/services/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:math';
import 'dart:typed_data';

// Package imports:
import 'package:archethic_lib_dart/src/utils/logs.dart';
import 'package:http/http.dart' as http show Response, post;

// Project imports:
Expand All @@ -31,6 +30,7 @@ import 'package:archethic_lib_dart/src/model/transaction_fee.dart';
import 'package:archethic_lib_dart/src/model/transaction_input.dart';
import 'package:archethic_lib_dart/src/model/transaction_status.dart';
import 'package:archethic_lib_dart/src/utils/crypto.dart';
import 'package:archethic_lib_dart/src/utils/logs.dart';
import 'package:archethic_lib_dart/src/utils/utils.dart';

class ApiService {
Expand Down Expand Up @@ -244,8 +244,8 @@ class ApiService {
if (transactionContentResponse.data != null &&
transactionContentResponse.data!.transaction != null &&
transactionContentResponse.data!.transaction!.data != null) {
content = String.fromCharCodes(
transactionContentResponse.data!.transaction!.data!.content!);
content =
transactionContentResponse.data!.transaction!.data!.content!;
}
}
} catch (e) {
Expand Down Expand Up @@ -533,7 +533,8 @@ class ApiService {
Transaction newKeychainTransaction(String seed,
List<String> authorizedPublicKeys, Uint8List originPrivateKey,
{String? serviceName, String? derivationPath}) {
final Keychain keychain = Keychain(hexToUint8List(seed));
final Keychain keychain =
Keychain(Uint8List.fromList(hexToUint8List(seed)));
if (serviceName!.isNotEmpty && derivationPath!.isNotEmpty) {
keychain.addService(serviceName, derivationPath);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/services/hosting_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class HostingService {
}*/
/*
for (int i = 0; i < archive.files.length; i++) {
crypto.Hmac hmac = crypto.Hmac(crypto.sha256, hexToUint8List(seed));
crypto.Hmac hmac = crypto.Hmac(crypto.sha256, Uint8List.fromList(hexToUint8List(seed));
crypto.Digest digest = hmac.convert(archive.files[i].name.codeUnits);
Uint8List _seed = Uint8List.fromList(digest.bytes);
String _address = deriveAddress(uint8ListToHex(_seed), 0,
Expand Down Expand Up @@ -115,7 +115,7 @@ class HostingService {
for (int i = 0; i < arrayFiles.length; i++) {
if (arrayFiles[i] == 'index.html') {
crypto.Hmac hmac = crypto.Hmac(crypto.sha256, hexToUint8List(seed));
crypto.Hmac hmac = crypto.Hmac(crypto.sha256, Uint8List.fromList(hexToUint8List(seed));
crypto.Digest digest = hmac.convert('folder' + 'index.html');
Uint8List _seed = Uint8List.fromList(digest.bytes);
String _address = AddressService(endpoint).deriveAddress(
Expand Down
3 changes: 1 addition & 2 deletions lib/src/services/oracle_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class OracleService {
if (txList.isNotEmpty) {
final Transaction transaction = await ApiService(endpoint)
.getLastTransaction(txList[0].address!, request: 'data { content }');
return oracleUcoPriceFromJson(
String.fromCharCodes(transaction.data!.content!));
return oracleUcoPriceFromJson(transaction.data!.content!);
} else {
return OracleUcoPrice(uco: Uco(eur: 0, usd: 0));
}
Expand Down
Loading

0 comments on commit e20691f

Please sign in to comment.