Skip to content

Commit

Permalink
[extrinsic] fix extrinsic codec for AssetHubExtensions (leonardocus…
Browse files Browse the repository at this point in the history
…todio#401)

* [extrinsic] fix extrinsic codec for `AssetHubExtensions`

* fix: add registry in `toEncodedMap`

* fmt
  • Loading branch information
clangenb authored Jan 26, 2024
1 parent 6929fc8 commit ca49c1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
25 changes: 20 additions & 5 deletions packages/polkadart/lib/extrinsic/extrinsic_payload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Extrinsic {
this.assetId,
});

toEncodedMap() {
toEncodedMap(dynamic registry) {
return {
'signer': signer,
'method': method,
Expand All @@ -39,7 +39,7 @@ class Extrinsic {
? '00'
: Era.codec.encodeMortal(blockNumber, eraPeriod),
'nonce': encodeHex(CompactCodec.codec.encode(nonce)),
'assetId': assetId != null ? assetId!.toRadixString(16) : '',
'assetId': maybeAssetIdEncoded(registry),
'tip': tip is int
? encodeHex(CompactCodec.codec.encode(tip))
: encodeHex(CompactBigIntCodec.codec.encode(tip)),
Expand Down Expand Up @@ -68,15 +68,18 @@ class Extrinsic {
final List<String> extras = <String>[];

late final SignedExtensions signedExtensions;
if (assetId != null) {

if (_usesChargeAssetTxPayment(registry)) {
signedExtensions = SignedExtensions.assetHubSignedExtensions;
} else {
signedExtensions = SignedExtensions.substrateSignedExtensions;
}

for (final signedExtensiontype in registry.getSignedExtensionTypes()) {
final payload =
signedExtensions.signedExtension(signedExtensiontype, toEncodedMap());
final payload = signedExtensions.signedExtension(
signedExtensiontype,
toEncodedMap(registry),
);

if (payload.isNotEmpty) {
extras.add(payload);
Expand Down Expand Up @@ -108,4 +111,16 @@ class Extrinsic {
dynamic registry, SignatureType signatureType, keyring.KeyPair keyPair) {
return keyPair.sign(encode(registry, signatureType));
}

bool _usesChargeAssetTxPayment(dynamic registry) {
return registry.getSignedExtensionTypes().contains('ChargeAssetTxPayment');
}

String maybeAssetIdEncoded(dynamic registry) {
if (_usesChargeAssetTxPayment(registry)) {
return assetId != null ? assetId!.toRadixString(16) : '00';
} else {
return '';
}
}
}
26 changes: 20 additions & 6 deletions packages/polkadart/lib/extrinsic/signing_payload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SigningPayload {
this.assetId,
});

toEncodedMap() {
toEncodedMap(dynamic registry) {
return {
'method': method,
'specVersion': encodeHex(U32Codec.codec.encode(specVersion)),
Expand All @@ -42,7 +42,7 @@ class SigningPayload {
? '00'
: Era.codec.encodeMortal(blockNumber, eraPeriod),
'nonce': encodeHex(CompactCodec.codec.encode(nonce)),
'assetId': assetId != null ? assetId!.toRadixString(16) : '',
'assetId': maybeAssetIdEncoded(registry),
'tip': tip is int
? encodeHex(CompactCodec.codec.encode(tip))
: encodeHex(CompactBigIntCodec.codec.encode(tip)),
Expand All @@ -58,24 +58,26 @@ class SigningPayload {
final List<String> additionalExtras = <String>[];

late final SignedExtensions signedExtensions;
if (assetId != null) {
if (_usesChargeAssetTxPayment(registry)) {
signedExtensions = SignedExtensions.assetHubSignedExtensions;
} else {
signedExtensions = SignedExtensions.substrateSignedExtensions;
}

registry.getSignedExtensionTypes().forEach((extension) {
final payload =
signedExtensions.signedExtension(extension, toEncodedMap());
signedExtensions.signedExtension(extension, toEncodedMap(registry));

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

registry.getSignedExtensionExtra().forEach((extension) {
final payload =
signedExtensions.additionalSignedExtension(extension, toEncodedMap());
final payload = signedExtensions.additionalSignedExtension(
extension,
toEncodedMap(registry),
);

if (payload.isNotEmpty) {
additionalExtras.add(payload);
Expand All @@ -88,4 +90,16 @@ class SigningPayload {

return Uint8List.fromList(hex.decode(payload));
}

bool _usesChargeAssetTxPayment(dynamic registry) {
return registry.getSignedExtensionTypes().contains('ChargeAssetTxPayment');
}

String maybeAssetIdEncoded(dynamic registry) {
if (_usesChargeAssetTxPayment(registry)) {
return assetId != null ? assetId!.toRadixString(16) : '00';
} else {
return '';
}
}
}

0 comments on commit ca49c1b

Please sign in to comment.