Skip to content

Commit

Permalink
Adds assethub signed extensions (leonardocustodio#351)
Browse files Browse the repository at this point in the history
* Add assethub

* Bump version

* lint
  • Loading branch information
leonardocustodio authored Oct 31, 2023
1 parent 2f2fda1 commit 86309e1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/polkadart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## 0.2.1

- Added AssetHub signed extensions.

## 0.2.0

- Added support for creating extrinsics and query RPC api and listen to events.

## 0.1.1

- Publishing packages under polkadart.dev publisher
- Publishing packages under polkadart.dev publisher.

## 0.1.0

Expand Down
11 changes: 9 additions & 2 deletions packages/polkadart/lib/extrinsic/extrinsic_payload.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:polkadart/extrinsic/signed_extensions/asset_hub.dart';
import 'package:polkadart/extrinsic/signed_extensions/substrate.dart';
import 'package:polkadart_scale_codec/primitives/primitives.dart';
import 'package:polkadart_scale_codec/utils/utils.dart';
Expand All @@ -15,6 +16,7 @@ class Extrinsic {
final int blockNumber;
final int nonce;
final dynamic tip;
final int? assetId;

const Extrinsic({
required this.signer,
Expand All @@ -24,6 +26,7 @@ class Extrinsic {
required this.blockNumber,
required this.nonce,
required this.tip,
this.assetId,
});

toEncodedMap() {
Expand All @@ -33,6 +36,7 @@ class Extrinsic {
'signature': signature,
'era': Era.codec.encodeMortal(blockNumber, eraPeriod),
'nonce': encodeHex(CompactCodec.codec.encode(nonce)),
'assetId': assetId != null ? assetId!.toRadixString(16) : '',
'tip': tip is int
? encodeHex(CompactCodec.codec.encode(tip))
: encodeHex(CompactBigIntCodec.codec.encode(tip)),
Expand Down Expand Up @@ -65,8 +69,11 @@ class Extrinsic {
final List extras = [];

registry.getSignedExtensionTypes().forEach((extension) {
final payload =
SubstrateSignedExtensions.signedExtension(extension, toEncodedMap());
final payload = assetId != null
? AssetHubSignedExtensions.signedExtension(extension, toEncodedMap())
: SubstrateSignedExtensions.signedExtension(
extension, toEncodedMap());

if (payload.isNotEmpty) {
extras.add(payload);
}
Expand Down
25 changes: 19 additions & 6 deletions packages/polkadart/lib/extrinsic/signed_extensions/asset_hub.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
class AssetHubSignedExtensions {
static String signedExtensionPayload(String extension, Map info) {
static String signedExtension(String extension, Map info) {
switch (extension) {
case 'CheckMortality':
return info['era'];
case 'CheckNonce':
return info['nonce'];
case 'ChargeAssetTxPayment':
return '${info['tip']}${info['assetId']}';
default:
return '';
}
}

static String additionalSignedExtension(String extension, Map info) {
switch (extension) {
case 'CheckNonce':
return info['nonce'];
case 'ChargeAssetTxPayment':
return '${info['tip']}${info['assetId']}';
case 'CheckSpecVersion':
return info['specVersion'];
case 'CheckTxVersion':
return info['transactionVersion'];
case 'CheckGenesis':
return info['genesisHash'];
case 'CheckMortality':
return info['era'];
case 'CheckNonce':
return info['nonce'];
case 'ChargeAssetTxPayment':
return info['tip'];
return info['blockHash'];
default:
return '';
}
Expand Down
23 changes: 17 additions & 6 deletions packages/polkadart/lib/extrinsic/signing_payload.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:polkadart/extrinsic/signed_extensions/asset_hub.dart';
import 'package:polkadart/extrinsic/signed_extensions/substrate.dart';
import 'package:polkadart/scale_codec.dart';
import 'package:polkadart/substrate/era.dart';
Expand All @@ -15,6 +16,7 @@ class SigningPayload {
final int eraPeriod; // CheckMortality
final int nonce; // CheckNonce
final dynamic tip; // ChargeTransactionPayment
final int? assetId; // ChargeAssetTxPayment

const SigningPayload({
required this.method,
Expand All @@ -26,6 +28,7 @@ class SigningPayload {
required this.eraPeriod,
required this.nonce,
required this.tip,
this.assetId,
});

toEncodedMap() {
Expand All @@ -34,10 +37,11 @@ class SigningPayload {
'specVersion': encodeHex(U32Codec.codec.encode(specVersion)),
'transactionVersion':
encodeHex(U32Codec.codec.encode(transactionVersion)),
'genesisHash': genesisHash,
'blockHash': blockHash,
'genesisHash': genesisHash.replaceAll('0x', ''),
'blockHash': blockHash.replaceAll('0x', ''),
'era': Era.codec.encodeMortal(blockNumber, eraPeriod),
'nonce': encodeHex(CompactCodec.codec.encode(nonce)),
'assetId': assetId != null ? assetId!.toRadixString(16) : '',
'tip': tip is int
? encodeHex(CompactCodec.codec.encode(tip))
: encodeHex(CompactBigIntCodec.codec.encode(tip)),
Expand All @@ -53,16 +57,23 @@ class SigningPayload {
final List additionalExtras = [];

registry.getSignedExtensionTypes().forEach((extension) {
final payload =
SubstrateSignedExtensions.signedExtension(extension, toEncodedMap());
final payload = assetId != null
? AssetHubSignedExtensions.signedExtension(extension, toEncodedMap())
: SubstrateSignedExtensions.signedExtension(
extension, toEncodedMap());

if (payload.isNotEmpty) {
extras.add(payload);
}
});

registry.getSignedExtensionExtra().forEach((extension) {
final payload = SubstrateSignedExtensions.additionalSignedExtension(
extension, toEncodedMap());
final payload = assetId != null
? AssetHubSignedExtensions.additionalSignedExtension(
extension, toEncodedMap())
: SubstrateSignedExtensions.additionalSignedExtension(
extension, toEncodedMap());

if (payload.isNotEmpty) {
additionalExtras.add(payload);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/polkadart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: polkadart
description: Interfaces for interacting with Polkadot JSON RPC interface
version: 0.2.0
version: 0.2.1
homepage: https://github.com/leonardocustodio/polkadart/tree/main/packages/polkadart
repository: https://github.com/leonardocustodio/polkadart

Expand Down

0 comments on commit 86309e1

Please sign in to comment.