<p>I probably was too eager to ask this question, though perhaps someone can take a look at how I implemented the solution <a href="https://github.com/jgarzik/picocoin/pull/105" rel="nofollow noreferrer" title="UAHF Pull Request for Picocoin">in this pull request</a>.  Here is the relevant code:</p>

<pre><code>// from bp_tx_sigserializer function
void uahf_ser_scriptCode(cstring *s, const cstring *scriptCode){

    if(scriptCode == NULL){
        cstr_append_c(s, 0);
        return;
    }
    /** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
    struct const_buffer it = { scriptCode-&gt;str, scriptCode-&gt;len };
    struct const_buffer itBegin = it;
    struct bscript_op op;
    unsigned int nCodeSeparators = 0;

    struct bscript_parser bp;
    bsp_start(&amp;bp, &amp;it);

    while (bsp_getop(&amp;op, &amp;bp)) {
        if (op.op == OP_CODESEPARATOR)
            nCodeSeparators++;
    }
    cstring *x = cstr_new_sz(1024*8);

    it = itBegin;
    bsp_start(&amp;bp, &amp;it);

    while (bsp_getop(&amp;op, &amp;bp)) {
        if (op.op == OP_CODESEPARATOR) {
            ser_bytes(x, itBegin.p, it.p - itBegin.p - 1);
            itBegin  = it;
        }
    }

    if (itBegin.p != scriptCode-&gt;str + scriptCode-&gt;len)
        ser_bytes(x, itBegin.p, it.p - itBegin.p);

    if(0 &lt; nCodeSeparators){
        cstr_free(x, true);
        x = cstr_new_sz(1024*8);
        cstr_append_buf(x,scriptCode-&gt;str,scriptCode-&gt;len);
    }
    ser_varlen(s, x-&gt;len);
    cstr_append_buf(s,x-&gt;str,x-&gt;len);
    cstr_free(x, true);
}
</code></pre>

CC BY-SA 3.0
2017-09-12T05:32:14.033Z
2017-09-12T05:32:14.033Z
Joel D
<p>I have managed to get correct sighashes for situations in which there are no OP_CODESEPARATOR ops in the code.  However, I cannot figure out, based on the UAHF documentation nor on the C++ code in the Bitcoin-ABC repository, how I am supposed to serialize the scriptCodes with the codeseparator OP.  Here is my serialization function (from the Picocoin C library):</p>

<pre><code>void uahf_ser_scriptCode(cstring *s, const cstring *scriptCode){

    if(scriptCode == NULL){
        cstr_append_c(s, 0);
        return;
    }
    struct const_buffer it = { scriptCode-&gt;str, scriptCode-&gt;len };


    struct const_buffer itBegin = it;
    struct bscript_op op;
    unsigned int nCodeSeparators = 0;
    struct bscript_parser bp;

    bsp_start(&amp;bp, &amp;it);
    while (bsp_getop(&amp;op, &amp;bp)) {
        if (op.op == OP_CODESEPARATOR){
            nCodeSeparators++;
            fprintf(stderr,"code separator\n");
        }

    }


    if(nCodeSeparators == 0){
        ser_varlen(s, scriptCode-&gt;len);
        ser_bytes(s, scriptCode-&gt;str, scriptCode-&gt;len);
        return;
    }



    //ser_varlen(s, scriptCode-&gt;len - nCodeSeparators);
    it = itBegin;
    bsp_start(&amp;bp, &amp;it);
    int count = 0;
    cstring *x = cstr_new_sz(1024*10);
    while (bsp_getop(&amp;op, &amp;bp) &amp;&amp; count &lt; nCodeSeparators) {
        if (op.op == OP_CODESEPARATOR) {
            count++;
            //ser_bytes(s, itBegin.p, it.p - itBegin.p - 1);
            /*if(count == nCodeSeparators){
                ser_bytes(x, itBegin.p, it.p - itBegin.p - 1);
            }*/
            itBegin  = it;
        }
    }


    if (itBegin.p != scriptCode-&gt;str + scriptCode-&gt;len)
        ser_bytes(x, itBegin.p, it.p - itBegin.p);

    ser_varlen(s,  x-&gt;len);
    cstr_append_buf(s,x-&gt;str,x-&gt;len);
    cstr_free(x, true);

}
</code></pre>

