Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified asset_sources/default_themes/stack_duo/dark.zip
Binary file not shown.
Binary file modified asset_sources/default_themes/stack_duo/light.zip
Binary file not shown.
Binary file modified asset_sources/default_themes/stack_wallet/dark.zip
Binary file not shown.
Binary file modified asset_sources/default_themes/stack_wallet/light.zip
Binary file not shown.
81 changes: 38 additions & 43 deletions integration_test/grin/grin_test_chain_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/v2/transaction_v2.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/slatepack_models.dart';
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/utilities/logger.dart';
Expand Down Expand Up @@ -351,11 +352,11 @@ void main() {
expect(await alice.pendingSlatepackFor(slateId), s1);

final inspected = await bob.inspectSlatepack(s1);
expect(inspected.state, GrinSlateState.s1);
expect(inspected.amount, grin(50).raw);
expect(inspected.sender, aliceAddress);
expect(inspected.state, SlateState.s1);
expect(inspected.amount, grin(50));
expect(inspected.senderAddress, aliceAddress);
final s2 = await bob.receiveSlatepack(s1);
expect(s2.state, GrinSlateState.s2);
expect(s2.state, SlateState.s2);
expect(s2.slateId, slateId);

await sync(bob);
Expand All @@ -368,7 +369,7 @@ void main() {
expect(bob.info.cachedBalance.pendingSpendable, grin(50));
expect(bob.info.cachedBalance.spendable, grin(0));

final s3 = await alice.finalizeSlatepack(s2.armoredSlatepack);
final s3 = await alice.finalizeSlatepack(s2.slatepack!);
expect(s3.posted, isTrue);
expect(s3.kernelExcess, hasLength(66));
// Posting mined height 16; ten more give it 11 confirmations.
Expand All @@ -385,7 +386,7 @@ void main() {
expect(data[TxV2OdKeys.grinHasStoredTx], isTrue);
expect(a.hash, s3.kernelExcess);
expect(sent(a), grin(50));
expect(fee(a), nano(s3.fee));
expect(fee(a), confirmed.fee);

b = await row(bob, slateId);
expect(b.height, 16);
Expand All @@ -406,27 +407,21 @@ void main() {
amount: grin(30),
payerAddress: aliceAddress,
);
expect(i1.state, GrinSlateState.i1);
expect(i1.state, SlateState.i1);
await sync(bob);
var b = await row(bob, i1.slateId);
var b = await row(bob, i1.slateId!);
expect(b.type, TransactionType.incoming);
expect(label(bob, b), 'Invoice issued (waiting for payer)');
expect(otherData(b)[TxV2OdKeys.grinSlateState], 'I1');

// An amount other than the invoice's is refused before the library is
// asked to pay, and nothing is recorded or locked.
await expectLater(
alice.processInvoice(
i1.armoredSlatepack,
confirmedAmount: nano(grin(30).raw - BigInt.one),
),
throwsA(
predicate(
(e) => e.toString().contains('does not match'),
'amount mismatch',
),
),
final refused = await alice.processInvoice(
i1.slatepack!,
confirmedAmount: nano(grin(30).raw - BigInt.one),
);
expect(refused.success, isFalse);
expect(refused.error, contains('does not match'));
expect(
chain.backend.calls.where((c) => c.startsWith('processInvoice')),
isEmpty,
Expand All @@ -435,47 +430,47 @@ void main() {
expect(
await MainDB.instance.isar.transactionV2s
.where()
.txidWalletIdEqualTo(i1.slateId, alice.walletId)
.txidWalletIdEqualTo(i1.slateId!, alice.walletId)
.findFirst(),
isNull,
);
expect(alice.info.cachedBalance.spendable, before);

final i2 = await alice.processInvoice(
i1.armoredSlatepack,
i1.slatepack!,
confirmedAmount: grin(30),
);
expect(i2.state, GrinSlateState.i2);
expect(i2.state, SlateState.i2);
expect(
chain.backend.calls.where((c) => c.startsWith('processInvoice')),
hasLength(1),
);
await sync(alice);
var a = await row(alice, i1.slateId);
var a = await row(alice, i1.slateId!);
expect(a.type, TransactionType.outgoing);
expect(label(alice, a), 'Paying invoice (waiting for invoicer)');
expect(otherData(a)[TxV2OdKeys.grinSlateState], 'I2');
expect(alice.info.cachedBalance.spendable < before, isTrue);

final i3 = await bob.finalizeInvoice(i2.armoredSlatepack);
final i3 = await bob.finalizeInvoice(i2.slatepack!);
expect(i3.posted, isTrue);
// Posting mined height 16, to Bob.
expect(await chain.mine(alice, 10), 26);
await sync(alice);
await sync(bob);

b = await row(bob, i1.slateId);
b = await row(bob, i1.slateId!);
expect(b.height, 16);
expect(label(bob, b), 'Received');
expect(received(b), grin(30));
expect(otherData(b)[TxV2OdKeys.grinKernelExcess], i3.kernelExcess);
a = await row(alice, i1.slateId);
a = await row(alice, i1.slateId!);
expect(a.height, 16);
expect(label(alice, a), 'Sent');
expect(sent(a), grin(30));
expect(fee(a), nano(i2.fee));
expect(fee(a), i2.fee!);
// Bob also got the block 16 coinbase and the fee.
expect(bob.info.cachedBalance.total, grin(30 + reward) + nano(i2.fee));
expect(bob.info.cachedBalance.total, grin(30 + reward) + i2.fee!);
}, timeout: _timeout);

testWidgets('cancelling an unfinalized send unlocks its inputs', (
Expand All @@ -491,13 +486,13 @@ void main() {
await sync(alice);
expect(alice.info.cachedBalance.spendable < before.spendable, isTrue);
expect(
label(alice, await row(alice, slate.slateId)),
label(alice, await row(alice, slate.slateId!)),
'Sending (waiting for response)',
);

expect(await alice.cancelPendingTransactionAndPost(slate.slateId), '');
expect(await alice.cancelPendingTransactionAndPost(slate.slateId!), '');
await sync(alice);
final tx = await row(alice, slate.slateId);
final tx = await row(alice, slate.slateId!);
expect(tx.isCancelled, isTrue);
expect(label(alice, tx), 'Cancelled');
expect(tx.height, isNull);
Expand All @@ -506,7 +501,7 @@ void main() {

// A second cancel is reported, not thrown.
expect(
await alice.cancelPendingTransactionAndPost(slate.slateId),
await alice.cancelPendingTransactionAndPost(slate.slateId!),
isNotEmpty,
);
}, timeout: _timeout);
Expand All @@ -522,20 +517,20 @@ void main() {

// Nothing is stored to post yet.
await sync(alice);
await expectLater(alice.repost(slate.slateId), throwsA(anything));
await expectLater(alice.repost(slate.slateId!), throwsA(anything));
expect(chain.backend.calls.where((c) => c.startsWith('repost')), isEmpty);

final s2 = await bob.receiveSlatepack(slate.armoredSlatepack);
final s2 = await bob.receiveSlatepack(slate.slatepack!);
(await chain.api(alice)).skipNextBroadcast = true;
final s3 = await alice.finalizeSlatepack(s2.armoredSlatepack);
final s3 = await alice.finalizeSlatepack(s2.slatepack!);
expect(s3.posted, isFalse);
await sync(alice);
var tx = await row(alice, slate.slateId);
var tx = await row(alice, slate.slateId!);
expect(otherData(tx)[TxV2OdKeys.grinHasStoredTx], isTrue);
expect(tx.height, isNull);
expect(alice.info.cachedChainHeight, 15);

await alice.repost(slate.slateId);
await alice.repost(slate.slateId!);
expect(
chain.backend.calls.where((c) => c.startsWith('repost')),
hasLength(1),
Expand All @@ -544,7 +539,7 @@ void main() {
expect(await chain.mine(alice, 10), 26);
await sync(alice);
await sync(bob);
tx = await row(alice, slate.slateId);
tx = await row(alice, slate.slateId!);
expect(tx.height, 16);
expect(label(alice, tx), 'Sent');
expect(otherData(tx)[TxV2OdKeys.grinKernelExcess], s3.kernelExcess);
Expand All @@ -557,13 +552,13 @@ void main() {
amount: grin(25),
recipientAddress: await bob.slatepackAddress(),
);
final s2 = await bob.receiveSlatepack(slate.armoredSlatepack);
final s3 = await alice.finalizeSlatepack(s2.armoredSlatepack);
final s2 = await bob.receiveSlatepack(slate.slatepack!);
final s3 = await alice.finalizeSlatepack(s2.slatepack!);
await chain.mine(alice, 1);
await sync(alice);
await sync(bob);

final json = await alice.exportPaymentProof(slate.slateId);
final json = await alice.exportPaymentProof(slate.slateId!);
final proof = GrinPaymentProof.fromJsonString(json);
expect(proof.amount, grin(25).raw);
expect(proof.excess, s3.kernelExcess);
Expand All @@ -587,11 +582,11 @@ void main() {
);
// The receiving side keeps no proof.
expect(
otherData(await row(bob, slate.slateId))[TxV2OdKeys.grinHasPaymentProof],
otherData(await row(bob, slate.slateId!))[TxV2OdKeys.grinHasPaymentProof],
isFalse,
);
await expectLater(
bob.exportPaymentProof(slate.slateId),
bob.exportPaymentProof(slate.slateId!),
throwsA(predicate((e) => e.toString().contains('no payment proof'))),
);
}, timeout: _timeout);
Expand Down
145 changes: 145 additions & 0 deletions lib/models/slatepack_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,148 @@ class FinalizeResult {
")";
}
}

/// What the confirm screen has to ask, or refuse, before a direct send.
class SlatepackHttpPolicy {
const SlatepackHttpPolicy({
this.blockedReason,
this.needsClearnetAck = false,
this.needsPlaintextAck = false,
});

/// Set when the send cannot go ahead now. Shown to the user.
final String? blockedReason;

/// The target is reached without Tor and learns the user's IP address.
final bool needsClearnetAck;

/// The target is a plain http:// URL, readable on the way (also by a Tor
/// exit).
final bool needsPlaintextAck;

bool get isBlocked => blockedReason != null;
}

/// Slatepack details of a stored transaction, for the details screen.
class SlatepackTxInfo {
const SlatepackTxInfo({
this.state = SlateState.unknown,
this.slateId,
this.kernelExcess,
this.counterpartyAddress,
this.ttlCutoffHeight,
this.finalizedAwaitingConfirmation = false,
this.broadcastPosted,
this.canRepost = false,
this.canShowSlatepack = false,
this.hasPaymentProof = false,
});

/// The last state the wallet saw.
final SlateState state;
final String? slateId;
final String? kernelExcess;
final String? counterpartyAddress;
final int? ttlCutoffHeight;

/// Finalized and stored but not seen on chain yet.
final bool finalizedAwaitingConfirmation;

/// The result of the wallet's last broadcast attempt. Null means that this
/// process did not observe the result.
final bool? broadcastPosted;

final bool canRepost;

/// The wallet still has the slatepack the other party needs.
final bool canShowSlatepack;

final bool hasPaymentProof;

/// Whether cancelling can release a transaction that is still local.
/// A finalized transaction is cancellable only when this wallet observed
/// that the node rejected its broadcast. An unknown or successful broadcast
/// may still confirm and must not be presented as safely cancellable.
bool get canCancel => finalizedAwaitingConfirmation
? broadcastPosted == false
: state != SlateState.s3 && state != SlateState.i3;
}

/// The texts of the dialog that asks before a slatepack transaction is
/// cancelled.
class SlatepackCancelPrompt {
const SlatepackCancelPrompt({
required this.title,
required this.message,
required this.confirmLabel,
});

final String title;
final String message;
final String confirmLabel;

/// What cancelling does depends on what the other party already holds.
/// [info] is null for wallets that keep no slate state; [incoming] is the
/// direction of the transaction.
factory SlatepackCancelPrompt.of(
SlatepackTxInfo? info, {
required bool incoming,
}) {
final state = info?.state ?? SlateState.unknown;
final String message;
if (info?.finalizedAwaitingConfirmation == true &&
info?.broadcastPosted == false) {
message =
"The node did not accept this transaction. Cancelling unlocks its "
"funds in this wallet. If it was posted somewhere else in the "
"meantime, it can still confirm.";
} else if (state == SlateState.i2) {
// An invoice this wallet paid: the payee finalizes alone.
message =
"Cancelling changes this wallet only: the funds it locked become "
"spendable here again. If the payee already has your payment, "
"they can still complete it until those funds are spent in "
"another transaction.";
} else if (state == SlateState.s2 ||
(state == SlateState.unknown && incoming)) {
// A payment this wallet answered: the sender finalizes alone.
message =
"Cancelling removes this transaction from this wallet only. If "
"the sender already has your response, they can still complete "
"the payment, and the funds may then only show after a rescan.";
} else {
// This wallet has to finalize, and will not after a cancel.
message =
"The other party can no longer complete it, and any funds it "
"locked become spendable again.";
}
return SlatepackCancelPrompt(
title: "Cancel this transaction?",
message: message,
confirmLabel: "Yes, cancel",
);
}
}

/// A checked payment proof.
class PaymentProofCheck {
const PaymentProofCheck({
required this.amount,
required this.kernelExcess,
required this.senderAddress,
required this.recipientAddress,
required this.isSender,
required this.isRecipient,
});

final Amount amount;
final String kernelExcess;
final String senderAddress;
final String recipientAddress;

/// This wallet's address is the proof's sender.
final bool isSender;

/// This wallet's address is the proof's recipient.
final bool isRecipient;
}
Loading
Loading