Skip to content

Commit df11a4a

Browse files
authored
Merge pull request #1171 from interlay/fix/coinbase-serialization
fix: coinbase serialization
2 parents 60a1b72 + 3b2cfa0 commit df11a4a

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

crates/bitcoin/src/script.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ impl Script {
4242
}
4343
}
4444

45+
// If the most significant byte is >= 0x80 and the value is positive, push a
46+
// new zero-byte to make the significant byte < 0x80 again.
47+
// See https://github.com/bitcoin/bitcoin/blob/b565485c24c0feacae559a7f6f7b83d7516ca58d/src/script/script.h#L360-L373
48+
if let Some(x) = height_bytes.last() {
49+
if (x & 0x80) != 0 {
50+
height_bytes.push(0);
51+
}
52+
}
53+
4554
// note: formatting the height_bytes vec automatically prepends the length of the vec, so no need
4655
// to append it manually
4756
script.append(height_bytes);
@@ -147,6 +156,16 @@ impl std::convert::TryFrom<&str> for Script {
147156
#[test]
148157
fn test_script_height() {
149158
assert_eq!(Script::height(7).bytes, vec![1, 7]);
159+
// 2^7 boundary
160+
assert_eq!(Script::height(127).bytes, vec![1, 127]);
161+
assert_eq!(Script::height(128).bytes, vec![2, 128, 0]);
162+
// 2^8 boundary
163+
assert_eq!(Script::height(255).bytes, vec![2, 0xff, 0x00]);
150164
assert_eq!(Script::height(256).bytes, vec![2, 0x00, 0x01]);
165+
// 2^15 boundary
166+
assert_eq!(Script::height(32767).bytes, vec![2, 0xff, 0x7f]);
167+
assert_eq!(Script::height(32768).bytes, vec![3, 0x00, 0x80, 0x00]);
168+
// 2^16 boundary
169+
assert_eq!(Script::height(65535).bytes, vec![3, 0xff, 0xff, 0x00]);
151170
assert_eq!(Script::height(65536).bytes, vec![3, 0x00, 0x00, 0x01]);
152171
}

0 commit comments

Comments
 (0)