@polkadot/primitives
Advanced tools
Comparing version 0.28.17 to 0.28.18
@@ -14,15 +14,29 @@ "use strict"; | ||
var _error = _interopRequireDefault(require("@polkadot/util/ext/error")); | ||
var _toHex = _interopRequireDefault(require("@polkadot/util/u8a/toHex")); | ||
// Copyright 2017-2018 @polkadot/primitives authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the ISC license. See the LICENSE file for details. | ||
const VERSIONS = ['latest', 'poc-1']; | ||
function extrinsicDecode(extrinsic) { | ||
const addressDecoded = (0, _decode.default)('AccountId', extrinsic, 'latest'); | ||
const nonceDecoded = (0, _decode.default)('AccountIndex', extrinsic.subarray(addressDecoded.length), 'latest'); | ||
const extrinsicDecoded = (0, _decode.default)('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), 'latest'); | ||
const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length); | ||
return (0, _objectSpread2.default)({}, extrinsicDecoded.value, { | ||
address: addressDecoded.value, | ||
nonce: nonceDecoded.value, | ||
signature | ||
}); | ||
for (let version of VERSIONS) { | ||
const addressDecoded = (0, _decode.default)('AccountId', extrinsic, version); | ||
const nonceDecoded = (0, _decode.default)('AccountIndex', extrinsic.subarray(addressDecoded.length), version); | ||
const extrinsicDecoded = (0, _decode.default)('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), version); | ||
const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length); | ||
const totalLength = addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length + signature.length; | ||
if (totalLength === extrinsic.length) { | ||
return (0, _objectSpread2.default)({}, extrinsicDecoded.value, { | ||
address: addressDecoded.value, | ||
accountIndex: nonceDecoded.value, | ||
signature | ||
}); | ||
} | ||
} | ||
throw new _error.default(`Unable to decode extrinsic: ${(0, _toHex.default)(extrinsic)}`); | ||
} |
@@ -19,2 +19,6 @@ // Copyright 2017-2018 @polkadot/primitives authors & contributors | ||
const poc1 = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 36, 241, 242, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
]); | ||
describe('extrinsic', () => { | ||
@@ -28,2 +32,20 @@ extrinsics.forEach((extrinsic, index) => { | ||
}); | ||
it('decodes poc-1 extrinsic', () => { | ||
expect( | ||
decode(poc1).extrinsic | ||
).toMatchObject({ | ||
name: 'set', | ||
section: 'timestamp' | ||
}); | ||
}); | ||
it('decodes poc-2/latest extrinsic', () => { | ||
expect( | ||
decode(extrinsics[1]).extrinsic | ||
).toMatchObject({ | ||
name: 'setHeads', | ||
section: 'parachains' | ||
}); | ||
}); | ||
}); |
@@ -5,19 +5,30 @@ // Copyright 2017-2018 @polkadot/primitives authors & contributors | ||
import { BlockExtrinsicDecoded, ExtrinsicDecoded } from '@polkadot/params/types'; | ||
import { BlockExtrinsicDecoded, ExtrinsicDecoded, EncodingVersions } from '@polkadot/params/types'; | ||
import BN from 'bn.js'; | ||
import decoder from '@polkadot/params/decode'; | ||
import ExtError from '@polkadot/util/ext/error'; | ||
import u8aToHex from '@polkadot/util/u8a/toHex'; | ||
const VERSIONS: Array<EncodingVersions> = ['latest', 'poc-1']; | ||
export default function extrinsicDecode (extrinsic: Uint8Array): BlockExtrinsicDecoded { | ||
const addressDecoded = decoder('AccountId', extrinsic, 'latest'); | ||
const nonceDecoded = decoder('AccountIndex', extrinsic.subarray(addressDecoded.length), 'latest'); | ||
const extrinsicDecoded = decoder('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), 'latest'); | ||
const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length); | ||
for (let version of VERSIONS) { | ||
const addressDecoded = decoder('AccountId', extrinsic, version); | ||
const nonceDecoded = decoder('AccountIndex', extrinsic.subarray(addressDecoded.length), version); | ||
const extrinsicDecoded = decoder('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), version); | ||
const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length); | ||
const totalLength = addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length + signature.length; | ||
return { | ||
...(extrinsicDecoded.value as ExtrinsicDecoded), | ||
address: addressDecoded.value as string, | ||
nonce: nonceDecoded.value as BN, | ||
signature | ||
}; | ||
if (totalLength === extrinsic.length) { | ||
return { | ||
...(extrinsicDecoded.value as ExtrinsicDecoded), | ||
address: addressDecoded.value as string, | ||
accountIndex: nonceDecoded.value as BN, | ||
signature | ||
}; | ||
} | ||
} | ||
throw new ExtError(`Unable to decode extrinsic: ${u8aToHex(extrinsic)}`); | ||
} |
{ | ||
"name": "@polkadot/primitives", | ||
"version": "0.28.17", | ||
"version": "0.28.18", | ||
"description": "Type defintions for the Polkadot network", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
117497
194
2844