Skip to content

Commit 45032e0

Browse files
committed
Added fromScript to AFTER fragment.
1 parent 3a224af commit 45032e0

File tree

1 file changed

+20
-7
lines changed
  • modules/bitcoin-miniscript/fragments

1 file changed

+20
-7
lines changed

modules/bitcoin-miniscript/fragments/AFTER.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import {
2-
NUMBER,
3-
} from "../../../universal-tokens";
1+
import { NUMBER } from "../../../universal-tokens";
42
import { lexKeyword } from "../../../lex-utils";
53
import { Types } from "../../../miniscript-types";
64
import {
@@ -41,9 +39,7 @@ export class AFTER
4139

4240
let locktime = NUMBER.parse(parseContext);
4341

44-
if (locktime < 1 || locktime >= 0x80000000) {
45-
throw new Error("Locktime invalid");
46-
}
42+
this.validateLocktime(locktime);
4743

4844
return new AFTER(locktime);
4945
};
@@ -78,6 +74,23 @@ export class AFTER
7874
};
7975

8076
toScript = () => {
81-
return `${this.locktime} CHECKLOCKTIMEVERIFY`;
77+
return `${this.locktime} OP_CHECKLOCKTIMEVERIFY`;
78+
};
79+
80+
static fromScript = (opcodes: string[]) => {
81+
let locktime = parseInt(opcodes[1]);
82+
if (isNaN(locktime)) {
83+
return;
84+
}
85+
86+
if (opcodes[0] == "OP_CHECKLOCKTIMEVERIFY") {
87+
return new AFTER(locktime);
88+
}
8289
};
90+
91+
private static validateLocktime(locktime: number) {
92+
if (locktime < 1 || locktime >= 0x80000000) {
93+
throw new Error("Locktime invalid");
94+
}
95+
}
8396
}

0 commit comments

Comments
 (0)