CC BY-SA 3.0
2017-09-12T00:40:33.780Z
Not a full answer, but perhaps it helps you already: bcash's sighash scheme is basically identical to BIP143.
Pieter Wuille
CC BY-SA 3.0
2017-09-12T05:28:38.793Z
I finally figured out what I was doing wrong.  I just needed to include the OP_SEPARATORS when using the BIP143 scheme.
Joel D
<bitcoin-cash>
UAHF implementing sighash, specifically, how are scriptCodes serialized?
Joel D
# UAHF implementing sighash, specifically, how are scriptCodes serialized?

(Asked by: username_0 on 2017-09-12)

I have managed to get correct sighashes for situations in which there are no OP\_CODESEPARATOR ops in the code. However, I cannot figure out, based on the UAHF documentation nor on the C++ code in the Bitcoin-ABC repository, how I am supposed to serialize the scriptCodes with the codeseparator OP. Here is my serialization function (from the Picocoin C library):

```
void uahf_ser_scriptCode(cstring *s, const cstring *scriptCode){

    if(scriptCode == NULL){
        cstr_append_c(s, 0);
        return;
    }
    struct const_buffer it = { scriptCode->str, scriptCode->len };

    struct const_buffer itBegin = it;
    struct bscript_op op;
    unsigned int nCodeSeparators = 0;
    struct bscript_parser bp;

    bsp_start(&bp, &it);
    while (bsp_getop(&op, &bp)) {
        if (op.op == OP_CODESEPARATOR){
            nCodeSeparators++;
            fprintf(stderr,"code separator\n");
        }

    }

    if(nCodeSeparators == 0){
        ser_varlen(s, scriptCode->len);
        ser_bytes(s, scriptCode->str, scriptCode->len);
        return;
    }

    //ser_varlen(s, scriptCode->len - nCodeSeparators);
    it = itBegin;
    bsp_start(&bp, &it);
    int count = 0;
    cstring *x = cstr_new_sz(1024*10);
    while (bsp_getop(&op, &bp) && count < nCodeSeparators) {
        if (op.op == OP_CODESEPARATOR) {
            count++;
            //ser_bytes(s, itBegin.p, it.p - itBegin.p - 1);
            /*if(count == nCodeSeparators){
                ser_bytes(x, itBegin.p, it.p - itBegin.p - 1);
            }*/
            itBegin  = it;
        }
    }

    if (itBegin.p != scriptCode->str + scriptCode->len)
        ser_bytes(x, itBegin.p, it.p - itBegin.p);

    ser_varlen(s,  x->len);
    cstr_append_buf(s,x->str,x->len);
    cstr_free(x, true);

}

```

**Tags:** bitcoin-cash

> **Comments:**
> **username_1**: Not a full answer, but perhaps it helps you already: bcash's sighash scheme is basically identical to BIP143.  
> **username_0**: I finally figured out what I was doing wrong.  I just needed to include the OP_SEPARATORS when using the BIP143 scheme.  

---

## 1 answers

### Answer by username_0 on 2017-09-12. Score: 0

I probably was too eager to ask this question, though perhaps someone can take a look at how I implemented the solution [in this pull request](https://github.com/jgarzik/picocoin/pull/105 "UAHF Pull Request for Picocoin"). Here is the relevant code:

```
// from bp_tx_sigserializer function
void uahf_ser_scriptCode(cstring *s, const cstring *scriptCode){

    if(scriptCode == NULL){
        cstr_append_c(s, 0);
        return;
    }
    /** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
    struct const_buffer it = { scriptCode->str, scriptCode->len };
    struct const_buffer itBegin = it;
    struct bscript_op op;
    unsigned int nCodeSeparators = 0;

    struct bscript_parser bp;
    bsp_start(&bp, &it);

    while (bsp_getop(&op, &bp)) {
        if (op.op == OP_CODESEPARATOR)
            nCodeSeparators++;
    }
    cstring *x = cstr_new_sz(1024*8);

    it = itBegin;
    bsp_start(&bp, &it);

    while (bsp_getop(&op, &bp)) {
        if (op.op == OP_CODESEPARATOR) {
            ser_bytes(x, itBegin.p, it.p - itBegin.p - 1);
            itBegin  = it;
        }
    }

    if (itBegin.p != scriptCode->str + scriptCode->len)
        ser_bytes(x, itBegin.p, it.p - itBegin.p);

    if(0 < nCodeSeparators){
        cstr_free(x, true);
        x = cstr_new_sz(1024*8);
        cstr_append_buf(x,scriptCode->str,scriptCode->len);
    }
    ser_varlen(s, x->len);
    cstr_append_buf(s,x->str,x->len);
    cstr_free(x, true);
}

```


---

