-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/content_type
- Loading branch information
Showing
15 changed files
with
278 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,3 +198,4 @@ lovelace | |
lovelaces | ||
pinenacl | ||
dtscalac | ||
vkeys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
catalyst_voices_packages/catalyst_cardano_serialization/lib/src/utils/cbor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/// Holds cbor tags not specified by the official cbor package. | ||
final class CborCustomTags { | ||
const CborCustomTags._(); | ||
|
||
/// A cbor tag describing a key-value pairs data. | ||
static const int map = 259; | ||
} | ||
|
||
/// How many bytes are used in cbor encoding for a major type/length. | ||
enum CborSize { | ||
/// Length/data is encoded inside of the type information. | ||
inline(bytes: 0), | ||
|
||
/// Length/data is in 1 byte following the type information. | ||
one(bytes: 1), | ||
|
||
/// Length/data is in 2 bytes following the type information. | ||
two(bytes: 2), | ||
|
||
/// Length/data is in 4 bytes following the type information. | ||
four(bytes: 4), | ||
|
||
/// Length/data is in 8 bytes following the type information. | ||
eight(bytes: 8); | ||
|
||
/// The amount of bytes it takes to encode the type in cbor. | ||
final int bytes; | ||
|
||
const CborSize({required this.bytes}); | ||
|
||
/// The max int value that can be inlined in cbor without extra bytes. | ||
static const int maxInlineEncoding = 23; | ||
|
||
/// Calculates the [CborSize] for arbitrary [value]. | ||
static CborSize ofInt(int value) { | ||
if (value <= maxInlineEncoding) { | ||
return CborSize.inline; | ||
} else if (value < 0x100) { | ||
return CborSize.one; | ||
} else if (value < 0x10000) { | ||
return CborSize.two; | ||
} else if (value < 0x100000000) { | ||
return CborSize.four; | ||
} else { | ||
return CborSize.eight; | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
catalyst_voices_packages/catalyst_cardano_serialization/lib/src/witness.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'package:cbor/cbor.dart'; | ||
|
||
/// A set of witnesses that sign the transaction. | ||
class TransactionWitnessSet { | ||
/// The witnesses that sign the transaction. | ||
final Set<VkeyWitness> vkeyWitnesses; | ||
|
||
/// The default constructor for [TransactionWitnessSet]. | ||
const TransactionWitnessSet({required this.vkeyWitnesses}); | ||
|
||
/// Serializes the type as cbor. | ||
CborValue toCbor() { | ||
return CborMap({ | ||
for (final vkey in vkeyWitnesses.indexed) | ||
CborSmallInt(vkey.$1): vkey.$2.toCbor(), | ||
}); | ||
} | ||
} | ||
|
||
/// The transaction witness with a [signature] of the transaction. | ||
class VkeyWitness { | ||
/// The public key of the witness. | ||
final Vkey vkey; | ||
|
||
/// The witness signature of the transaction. | ||
final Ed25519Signature signature; | ||
|
||
/// The default constructor for [VkeyWitness]. | ||
const VkeyWitness({ | ||
required this.vkey, | ||
required this.signature, | ||
}); | ||
|
||
/// Builds a fake [VkeyWitness] that helps to measure target transaction | ||
/// size when the transaction hasn't been signed yet. | ||
factory VkeyWitness.seeded(int byte) { | ||
return VkeyWitness( | ||
vkey: Vkey.seeded(byte), | ||
signature: Ed25519Signature.seeded(byte), | ||
); | ||
} | ||
|
||
/// Serializes the type as cbor. | ||
CborValue toCbor() { | ||
return CborList([ | ||
vkey.toCbor(), | ||
signature.toCbor(), | ||
]); | ||
} | ||
} | ||
|
||
/// The public key of the witness. | ||
extension type Vkey._(List<int> bytes) { | ||
/// The length of the [Vkey]. | ||
static const int length = 32; | ||
|
||
/// The default constructor for [Vkey]. | ||
Vkey.fromBytes(this.bytes) { | ||
if (bytes.length != length) { | ||
throw ArgumentError('Vkey length does not match: ${bytes.length}'); | ||
} | ||
} | ||
|
||
/// Returns the [Vkey] filled with [byte] that can be | ||
/// used to reserve size to calculate the final transaction bytes size. | ||
factory Vkey.seeded(int byte) => Vkey.fromBytes(List.filled(length, byte)); | ||
|
||
/// Serializes the type as cbor. | ||
CborValue toCbor() => CborBytes(bytes); | ||
} | ||
|
||
/// The witness signature of the transaction. | ||
extension type Ed25519Signature._(List<int> bytes) { | ||
/// The length of the [Ed25519Signature]. | ||
static const int length = 64; | ||
|
||
/// The default constructor for [Ed25519Signature]. | ||
Ed25519Signature.fromBytes(this.bytes) { | ||
if (bytes.length != length) { | ||
throw ArgumentError( | ||
'Ed25519Signature length does not match: ${bytes.length}', | ||
); | ||
} | ||
} | ||
|
||
/// Returns the [Ed25519Signature] filled with [byte] that can be | ||
/// used to reserve size to calculate the final transaction bytes size. | ||
factory Ed25519Signature.seeded(int byte) => | ||
Ed25519Signature.fromBytes(List.filled(length, byte)); | ||
|
||
/// Serializes the type as cbor. | ||
CborValue toCbor() => CborBytes(bytes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
catalyst_voices_packages/catalyst_cardano_serialization/test/utils/cbor_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:catalyst_cardano_serialization/src/utils/cbor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group(CborSize, () { | ||
test('of int value', () { | ||
expect(CborSize.ofInt(0), equals(CborSize.inline)); | ||
expect( | ||
CborSize.ofInt(CborSize.maxInlineEncoding), | ||
equals(CborSize.inline), | ||
); | ||
expect(CborSize.ofInt(0xff), equals(CborSize.one)); | ||
expect(CborSize.ofInt(0x100), equals(CborSize.two)); | ||
expect(CborSize.ofInt(0xffff), equals(CborSize.two)); | ||
expect(CborSize.ofInt(0x10000), equals(CborSize.four)); | ||
expect(CborSize.ofInt(0xffffffff), equals(CborSize.four)); | ||
expect(CborSize.ofInt(0x100000000), equals(CborSize.eight)); | ||
expect(CborSize.ofInt(0xfffffffffffff), equals(CborSize.eight)); | ||
}); | ||
|
||
test('bytes length', () { | ||
expect(CborSize.inline.bytes, equals(0)); | ||
expect(CborSize.one.bytes, equals(1)); | ||
expect(CborSize.two.bytes, equals(2)); | ||
expect(CborSize.four.bytes, equals(4)); | ||
expect(CborSize.eight.bytes, equals(8)); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.