Skip to content

Commit

Permalink
add SEP-7 support to Flutter Wallet SDK (stellar#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete authored Oct 10, 2024
1 parent f092797 commit 44ea9ff
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/build/apps/wallet/component/dart/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CodeExample } from "@site/src/components/CodeExample";

```dart
// pubspec.yaml
stellar_wallet_flutter_sdk: ^0.3.6
stellar_flutter_sdk: ^1.8.6
stellar_wallet_flutter_sdk: ^1.0.0
stellar_flutter_sdk: ^1.8.8
```

</CodeExample>
Expand Down
70 changes: 70 additions & 0 deletions docs/build/apps/wallet/sep7.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ const uri = wallet.parseSep7Uri(txUri);
// uri can be parsed and transaction can be signed/submitted by an application that implements Sep-7
```

```dart
final sourceAccountKeyPair = PublicKeyPair.fromAccountId('G...');
final destinationAccountKeyPair = PublicKeyPair.fromAccountId('G...');
var txBuilder = await stellar.transaction(sourceAccountKeyPair);
final tx = txBuilder.createAccount(destinationAccountKeyPair).build();
final xdr = Uri.encodeComponent(tx.toEnvelopeXdrBase64());
final callback = Uri.encodeComponent('https://example.com/callback');
final txUri = 'web+stellar:tx?xdr=$xdr&callback=$callback';
final uri = wallet.parseSep7Uri(txUri);
// uri can be parsed and transaction can be signed/submitted by an application that implements Sep-7
```

</CodeExample>

You can set replacements to be made in the xdr for specific fields by the application, these will be added in the [Sep-11 transaction representation format] to the URI.
Expand All @@ -45,6 +57,17 @@ uri.addReplacement({
});
```

```dart
final uri = wallet.parseSep7Uri(txUri);
if (uri is Sep7Tx) {
uri.addReplacement(Sep7Replacement(
id: 'X',
path: 'sourceAccount',
hint: 'account from where you want to pay fees'));
}
```

</CodeExample>

You can assign parameters after creating the initial instance using the appropriate setter for the parameter.
Expand All @@ -64,6 +87,18 @@ uri.msg = "here goes a message";
uri.toString(); // encodes everything and converts to a uri string
```

```dart
final sourceAccountKeyPair = PublicKeyPair.fromAccountId('G...');
final destinationAccountKeyPair = PublicKeyPair.fromAccountId('G...');
var txBuilder = await stellar.transaction(sourceAccountKeyPair);
final tx = txBuilder.createAccount(destinationAccountKeyPair).build();
final uri = Sep7Tx.forTransaction(tx);
uri.setCallback('https://example.com/callback');
uri.setMsg('here goes a message');
uri.toString(); // encodes everything and converts to a uri string
```

</CodeExample>

## Pay Operation
Expand All @@ -85,6 +120,19 @@ const uri = parseSep7Uri(payUri);
// uri can be parsed and transaction can be built/signed/submitted by an application that implements Sep-7
```

```dart
const destination = 'G...';
const assetIssuer = 'G...';
const assetCode = 'USDC';
const amount = '120.1234567';
const memo = 'memo';
final message = Uri.encodeComponent('pay me with lumens');
const originDomain = 'example.com';
final payUri = 'web+stellar:pay?destination=$destination&amount=$amount&memo=$memo&msg=$message&origin_domain=$originDomain&asset_issuer=$assetIssuer&asset_code=$assetCode';
final uri = Sep7.parseSep7Uri(payUri);
// uri can be parsed and transaction can be built/signed/submitted by an application that implements Sep-7
```

</CodeExample>

You can assign parameters after creating the initial instance using the appropriate setter for the parameter.
Expand All @@ -101,6 +149,16 @@ uri.amount = "10";
uri.toString(); // encodes everything and converts to a uri string
```

```dart
final uri = Sep7Pay.forDestination('G...');
uri.setCallback('https://example.com/callback');
uri.setMsg('here goes a message');
uri.setAssetCode('USDC');
uri.setAssetIssuer('G...');
uri.setAmount('10');
uri.toString(); // encodes everything and converts to a uri string
```

</CodeExample>

The last step after building a `Sep7Tx` or `Sep7Pay` is to add a signature to your uri. This will create a payload out of the transaction and sign it with the provided keypair.
Expand All @@ -115,6 +173,14 @@ uri.addSignature(Keypair.fromSecret(keypair.secretKey));
console.log(uri.signature); // signed uri payload
```

```dart
final uri = Sep7Pay.forDestination('G...');
uri.setOriginDomain('example.com');
final keypair = wallet.stellar().account().createKeyPair();
uri.addSignature(keypair);
print(uri.getSignature()); // signed uri payload
```

</CodeExample>

The signature can then be verified by fetching the [Stellar toml file] from the origin domain in the uri, and using the included signing key to verify the uri signature. This is all done as part of the `verifySignature` method.
Expand All @@ -125,6 +191,10 @@ The signature can then be verified by fetching the [Stellar toml file] from the
const passesVerification = await uri.verifySignature(); // true or false
```

```dart
final passesVerification = await uri.verifySignature(); // true or false
```

</CodeExample>

[Sep-7]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0007.md
Expand Down

0 comments on commit 44ea9ff

Please sign in to comment.