Skip to content

Commit

Permalink
fix: stake / unstake schema
Browse files Browse the repository at this point in the history
  • Loading branch information
parodyBit committed May 27, 2024
1 parent 30ed844 commit b276a61
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 18 deletions.
15 changes: 13 additions & 2 deletions example/stake_transaction_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ void main() async {
/// The authorization by the node
KeyedSignature authorization = signHash(pkh, masterNode.privateKey);

/// Build the Stake Key
StakeKey stakeKey = StakeKey(
validator: authorization.publicKey.pkh,
withdrawer: PublicKeyHash.fromAddress(withdrawer.address.address),
);

/// build stake transaction body
StakeBody body = StakeBody(
inputs: [
Input(outputPointer: outputPointer),
],
output: StakeOutput(
value: MINIMUM_STAKEABLE_AMOUNT_WITS,
key: stakeKey,
authorization: authorization,
),
);
Expand All @@ -52,8 +59,12 @@ void main() async {
/// var response = await nodeClient.inventory(stake.jsonMap());
///
UnstakeBody unstakeBody = UnstakeBody(
operator: PublicKeyHash.fromAddress(""),
withdrawal: ValueTransferOutput.fromJson({}));
operator: PublicKeyHash.fromAddress(withdrawer.address.address),
withdrawal: ValueTransferOutput.fromJson({
"pkh": withdrawer.address.address,
"time_lock": 0,
"value": 1,
}));

KeyedSignature unstakeSignature =
signHash(bytesToHex(unstakeBody.hash), masterNode.privateKey);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/schema/public_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class PublicKey extends GeneratedMessage {

Uint8List get pbBytes => writeToBuffer();

PublicKeyHash get pkh => PublicKeyHash(
hash: sha256(data: Uint8List.fromList(publicKey)).sublist(0, 20),
);

@TagNumber(1)
List<int> get publicKey => $_getN(0);
@TagNumber(1)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/schema/stake_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class StakeBody extends GeneratedMessage {
package: const PackageName('witnet'), createEmptyInstance: create)
..pc<Input>(1, 'inputs', PbFieldType.PM, subBuilder: Input.create)
..aOM<StakeOutput>(2, 'output', subBuilder: StakeOutput.create)
..aOM<ValueTransferOutput>(2, 'change',
..aOM<ValueTransferOutput>(3, 'change',
subBuilder: ValueTransferOutput.create)
..hasRequiredFields = false;

Expand Down
3 changes: 2 additions & 1 deletion lib/src/schema/stake_key.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
part of 'schema.dart';



class StakeKey extends GeneratedMessage {
static final BuilderInfo _i = BuilderInfo('StakeOutput',
package: const PackageName('witnet'), createEmptyInstance: create)
Expand Down Expand Up @@ -58,7 +59,7 @@ class StakeKey extends GeneratedMessage {
PublicKeyHash get validator => $_getN(1);
@TagNumber(1)
set validator(PublicKeyHash v) {
setField(0, v);
setField(1, v);
}

@TagNumber(1)
Expand Down
19 changes: 8 additions & 11 deletions lib/src/schema/stake_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class StakeOutput extends GeneratedMessage {
static final BuilderInfo _i = BuilderInfo('StakeOutput',
package: const PackageName('witnet'), createEmptyInstance: create)
..a<Int64>(1, 'value', PbFieldType.OU6, defaultOrMaker: Int64.ZERO)
..aOM<PublicKeyHash>(2, 'key', subBuilder: PublicKeyHash.create)
..aOM<StakeKey>(2, 'key', subBuilder: StakeKey.create)
..aOM<KeyedSignature>(3, 'authorization', subBuilder: KeyedSignature.create)
..hasRequiredFields = false;

Expand All @@ -23,7 +23,7 @@ class StakeOutput extends GeneratedMessage {

factory StakeOutput({
int? value,
PublicKeyHash? key,
StakeKey? key,
KeyedSignature? authorization,
}) {
final _result = create();
Expand All @@ -47,14 +47,13 @@ class StakeOutput extends GeneratedMessage {
@override
factory StakeOutput.fromJson(Map<String, dynamic> json) => StakeOutput(
value: json["value"],

key: PublicKeyHash.fromAddress(json["key"]),
key: StakeKey.fromJson(json["key"]),
authorization: KeyedSignature.fromJson(json["authorization"]),
);

Map<String, dynamic> jsonMap({bool asHex = false}) => {
"value": value.toInt(),
"key": key.address,
"key": key.jsonMap(asHex: asHex),
"authorization": authorization.jsonMap(asHex: asHex),
};

Expand All @@ -76,10 +75,9 @@ class StakeOutput extends GeneratedMessage {
void clearValue() => clearField(1);

@TagNumber(2)
PublicKeyHash get key => $_getN(2);
StakeKey get key => $_getN(2);
@TagNumber(2)
set key(PublicKeyHash v) {

set key(StakeKey v) {
setField(2, v);
}

Expand All @@ -88,7 +86,7 @@ class StakeOutput extends GeneratedMessage {
@TagNumber(2)
void clearKey() => clearField(2);
@TagNumber(2)
PublicKeyHash ensureKey() => $_ensure(1);
StakeKey ensureKey() => $_ensure(1);

@TagNumber(3)
KeyedSignature get authorization => $_getN(3);
Expand All @@ -102,6 +100,5 @@ class StakeOutput extends GeneratedMessage {
@TagNumber(3)
void clearAuthorization() => clearField(3);
@TagNumber(3)
PublicKeyHash ensureAuthorization() => $_ensure(2);

KeyedSignature ensureAuthorization() => $_ensure(2);
}
49 changes: 48 additions & 1 deletion lib/src/schema/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ enum TransactionKind {
reveal,
tally,
mint,
stake,
unstake,
notSet
}

Expand All @@ -17,6 +19,8 @@ const Map<int, TransactionKind> _Transaction_KindByTag = {
4: TransactionKind.reveal,
5: TransactionKind.tally,
6: TransactionKind.mint,
7: TransactionKind.stake,
8: TransactionKind.unstake,
0: TransactionKind.notSet
};

Expand All @@ -36,6 +40,10 @@ class Transaction extends GeneratedMessage {
protoName: 'Tally', subBuilder: TallyTransaction.create)
..aOM<MintTransaction>(6, 'Mint',
protoName: 'Mint', subBuilder: MintTransaction.create)
..aOM<StakeTransaction>(6, 'Mint',
protoName: 'Stake', subBuilder: StakeTransaction.create)
..aOM<UnstakeTransaction>(6, 'Mint',
protoName: 'Unstake', subBuilder: UnstakeTransaction.create)
..hasRequiredFields = false;

static Transaction create() => Transaction._();
Expand All @@ -59,6 +67,8 @@ class Transaction extends GeneratedMessage {
RevealTransaction? reveal,
TallyTransaction? tally,
MintTransaction? mint,
StakeTransaction? stake,
UnstakeTransaction? unstake,
}) {
final _result = create();
if (valueTransfer != null) {
Expand All @@ -79,6 +89,12 @@ class Transaction extends GeneratedMessage {
if (mint != null) {
_result.mint = mint;
}
if (stake != null) {
_result.stake = stake;
}
if (unstake != null) {
_result.unstake = unstake;
}
return _result;
}

Expand Down Expand Up @@ -110,9 +126,12 @@ class Transaction extends GeneratedMessage {
case 'Reveal':
return Transaction(
reveal: RevealTransaction.fromJson(_txn['Reveal']));

case 'Tally':
return Transaction(tally: TallyTransaction.fromJson(_txn['Tally']));
case 'Stake':
return Transaction(stake: StakeTransaction.fromJson(_txn['Stake']));
case 'Unstake':
return Transaction(unstake: UnstakeTransaction.fromJson(_txn['Unstake']));
}
} else {
throw ArgumentError('Invalid json');
Expand Down Expand Up @@ -250,4 +269,32 @@ class Transaction extends GeneratedMessage {
void clearMint() => clearField(6);
@TagNumber(6)
MintTransaction ensureMint() => $_ensure(5);

@TagNumber(7)
StakeTransaction get stake => $_getN(6);
@TagNumber(7)
set stake(StakeTransaction v) {
setField(7, v);
}

@TagNumber(7)
bool hasStake() => $_has(6);
@TagNumber(7)
void clearStake() => clearField(7);
@TagNumber(7)
StakeTransaction ensureStake() => $_ensure(6);


@TagNumber(8)
UnstakeTransaction get unstake => $_getN(7);
@TagNumber(8)
set unstake(UnstakeTransaction v) {
setField(8, v);
}
@TagNumber(8)
bool hasUnstake() => $_has(7);
@TagNumber(8)
void clearUnstake() => clearField(8);
@TagNumber(8)
UnstakeTransaction ensureUnstake() => $_ensure(7);
}
3 changes: 1 addition & 2 deletions lib/src/schema/unstake_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class UnstakeTransaction extends GeneratedMessage {
package: const PackageName('witnet'), createEmptyInstance: create)
..aOM<UnstakeBody>(1, 'body', subBuilder: UnstakeBody.create)

..pc<KeyedSignature>(2, 'signature', PbFieldType.PM,
subBuilder: KeyedSignature.create)
..aOM<KeyedSignature>(2, 'signature', subBuilder: KeyedSignature.create)
..hasRequiredFields = false;

static UnstakeTransaction create() => UnstakeTransaction._();
Expand Down

0 comments on commit b276a61

Please sign in to comment.