mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-25 08:38:45 +00:00
auto format code
This commit is contained in:
parent
ee88cd8fb0
commit
24c9561257
1 changed files with 13 additions and 35 deletions
|
@ -1,6 +1,4 @@
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'package:hex/hex.dart';
|
|
||||||
import 'package:convert/convert.dart';
|
|
||||||
|
|
||||||
// The Structure enum
|
// The Structure enum
|
||||||
enum Structure {
|
enum Structure {
|
||||||
|
@ -25,8 +23,6 @@ class CompactSizeResult {
|
||||||
CompactSizeResult({required this.amount, required this.bytesRead});
|
CompactSizeResult({required this.amount, required this.bytesRead});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This class is a data structure representing the entire output, comprised of both the
|
// This class is a data structure representing the entire output, comprised of both the
|
||||||
// normal Script pub key and the token data. We get this after we parse/unwrap the raw
|
// normal Script pub key and the token data. We get this after we parse/unwrap the raw
|
||||||
// output.
|
// output.
|
||||||
|
@ -36,7 +32,6 @@ class ParsedOutput {
|
||||||
ParsedOutput({this.script_pub_key, this.token_data});
|
ParsedOutput({this.script_pub_key, this.token_data});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This is equivalent to the Electron Cash python's "OutputData" in token.py.
|
// This is equivalent to the Electron Cash python's "OutputData" in token.py.
|
||||||
// Named here specifically as "TokenOutputData" to reflect the fact that
|
// Named here specifically as "TokenOutputData" to reflect the fact that
|
||||||
// it is specifically for tokens, whereas the other class ParsedOutput represents
|
// it is specifically for tokens, whereas the other class ParsedOutput represents
|
||||||
|
@ -55,7 +50,6 @@ class TokenOutputData {
|
||||||
this.bitfield,
|
this.bitfield,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Get the "capability", see Capability enum.
|
// Get the "capability", see Capability enum.
|
||||||
int getCapability() {
|
int getCapability() {
|
||||||
if (bitfield != null) {
|
if (bitfield != null) {
|
||||||
|
@ -86,7 +80,6 @@ class TokenOutputData {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Functions to return specific attributes based on the Capability.
|
// Functions to return specific attributes based on the Capability.
|
||||||
bool isMintingNFT() {
|
bool isMintingNFT() {
|
||||||
return hasNFT() && getCapability() == Capability.Minting.index;
|
return hasNFT() && getCapability() == Capability.Minting.index;
|
||||||
|
@ -100,7 +93,6 @@ class TokenOutputData {
|
||||||
return hasNFT() && getCapability() == Capability.NoCapability.index;
|
return hasNFT() && getCapability() == Capability.NoCapability.index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This function validates if the bitfield makes sense or violates known rules/logic.
|
// This function validates if the bitfield makes sense or violates known rules/logic.
|
||||||
bool isValidBitfield() {
|
bool isValidBitfield() {
|
||||||
if (bitfield == null) {
|
if (bitfield == null) {
|
||||||
|
@ -126,21 +118,17 @@ class TokenOutputData {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The serialze and deserialize functions are the nuts and bolts of how we unpack
|
// The serialze and deserialize functions are the nuts and bolts of how we unpack
|
||||||
// and pack outputs. These are called by the wrap and unwrap functions.
|
// and pack outputs. These are called by the wrap and unwrap functions.
|
||||||
int deserialize(Uint8List buffer, {int cursor = 0, bool strict = false}) {
|
int deserialize(Uint8List buffer, {int cursor = 0, bool strict = false}) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
this.id = buffer.sublist(cursor, cursor + 32);
|
this.id = buffer.sublist(cursor, cursor + 32);
|
||||||
cursor += 32;
|
cursor += 32;
|
||||||
|
|
||||||
|
|
||||||
this.bitfield = Uint8List.fromList([buffer[cursor]]);
|
this.bitfield = Uint8List.fromList([buffer[cursor]]);
|
||||||
cursor += 1;
|
cursor += 1;
|
||||||
|
|
||||||
if (this.hasCommitmentLength()) {
|
if (this.hasCommitmentLength()) {
|
||||||
|
|
||||||
// Read the first byte to determine the length of the commitment data
|
// Read the first byte to determine the length of the commitment data
|
||||||
int commitmentLength = buffer[cursor];
|
int commitmentLength = buffer[cursor];
|
||||||
|
|
||||||
|
@ -156,34 +144,30 @@ class TokenOutputData {
|
||||||
this.commitment = null;
|
this.commitment = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (this.hasAmount()) {
|
if (this.hasAmount()) {
|
||||||
// Use readCompactSize that returns CompactSizeResult
|
// Use readCompactSize that returns CompactSizeResult
|
||||||
CompactSizeResult result = readCompactSize(buffer, cursor, strict: strict);
|
CompactSizeResult result =
|
||||||
|
readCompactSize(buffer, cursor, strict: strict);
|
||||||
this.amount = result.amount;
|
this.amount = result.amount;
|
||||||
cursor += result.bytesRead;
|
cursor += result.bytesRead;
|
||||||
} else {
|
} else {
|
||||||
this.amount = 0;
|
this.amount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!this.isValidBitfield() ||
|
if (!this.isValidBitfield() ||
|
||||||
(this.hasAmount() && this.amount == 0) ||
|
(this.hasAmount() && this.amount == 0) ||
|
||||||
(this.amount! < 0 || this.amount! > (1 << 63) - 1) ||
|
(this.amount! < 0 || this.amount! > (1 << 63) - 1) ||
|
||||||
(this.hasCommitmentLength() && this.commitment!.isEmpty) ||
|
(this.hasCommitmentLength() && this.commitment!.isEmpty) ||
|
||||||
(this.amount! == 0 && !this.hasNFT())
|
(this.amount! == 0 && !this.hasNFT())) {
|
||||||
) {
|
|
||||||
throw Exception('Unable to parse token data or token data is invalid');
|
throw Exception('Unable to parse token data or token data is invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
return cursor; // Return the number of bytes read
|
return cursor; // Return the number of bytes read
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception('Deserialization failed: $e');
|
throw Exception('Deserialization failed: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Serialize method
|
// Serialize method
|
||||||
Uint8List serialize() {
|
Uint8List serialize() {
|
||||||
var buffer = BytesBuilder();
|
var buffer = BytesBuilder();
|
||||||
|
@ -204,9 +188,7 @@ class TokenOutputData {
|
||||||
|
|
||||||
return buffer.toBytes();
|
return buffer.toBytes();
|
||||||
}
|
}
|
||||||
|
} //END OF OUTPUTDATA CLASS
|
||||||
} //END OF OUTPUTDATA CLASS
|
|
||||||
|
|
||||||
|
|
||||||
// The prefix byte is specified by the CashTokens spec.
|
// The prefix byte is specified by the CashTokens spec.
|
||||||
final List<int> PREFIX_BYTE = [0xef];
|
final List<int> PREFIX_BYTE = [0xef];
|
||||||
|
@ -232,9 +214,7 @@ ParsedOutput wrap_spk(TokenOutputData? token_data, Uint8List script_pub_key) {
|
||||||
return parsedOutput;
|
return parsedOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function unwraps any output, either "normal" (containing no token data)
|
||||||
|
|
||||||
// This function unwraps any output, either "normal" (containing no token data)
|
|
||||||
// or an output with token data. If no token data, just the output is returned,
|
// or an output with token data. If no token data, just the output is returned,
|
||||||
// and if token data exists, both the output and token data are returned.
|
// and if token data exists, both the output and token data are returned.
|
||||||
// Note that the data returend in both cases in of ParsedOutput type, which
|
// Note that the data returend in both cases in of ParsedOutput type, which
|
||||||
|
@ -242,13 +222,11 @@ ParsedOutput wrap_spk(TokenOutputData? token_data, Uint8List script_pub_key) {
|
||||||
ParsedOutput unwrap_spk(Uint8List wrapped_spk) {
|
ParsedOutput unwrap_spk(Uint8List wrapped_spk) {
|
||||||
ParsedOutput parsedOutput = ParsedOutput();
|
ParsedOutput parsedOutput = ParsedOutput();
|
||||||
|
|
||||||
|
|
||||||
if (wrapped_spk.isEmpty || wrapped_spk[0] != PREFIX_BYTE[0]) {
|
if (wrapped_spk.isEmpty || wrapped_spk[0] != PREFIX_BYTE[0]) {
|
||||||
parsedOutput.script_pub_key = wrapped_spk;
|
parsedOutput.script_pub_key = wrapped_spk;
|
||||||
return parsedOutput;
|
return parsedOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int read_cursor = 1; // Start after the PREFIX_BYTE
|
int read_cursor = 1; // Start after the PREFIX_BYTE
|
||||||
TokenOutputData token_data = TokenOutputData();
|
TokenOutputData token_data = TokenOutputData();
|
||||||
|
|
||||||
|
@ -256,14 +234,12 @@ ParsedOutput unwrap_spk(Uint8List wrapped_spk) {
|
||||||
try {
|
try {
|
||||||
// Deserialize updates read_cursor by the number of bytes read
|
// Deserialize updates read_cursor by the number of bytes read
|
||||||
|
|
||||||
wrapped_spk_without_prefix_byte= wrapped_spk.sublist(read_cursor);
|
wrapped_spk_without_prefix_byte = wrapped_spk.sublist(read_cursor);
|
||||||
int bytesRead = token_data.deserialize(wrapped_spk_without_prefix_byte);
|
int bytesRead = token_data.deserialize(wrapped_spk_without_prefix_byte);
|
||||||
|
|
||||||
|
|
||||||
read_cursor += bytesRead;
|
read_cursor += bytesRead;
|
||||||
parsedOutput.token_data = token_data;
|
parsedOutput.token_data = token_data;
|
||||||
parsedOutput.script_pub_key = wrapped_spk.sublist(read_cursor);
|
parsedOutput.script_pub_key = wrapped_spk.sublist(read_cursor);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If unable to deserialize, return all bytes as the full scriptPubKey
|
// If unable to deserialize, return all bytes as the full scriptPubKey
|
||||||
parsedOutput.script_pub_key = wrapped_spk;
|
parsedOutput.script_pub_key = wrapped_spk;
|
||||||
|
@ -277,9 +253,11 @@ ParsedOutput unwrap_spk(Uint8List wrapped_spk) {
|
||||||
//These are part of a "length value " scheme where the length (and endianness) are given first
|
//These are part of a "length value " scheme where the length (and endianness) are given first
|
||||||
// and inform the program of how many bytes to grab next. These are in turn used by the serialize
|
// and inform the program of how many bytes to grab next. These are in turn used by the serialize
|
||||||
// and deserialize functions.-
|
// and deserialize functions.-
|
||||||
CompactSizeResult readCompactSize(Uint8List buffer, int cursor, {bool strict = false}) {
|
CompactSizeResult readCompactSize(
|
||||||
|
Uint8List buffer,
|
||||||
|
int cursor, {
|
||||||
|
bool strict = false,
|
||||||
|
}) {
|
||||||
int bytesRead = 0; // Variable to count bytes read
|
int bytesRead = 0; // Variable to count bytes read
|
||||||
int val;
|
int val;
|
||||||
try {
|
try {
|
||||||
|
@ -314,6 +292,7 @@ CompactSizeResult readCompactSize(Uint8List buffer, int cursor, {bool strict = f
|
||||||
throw Exception("attempt to read past end of buffer");
|
throw Exception("attempt to read past end of buffer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Uint8List writeCompactSize(int size) {
|
Uint8List writeCompactSize(int size) {
|
||||||
var buffer = ByteData(9); // Maximum needed size for compact size is 9 bytes
|
var buffer = ByteData(9); // Maximum needed size for compact size is 9 bytes
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
|
@ -336,4 +315,3 @@ Uint8List writeCompactSize(int size) {
|
||||||
throw Exception("Size too large to represent as CompactSize");
|
throw Exception("Size too large to represent as CompactSize");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue