forked from bitauth/libauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.ts
212 lines (198 loc) · 5.85 KB
/
time.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
AuthenticationProgramStateCommon,
ErrorState,
StackState
} from '../../state';
import { isScriptNumberError, parseBytesAsScriptNumber } from './common';
import { applyError, AuthenticationErrorCommon } from './errors';
import { OpcodesCommon } from './opcodes';
enum Bits {
sequenceLocktimeDisableFlag = 31,
sequenceLocktimeTypeFlag = 22
}
enum Constants {
LocktimeScriptNumberByteLength = 5,
LocktimeThreshold = 500_000_000,
maximumSequenceNumber = 0xffffffff,
sequenceLocktimeTransactionVersionMinimum = 2,
// tslint:disable-next-line: no-bitwise
sequenceLocktimeDisableFlag = (1 << Bits.sequenceLocktimeDisableFlag) >>> 0,
// tslint:disable-next-line: no-bitwise
sequenceLocktimeTypeFlag = 1 << Bits.sequenceLocktimeTypeFlag,
sequenceGranularity = 9,
sequenceLocktimeMask = 0x0000ffff
}
export const readLocktime = <
State extends StackState & ErrorState<Errors>,
Errors
>(
state: State,
operation: (nextState: State, locktime: number) => State,
flags: {
requireMinimalEncoding: boolean;
}
) => {
const item = state.stack[state.stack.length - 1] as Uint8Array | undefined;
// tslint:disable-next-line:no-if-statement
if (!item) {
return applyError<State, Errors>(
AuthenticationErrorCommon.emptyStack,
state
);
}
const parsedLocktime = parseBytesAsScriptNumber(
item,
flags.requireMinimalEncoding,
Constants.LocktimeScriptNumberByteLength
);
// tslint:disable-next-line: no-if-statement
if (isScriptNumberError(parsedLocktime)) {
return applyError<State, Errors>(
AuthenticationErrorCommon.invalidScriptNumber,
state
);
}
const locktime = Number(parsedLocktime);
// tslint:disable-next-line: no-if-statement
if (locktime < 0) {
return applyError<State, Errors>(
AuthenticationErrorCommon.negativeLocktime,
state
);
}
return operation(state, locktime);
};
const locktimeTypesAreCompatible = (
locktime: number,
requiredLocktime: number
) =>
(locktime < Constants.LocktimeThreshold &&
requiredLocktime < Constants.LocktimeThreshold) ||
(locktime >= Constants.LocktimeThreshold &&
requiredLocktime >= Constants.LocktimeThreshold);
export const opCheckLockTimeVerify = <
State extends StackState &
ErrorState<Errors> & {
readonly locktime: number;
readonly sequenceNumber: number;
},
Errors
>(flags: {
requireMinimalEncoding: boolean;
}) => (state: State) =>
readLocktime(
state,
(nextState, requiredLocktime) => {
// tslint:disable-next-line: no-if-statement
if (!locktimeTypesAreCompatible(nextState.locktime, requiredLocktime)) {
return applyError<State, Errors>(
AuthenticationErrorCommon.incompatibleLocktimeType,
nextState
);
}
// tslint:disable-next-line: no-if-statement
if (requiredLocktime > nextState.locktime) {
return applyError<State, Errors>(
AuthenticationErrorCommon.unsatisfiedLocktime,
nextState
);
}
// tslint:disable-next-line: no-if-statement
if (nextState.sequenceNumber === Constants.maximumSequenceNumber) {
return applyError<State, Errors>(
AuthenticationErrorCommon.locktimeDisabled,
nextState
);
}
return nextState;
},
flags
);
// tslint:disable-next-line: no-bitwise
const includesFlag = (value: number, flag: number) => (value & flag) !== 0;
export const opCheckSequenceVerify = <
State extends StackState &
ErrorState<Errors> & {
readonly sequenceNumber: number;
readonly version: number;
},
Errors
>(flags: {
requireMinimalEncoding: boolean;
}) => (state: State) =>
readLocktime(
state,
// tslint:disable-next-line: cyclomatic-complexity
(nextState, requiredSequence) => {
const sequenceLocktimeDisabled = includesFlag(
requiredSequence,
Constants.sequenceLocktimeDisableFlag
);
// tslint:disable-next-line: no-if-statement
if (sequenceLocktimeDisabled) {
return nextState;
}
// tslint:disable-next-line: no-if-statement
if (
nextState.version < Constants.sequenceLocktimeTransactionVersionMinimum
) {
return applyError<State, Errors>(
AuthenticationErrorCommon.checkSequenceUnavailable,
nextState
);
}
// tslint:disable-next-line: no-if-statement
if (
includesFlag(
nextState.sequenceNumber,
Constants.sequenceLocktimeDisableFlag
)
) {
return applyError<State, Errors>(
AuthenticationErrorCommon.unmatchedSequenceDisable,
nextState
);
}
// tslint:disable-next-line: no-if-statement
if (
includesFlag(requiredSequence, Constants.sequenceLocktimeTypeFlag) !==
includesFlag(
nextState.sequenceNumber,
Constants.sequenceLocktimeTypeFlag
)
) {
return applyError<State, Errors>(
AuthenticationErrorCommon.incompatibleSequenceType,
nextState
);
}
// tslint:disable-next-line: no-if-statement
if (
// tslint:disable-next-line: no-bitwise
(requiredSequence & Constants.sequenceLocktimeMask) >
// tslint:disable-next-line: no-bitwise
(nextState.sequenceNumber & Constants.sequenceLocktimeMask)
) {
return applyError<State, Errors>(
AuthenticationErrorCommon.unsatisfiedSequenceNumber,
nextState
);
}
return nextState;
},
flags
);
export const timeOperations = <
Opcodes,
State extends AuthenticationProgramStateCommon<Opcodes, Errors>,
Errors
>(flags: {
requireMinimalEncoding: boolean;
}) => ({
[OpcodesCommon.OP_CHECKLOCKTIMEVERIFY]: opCheckLockTimeVerify<State, Errors>(
flags
),
[OpcodesCommon.OP_CHECKSEQUENCEVERIFY]: opCheckSequenceVerify<State, Errors>(
flags
)
});