Skip to content

Commit d262955

Browse files
authored
Merge pull request #99 from XuJiandong/fix-since
fix: Enhance multisig handling for optional since value
2 parents 9341664 + 07ec6b5 commit d262955

File tree

4 files changed

+76
-43
lines changed

4 files changed

+76
-43
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const BINARIES: &[(&str, &str)] = &[
2727
),
2828
(
2929
"secp256k1_blake160_multisig_all",
30-
"50c8623ef5112510ccdf2d8e480d02d0de7288eb9968f8b019817340c3991145",
30+
"36c971b8d41fbd94aabca77dc75e826729ac98447b46f91e00796155dddb0d29",
3131
),
3232
];
3333

c/secp256k1_blake160_multisig_all.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ int main() {
114114
return ERROR_ARGUMENTS_LEN;
115115
}
116116
// Extract optional since value.
117-
uint64_t since = 0;
118117
if (args_bytes_seg.size == BLAKE160_SIZE + sizeof(uint64_t)) {
119-
since = *(uint64_t *)&args_bytes_seg.ptr[BLAKE160_SIZE];
118+
uint64_t since = *(uint64_t *)&args_bytes_seg.ptr[BLAKE160_SIZE];
119+
// Check lock period logic, we have prepared a handy utility function for this.
120+
ret = check_since(since);
121+
if (ret != CKB_SUCCESS) {
122+
return ret;
123+
}
120124
}
121125

122126
// Load the first witness, or the witness of the same index as the first input using
@@ -195,12 +199,6 @@ int main() {
195199
return ERROR_MULTSIG_SCRIPT_HASH;
196200
}
197201

198-
// Check lock period logic, we have prepared a handy utility function for this.
199-
ret = check_since(since);
200-
if (ret != CKB_SUCCESS) {
201-
return ret;
202-
}
203-
204202
// Load the current transaction hash.
205203
unsigned char tx_hash[BLAKE2B_BLOCK_SIZE];
206204
len = BLAKE2B_BLOCK_SIZE;

docs/c/secp256k1_blake160_multisig_all.html

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,8 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
270270

271271
</div>
272272

273-
<div class="content"><div class='highlight'><pre> <span class="hljs-type">uint64_t</span> since = <span class="hljs-number">0</span>;
274-
<span class="hljs-keyword">if</span> (args_bytes_seg.size == BLAKE160_SIZE + <span class="hljs-keyword">sizeof</span>(<span class="hljs-type">uint64_t</span>)) {
275-
since = *(<span class="hljs-type">uint64_t</span> *)&amp;args_bytes_seg.ptr[BLAKE160_SIZE];
276-
}</pre></div></div>
273+
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (args_bytes_seg.size == BLAKE160_SIZE + <span class="hljs-keyword">sizeof</span>(<span class="hljs-type">uint64_t</span>)) {
274+
<span class="hljs-type">uint64_t</span> since = *(<span class="hljs-type">uint64_t</span> *)&amp;args_bytes_seg.ptr[BLAKE160_SIZE];</pre></div></div>
277275

278276
</li>
279277

@@ -284,6 +282,25 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
284282
<div class="sswrap ">
285283
<a class="ss" href="#section-11">&#x00a7;</a>
286284
</div>
285+
<p>Check lock period logic, we have prepared a handy utility function for this.</p>
286+
287+
</div>
288+
289+
<div class="content"><div class='highlight'><pre> ret = check_since(since);
290+
<span class="hljs-keyword">if</span> (ret != CKB_SUCCESS) {
291+
<span class="hljs-keyword">return</span> ret;
292+
}
293+
}</pre></div></div>
294+
295+
</li>
296+
297+
298+
<li id="section-12">
299+
<div class="annotation">
300+
301+
<div class="sswrap ">
302+
<a class="ss" href="#section-12">&#x00a7;</a>
303+
</div>
287304
<p>Load the first witness, or the witness of the same index as the first input using
288305
current script.</p>
289306

@@ -302,11 +319,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
302319
</li>
303320

304321

305-
<li id="section-12">
322+
<li id="section-13">
306323
<div class="annotation">
307324

308325
<div class="sswrap ">
309-
<a class="ss" href="#section-12">&#x00a7;</a>
326+
<a class="ss" href="#section-13">&#x00a7;</a>
310327
</div>
311328
<p>We will treat the first witness as WitnessArgs object, and extract the lock field
312329
from the object.</p>
@@ -326,11 +343,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
326343
</li>
327344

328345

329-
<li id="section-13">
346+
<li id="section-14">
330347
<div class="annotation">
331348

332349
<div class="sswrap ">
333-
<a class="ss" href="#section-13">&#x00a7;</a>
350+
<a class="ss" href="#section-14">&#x00a7;</a>
334351
</div>
335352
<p>This is more of a safe guard, since lock is a field in witness, it
336353
cannot exceed the maximum size of the enclosing witness, this way
@@ -346,11 +363,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
346363
</li>
347364

