Giter Club home page Giter Club logo

Comments (2)

rustyrussell avatar rustyrussell commented on May 22, 2024

Pierre-Marie [email protected] 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;

}

from lightning.

pm47 avatar pm47 commented on May 22, 2024

Fair enough!
Cheers

from lightning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.