Skip to content

open_anchor supplies txhash instead of txid #17

Closed

Description

@pm47

Hello,

This is just a matter of reversing the array, but the comment is a bit misleading

message open_anchor {
  // Transaction ID of anchor.
  required sha256_hash txid = 1;

See https://github.com/ElementsProject/lightning/blob/master/bitcoin/tx.c#L232

Cheers,

Pierre

Activity

  1. rustyrussell commented on Jan 27, 2016

    @rustyrussell
    Collaborator

    Pierre-Marie notifications@github.com writes:

    Hello,

    This is just a matter of reversing the array, but the comment is a bit misleading

    message open_anchor {
      // Transaction ID of anchor.
      required sha256_hash txid = 1;
    

    See https://github.com/ElementsProject/lightning/blob/master/bitcoin/tx.c#L232

    We always encode hashes as big-endian. That's the normal encoding for SHA.

    Unfortunately, bitcoind uses little-endian for transaction hashes for
    reasons unknown. It uses big-endian for block hashes though!

    It's wrong. I keep this script around to fix it up to paste into block:

    ( cat > hex_rev.c; chmod a+x hex_rev.c )

    // 2>/dev/null; set -e; OUT=/tmp/basename $0 .c; if [ ! -f "$OUT" ] || [ "$OUT" -ot "$0" ]; then gcc -Wall -g -o "$OUT".$$ $0 && mv "$OUT".$$ "$OUT"; fi; exec "$OUT" "$@"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <err.h>
    #include <stdbool.h>

    static char hexchar(unsigned int x)
    {
    if (x < 10)
    return '0' + x;
    else
    return 'a' - 10 + x;
    }

    static void print_hex(const unsigned char *val, size_t len)
    {
    size_t i;

    for (i = 0; i < len; i++)
        printf("%c%c", hexchar(val[i] >> 4), hexchar(val[i] % 16));
    printf("\n");
    

    }

    static bool char_to_hex(unsigned char *val, char c)
    {
    if (c >= '0' && c <= '9') {
    *val = c - '0';
    return true;
    }
    if (c >= 'a' && c <= 'f') {
    *val = c - 'a' + 10;
    return true;
    }
    if (c >= 'A' && c <= 'F') {
    *val = c - 'A' + 10;
    return true;
    }
    return false;
    }

    static bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
    {
    unsigned char v1, v2;
    unsigned char *p = buf;

    while (slen > 1) {
        if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
            return false;
        if (!bufsize)
            return false;
        *(p++) = (v1 << 4) | v2;
        str += 2;
        slen -= 2;
        bufsize--;
    }
    return slen == 0 && bufsize == 0;
    

    }

    static void reverse_hash(unsigned char *hex, size_t len)
    {
    unsigned int i;

    for (i = 0; i < len / 2; i++) {
        unsigned char tmp = hex[i];
        hex[i] = hex[len - 1 - i];
        hex[len - 1 - i] = tmp;
    }
    

    }

    int main(int argc, char *argv[])
    {
    int i;

    for (i = 1; i < argc; i++) {
        unsigned char hex[strlen(argv[i]) / 2];
        if (!from_hex(argv[i], strlen(argv[i]), hex, sizeof(hex)))
            errx(1, "Bad hex '%s'", argv[i]);
        reverse_hash(hex, sizeof(hex));
        print_hex(hex, sizeof(hex));
    }
    return 0;
    

    }

  2. pm47 commented on Feb 8, 2016

    @pm47
    ContributorAuthor

    Fair enough!
    Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions