/
SignaturePayload.ts
141 lines (121 loc) · 3.95 KB
/
SignaturePayload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyNumber, AnyU8a, IExtrinsicEra, IKeyringPair } 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';
import ExtrinsicEra from './ExtrinsicEra';
import Nonce from './NonceCompact';
interface SignaturePayloadValue {
nonce?: AnyNumber;
method?: Method;
era?: AnyU8a | IExtrinsicEra;
blockHash?: AnyU8a;
}
// a helper function for both types of payloads, Raw and metadata-known
function sign (signerPair: IKeyringPair, u8a: Uint8Array): Uint8Array {
const encoded = u8a.length > 256
? blake2AsU8a(u8a)
: u8a;
return signerPair.sign(encoded);
}
/**
* @name SignaturePayload
* @description
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
* on the contents included
*
* 1-8 bytes: The Transaction Compact<Index/Nonce> as provided in the transaction itself.
* 2+ bytes: The Function Descriptor as provided in the transaction itself.
* 1/2 bytes: The Transaction Era as provided in the transaction itself.
* 32 bytes: The hash of the authoring block implied by the Transaction Era and the current block.
*/
export default class SignaturePayload extends Struct {
protected _signature?: Uint8Array;
public constructor (value?: SignaturePayloadValue | Uint8Array) {
super({
nonce: Nonce,
method: Method,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description `true` if the payload refers to a valid signature
*/
public get isSigned (): boolean {
return !!(this._signature && this._signature.length === 64);
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
return this.get('blockHash') as Hash;
}
/**
* @description The [[Method]] contained in the payload
*/
public get method (): Method {
return this.get('method') as Method;
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[Nonce]]
*/
public get nonce (): Nonce {
return this.get('nonce') as Nonce;
}
/**
* @description The raw signature as a `Uint8Array`
*/
public get signature (): Uint8Array {
if (!this.isSigned) {
throw new Error('Transaction is not signed');
}
return this._signature as Uint8Array;
}
/**
* @description Sign the payload with the keypair
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sign (signerPair: IKeyringPair, version?: RuntimeVersion): Uint8Array {
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 {
public constructor (value?: any) {
super({
nonce: Nonce,
method: U8a,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description Sign the payload with the keypair
*/
public sign (signerPair: IKeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
}
}