Add SignaturePayloadRaw (#967)

This commit is contained in:
Jaco Greeff
2019-06-04 11:23:41 +02:00
committed by GitHub
parent b4a675c378
commit b37eb31b60
3 changed files with 36 additions and 6 deletions
+34 -6
View File
@@ -8,6 +8,7 @@ import { AnyNumber, AnyU8a } from '../types';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Struct from '../codec/Struct';
import U8a from '../codec/U8a';
import Hash from '../primitive/Hash';
import Method from '../primitive/Method';
import RuntimeVersion from '../rpc/RuntimeVersion';
@@ -21,6 +22,15 @@ type SignaturePayloadValue = {
blockHash?: AnyU8a
};
// a helper function for both types of payloads, Raw and metadata-known
function sign (signerPair: KeyringPair, u8a: Uint8Array): Uint8Array {
const encoded = u8a.length > 256
? blake2AsU8a(u8a)
: u8a;
return signerPair.sign(encoded);
}
/**
* @name SignaturePayload
* @description
@@ -94,13 +104,31 @@ export default class SignaturePayload extends Struct {
* @description Sign the payload with the keypair
*/
sign (signerPair: KeyringPair, version?: RuntimeVersion): Uint8Array {
const u8a = this.toU8a();
const encoded = u8a.length > 256
? blake2AsU8a(u8a)
: u8a;
this._signature = signerPair.sign(encoded);
this._signature = sign(signerPair, this.toU8a());
return this._signature;
}
}
/**
* @name SignaturePayloadRaw
* @description
* A version of [[SignaturePayload]] where it does not rely on [[Method]] being initalized with metadata. When constructing, it treats the [[Method]] as a raw stream of bytes, so will always apply the signature over this without any additional checking. Unlike the [[SignaturePayload]], it assumed that you will only construct and sign, thereby providing no insigt into constructed values
*/
export class SignaturePayloadRaw extends Struct {
constructor (value?: any) {
super({
nonce: Nonce,
method: U8a,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description Sign the payload with the keypair
*/
sign (signerPair: KeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
}
}
+1
View File
@@ -53,6 +53,7 @@ export { default as Schedule } from './Schedule';
export { default as SeedOf } from './SeedOf';
export { default as SessionKey } from './SessionKey';
export { default as Signature, Ed25519Signature, Sr25519Signature } from './Signature';
export { default as SignaturePayload, SignaturePayloadRaw } from './SignaturePayload';
export { default as StakingLedger } from './StakingLedger';
export { default as StoredPendingChange } from './StoredPendingChange';
export { default as TreasuryProposal } from './TreasuryProposal';