-
Notifications
You must be signed in to change notification settings - Fork 110
/
signature.ts
106 lines (90 loc) · 2.36 KB
/
signature.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
import {
ComparableToken,
SemanticEncoding,
Token,
TokenFactory,
TokenValidationError,
} from './token';
import { validateSignature, ValidationResult } from '@taquito/utils';
import { BaseTokenSchema } from '../schema/types';
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing a Signature
*/
export class SignatureValidationError extends TokenValidationError {
name = 'SignatureValidationError';
constructor(public value: any, public token: SignatureToken, message: string) {
super(value, token, message);
}
}
export class SignatureToken extends ComparableToken {
static prim: 'signature' = 'signature' as const;
constructor(
protected val: { prim: string; args: any[]; annots: any[] },
protected idx: number,
protected fac: TokenFactory
) {
super(val, idx, fac);
}
public Execute(val: { [key: string]: string }): string {
if (val.string) {
return val.string;
}
// TODO decode the signature
return val.bytes;
}
/**
* @throws {@link SignatureValidationError}
*/
private validate(value: any) {
if (validateSignature(value) !== ValidationResult.VALID) {
throw new SignatureValidationError(value, this, 'Signature is not valid');
}
}
/**
* @throws {@link SignatureValidationError}
*/
public Encode(args: any[]): any {
const val = args.pop();
this.validate(val);
return { string: val };
}
/**
* @throws {@link SignatureValidationError}
*/
public EncodeObject(val: any, semantic?: SemanticEncoding): any {
this.validate(val);
if (semantic && semantic[SignatureToken.prim]) {
return semantic[SignatureToken.prim](val);
}
return { string: val };
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
public ExtractSchema() {
return SignatureToken.prim;
}
generateSchema(): BaseTokenSchema {
return {
__michelsonType: SignatureToken.prim,
schema: SignatureToken.prim,
};
}
ToKey(val: any) {
return this.Execute(val);
}
ToBigMapKey(val: string) {
return {
key: { string: val },
type: { prim: SignatureToken.prim },
};
}
findAndReturnTokens(tokenToFind: string, tokens: Token[]) {
if (SignatureToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}