Flutter电商功能插件commerciosdk的使用
Flutter电商功能插件Commercio SDK的使用
简介
本仓库包含官方Commercio.network Dart SDK的代码,完全基于Sacco.dart。
主要特性包括:
- 完全用Dart编写
- 完全无状态
由于这些特点,你可以在任何纯Dart项目或任何Flutter应用中使用此SDK。
你可以在这里找到官方文档:Commercio SDK 文档。
辅助方法
在SDK中,你将找到以下辅助方法,几乎可以帮你完成在Commercio.network区块链上执行的任何操作。
请注意,你可以在SDK文档中找到这些方法的使用示例。我们强烈建议你查看这些文档以获取完整的SDK引用。
加密方法
- 创建HD钱包
- 生成AES密钥
- 生成RSA密钥
文档相关方法
- 共享文档
- 发送收据
- 列出文档
- 列出收据
身份验证方法
- 创建Did文档
- 关联Did文档
- 请求提升
CommercioMint方法
- 铸造CCC
- 烧毁CCC
CommercioKYC方法
- 邀请用户
- 购买会员资格
- 存入奖励池
示例代码
以下是使用Commercio SDK的基本示例代码:
import 'package:sacco/sacco.dart';
import 'package:uuid/uuid.dart';
import 'package:commerciosdk/export.dart';
Future<void> main() async {
// --------------------------------------------
// --- 设置网络信息
// --------------------------------------------
final lcdUrl = Uri.parse('http://localhost:1337');
final networkInfo = NetworkInfo(
bech32Hrp: 'did:com:',
lcdUrl: lcdUrl,
);
// --------------------------------------------
// --- 创建用于setIdentity的钱包
// --------------------------------------------
final mnemonic = Bip39.generateMnemonic(strength: 256).split(' ');
final wallet = Wallet.derive(mnemonic, networkInfo);
// --------------------------------------------
// --- 创建setIdentity交易
// --------------------------------------------
// 注意,为了设置身份并进行区块链上的操作,[wallet]需要一些令牌,例如从tumbler或另一个钱包获得。
//
// 有关如何发送令牌,请参阅
// https://docs.commercio.network/developers/create-sign-broadcast-tx.html
final rsaVerificationKeyPair = await KeysHelper.generateRsaKeyPair(
keyType: CommercioRSAKeyType.verification,
);
final rsaVerificationPubKey = rsaVerificationKeyPair.publicKey;
final rsaSignatureKeyPair = await KeysHelper.generateRsaKeyPair(
keyType: CommercioRSAKeyType.signature,
);
final rsaSignaturePubKey = rsaSignatureKeyPair.publicKey;
try {
final didDocument = await DidDocumentHelper.fromWallet(
wallet: wallet,
pubKeys: [rsaVerificationPubKey, rsaSignaturePubKey],
);
print('DDO:\n${didDocument.toJson()}');
print('');
final response = await IdHelper.setDidDocumentsList(
[didDocument],
wallet,
);
if (response.success) {
print('TX 成功发送:\n$lcdUrl/txs/${response.hash}');
print('----- 你可以从以下URL检索你的did -----');
print('Endpoint:\n$lcdUrl/identities/${wallet.bech32Address}');
} else {
print('TX 错误:\n${response.error?.errorMessage}');
}
} catch (error) {
print('设置DDO时发生错误:\n$error');
}
// --------------------------------------------
// --- 创建用于shareDocument的两个钱包
// --------------------------------------------
final senderMnemonic = Bip39.generateMnemonic(strength: 256).split(' ');
final senderWallet = Wallet.derive(senderMnemonic, networkInfo);
final recipientMnemonic = Bip39.generateMnemonic(strength: 256).split(' ');
final recipientWallet = Wallet.derive(recipientMnemonic, networkInfo);
// --------------------------------------------
// --- 创建一个shareDocument交易
// --------------------------------------------
final docRecipientDid = recipientWallet.bech32Address;
final docId = const Uuid().v4();
final checksum = CommercioDocChecksum(
value: 'a00ab326fc8a3dd93ec84f7e7773ac2499b381c4833e53110107f21c3b90509c',
algorithm: CommercioDocChecksumAlgorithm.SHA256,
);
// 注意,为了使[senderWallet]共享的文档进行DoSign,[recipientWallet]必须在区块链上有身份。
//
// 设置身份的步骤描述在“创建setIdentity交易”部分。
//
// 有关详细信息,请参阅
// https://docs.commercio.network/x/id/tx/create-an-identity.html
final doSign = CommercioDoSign(
storageUri: 'http://www.commercio.network',
signerIstance: 'did:com:1cc65t29yuwuc32ep2h9uqhnwrregfq230lf2rj',
sdnData: const {
CommercioSdnData.COMMON_NAME,
CommercioSdnData.SURNAME,
},
vcrId: 'xxxxx',
certificateProfile: 'xxxxx',
);
try {
final commercioDoc = await CommercioDocHelper.fromWallet(
wallet: senderWallet,
recipients: [docRecipientDid],
id: docId,
metadata: CommercioDocMetadata(
contentUri: 'https://example.com/document/metadata',
schema: CommercioDocMetadataSchema(
uri: 'https://example.com/custom/metadata/schema',
version: '7.0.0',
),
),
contentUri: 'https://example.com/document',
checksum: checksum,
doSign: doSign,
encryptedData: {CommercioEncryptedData.CONTENT_URI},
);
final response = await DocsHelper.shareDocumentsList(
[commercioDoc],
senderWallet,
);
if (response.success) {
print('TX 成功发送:\n$lcdUrl/txs/${response.hash}');
print('----- 你可以从以下URL检索你的发送的文档 -----');
print('Endpoint:\n$lcdUrl/docs/${senderWallet.bech32Address}/sent');
} else {
print('TX 错误:\n${response.error?.errorMessage}');
}
} catch (error) {
print('共享文档时发生错误:\n$error');
}
}
更多关于Flutter电商功能插件commerciosdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复