Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add safe network user transfer example #133

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.2"
version: "1.2.3"
path:
dependency: transitive
description:
Expand Down
42 changes: 42 additions & 0 deletions example/safe_network_user_transfer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:mixin_bot_sdk_dart/mixin_bot_sdk_dart.dart';

import 'common.dart';
import 'safe_upgrade.dart';

// the key store can from safe_upgrade.dart
final _networkUserKeystore = KeyStore(
userId: "32cef966-4184-3826-9be1-77470cb6b0ec",
sessionId: "695775f4-76ac-4eb1-a126-1de49e110f03",
sessionPrivateKey:
"986e92602425e1d8f6a5e6a0237a0ac43c327d81579c413fd7b574b65ae75abf",
spendKey: "51788fd1555838e1313ccf3466b8cf95e56d25cc7e269953135e8be75576f485",
pinTokenBase64: "_5c1H2IKZxMWDvIN-p4BU58okomPeyWnbmlGZlUmZxU",
);

void main() async {
final sessionKey = Key.fromHexSeed(_networkUserKeystore.sessionPrivateKey);
final spendKey = Key.fromHexSeed(_networkUserKeystore.spendKey);

final userClient = Client(
sessionPrivateKey: sessionKey,
sessionId: _networkUserKeystore.sessionId,
userId: _networkUserKeystore.userId,
);

(await userClient.accountApi.getMe()).data;
print("spend public key: ${spendKey.publicKey().toHexString()}");

print(
"use Mixin Messenger scan the qrcode, and transfer to this network user");
print('mixin://pay/${_networkUserKeystore.userId}?asset=$cnb');

final txs = await userClient.utxoApi.transactionToUser(
receiverIds: ["cfb018b0-eaf7-40ec-9e07-28a5158f1269"],
amount: "0.01",
asset: cnb,
spendKey: spendKey,
);
for (final tx in txs) {
print("view tx in https://viewblock.io/mixin/tx/${tx.transactionHash}");
}
}
36 changes: 35 additions & 1 deletion example/safe_upgrade.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:math' as math;
import 'dart:typed_data';

Expand All @@ -7,10 +8,34 @@ import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
import '../lib/mixin_bot_sdk_dart.dart';
import 'common.dart';

class KeyStore {
final String userId;
final String sessionId;
final String sessionPrivateKey;
final String spendKey;
final String pinTokenBase64;

KeyStore({
required this.userId,
required this.sessionId,
required this.sessionPrivateKey,
required this.spendKey,
required this.pinTokenBase64,
});

Map<String, dynamic> toJson() => {
"user_id": userId,
"session_id": sessionId,
"session_private_key": sessionPrivateKey,
"spend_key": spendKey,
"pin_token_base64": pinTokenBase64,
};
}

/// Samples for upgrade legacy pin to safe
void main() async {
final sessionKey = ed.generateKey();
print(hex.encode(sessionKey.privateKey.bytes));
print(hex.encode(ed.seed(sessionKey.privateKey)));

final user = (await client.userApi.createUsers(
fullName: 'User_${math.Random().nextInt(10)}',
Expand Down Expand Up @@ -58,4 +83,13 @@ void main() async {
);

print('account: ${account.userId} ${account.hasSafe}');

final keystore = KeyStore(
userId: user.userId,
sessionId: user.sessionId,
sessionPrivateKey: hex.encode(ed.seed(sessionKey.privateKey)),
spendKey: hex.encode(ed.seed(keyPair.privateKey)),
pinTokenBase64: user.pinTokenBase64,
);
print('keystore(please save it): \n${jsonEncode(keystore.toJson())}');
}
3 changes: 3 additions & 0 deletions lib/src/util/key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Key {
factory Key.fromHexSeed(String seedInHex) =>
Key.fromSeed(seedInHex.hexToBytes());

Uint8List publicKey() =>
Uint8List.fromList(ed.public(ed.PrivateKey(raw)).bytes);

final Uint8List _raw;

Uint8List get raw => _raw;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/util/safe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ String signSafeTransaction({
final index = utxo.keys.indexOf(key.publicKey().hexString());
if (index == -1) {
throw Exception(
'invalid public key for the input: $i ${utxo.keys}, ${key.publicKey().hexString()}');
'invalid public key for the input: $i ${utxo.keys}, ${key.publicKey().hexString()}.\n'
'maybe this is a invalid spendKey: ${privateKey.publicKey().toHexString()}');
}
final sigs = <int, String>{};
final sig = key.sign(msg);
Expand Down
Loading