348365

349-
<li id="section-14">
366+
<li id="section-15">
350367
<div class="annotation">
351368

352369
<div class="sswrap ">
353-
<a class="ss" href="#section-14">&#x00a7;</a>
370+
<a class="ss" href="#section-15">&#x00a7;</a>
354371
</div>
355372
<p>Keep the full lock field somewhere, since later we will modify this field in place.</p>
356373

@@ -363,11 +380,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
363380
</li>
364381

365382

366-
<li id="section-15">
383+
<li id="section-16">
367384
<div class="annotation">
368385

369386
<div class="sswrap ">
370-
<a class="ss" href="#section-15">&#x00a7;</a>
387+
<a class="ss" href="#section-16">&#x00a7;</a>
371388
</div>
372389
<p>Extract multisig script flags.</p>
373390

@@ -396,11 +413,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
396413
</li>
397414

398415

399-
<li id="section-16">
416+
<li id="section-17">
400417
<div class="annotation">
401418

402419
<div class="sswrap ">
403-
<a class="ss" href="#section-16">&#x00a7;</a>
420+
<a class="ss" href="#section-17">&#x00a7;</a>
404421
</div>
405422
<p>Based on the number of public keys and thresholds, we can calculate
406423
the required length of the lock field.</p>
@@ -417,11 +434,11 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
417434
</li>
418435

419436

420-
<li id="section-17">
437+
<li id="section-18">
421438
<div class="annotation">
422439

423440
<div class="sswrap ">
424-
<a class="ss" href="#section-17">&#x00a7;</a>
441+
<a class="ss" href="#section-18">&#x00a7;</a>
425442
</div>
426443
<p>Perform hash check of the <code>multisig_script</code> part, notice the signature part
427444
is not included here.</p>
@@ -440,24 +457,6 @@ <h1 id="secp256k1-blake160-multisig-all">secp256k1-blake160-multisig-all</h1>
440457
</li>
441458

442459

443-
<li id="section-18">
444-
<div class="annotation">
445-
446-
<div class="sswrap ">
447-
<a class="ss" href="#section-18">&#x00a7;</a>
448-
</div>
449-
<p>Check lock period logic, we have prepared a handy utility function for this.</p>
450-
451-
</div>
452-
453-
<div class="content"><div class='highlight'><pre> ret = check_since(since);
454-
<span class="hljs-keyword">if</span> (ret != CKB_SUCCESS) {
455-
<span class="hljs-keyword">return</span> ret;
456-
}</pre></div></div>
457-
458-
</li>
459-
460-
461460
<li id="section-19">
462461
<div class="annotation">
463462

src/tests/secp256k1_blake160_multisig_all.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,42 @@ fn test_multisig_0_2_3_unlock_with_since() {
465465
}
466466
}
467467

468+
#[test]
469+
fn test_multisig_0_2_3_unlock_without_since() {
470+
let mut data_loader = DummyDataLoader::new();
471+
let keys = generate_keys(3);
472+
let multi_sign_script = gen_multi_sign_script(&keys, 2, 0);
473+
// no since value
474+
let args = blake160(&multi_sign_script);
475+
let _lock_script = gen_multi_sign_lock_script(args.clone());
476+
let raw_tx = gen_tx(&mut data_loader, args);
477+
478+
{
479+
// relative since
480+
let since = 0x8000_0000_8888_8888u64;
481+
let inputs: Vec<CellInput> = raw_tx
482+
.inputs()
483+
.into_iter()
484+
.map(|i| i.as_builder().since(since.pack()).build())
485+
.collect();
486+
let raw_tx = raw_tx.as_advanced_builder().set_inputs(inputs).build();
487+
let tx = multi_sign_tx(raw_tx, &multi_sign_script, &[&keys[1], &keys[2]]);
488+
verify(&data_loader, &tx).expect("pass verification");
489+
}
490+
{
491+
// absolute since
492+
let since = 0x2000_0000_0000_0000u64;
493+
let inputs: Vec<CellInput> = raw_tx
494+
.inputs()
495+
.into_iter()
496+
.map(|i| i.as_builder().since(since.pack()).build())
497+
.collect();
498+
let raw_tx = raw_tx.as_advanced_builder().set_inputs(inputs).build();
499+
let tx = multi_sign_tx(raw_tx, &multi_sign_script, &[&keys[1], &keys[2]]);
500+
verify(&data_loader, &tx).expect("pass verification");
501+
}
502+
}
503+
468504
#[test]
469505
fn test_multisig_0_2_3_unlock_with_since_epoch() {
470506
let mut data_loader = DummyDataLoader::new();

0 commit comments

Comments
 (0)