-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathbase58.c
71 lines (62 loc) · 1.72 KB
/
base58.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright 2012 Luke Dashjr
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the standard MIT license. See COPYING for more details.
*/
#ifndef WIN32
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <libbase58.h>
#include <blkmaker.h>
#include "private.h"
bool _blkmk_b58tobin(void *bin, size_t binsz, const char *b58, size_t b58sz) {
return b58tobin(bin, &binsz, b58, b58sz);
}
int _blkmk_b58check(void *bin, size_t binsz, const char *base58str) {
if (!b58_sha256_impl)
b58_sha256_impl = blkmk_sha256_impl;
return b58check(bin, binsz, base58str, 34);
}
size_t blkmk_address_to_script(void *out, size_t outsz, const char *addr) {
unsigned char addrbin[25];
unsigned char *cout = out;
const size_t b58sz = strlen(addr);
int addrver;
size_t rv;
rv = sizeof(addrbin);
if (!b58_sha256_impl)
b58_sha256_impl = blkmk_sha256_impl;
if (!b58tobin(addrbin, &rv, addr, b58sz))
return 0;
addrver = b58check(addrbin, sizeof(addrbin), addr, b58sz);
switch (addrver) {
case 0: // Bitcoin pubkey hash
case 111: // Testnet pubkey hash
if (outsz < (rv = 25))
return rv;
cout[ 0] = 0x76; // OP_DUP
cout[ 1] = 0xa9; // OP_HASH160
cout[ 2] = 0x14; // push 20 bytes
memcpy(&cout[3], &addrbin[1], 20);
cout[23] = 0x88; // OP_EQUALVERIFY
cout[24] = 0xac; // OP_CHECKSIG
return rv;
case 5: // Bitcoin script hash
case 196: // Testnet script hash
if (outsz < (rv = 23))
return rv;
cout[ 0] = 0xa9; // OP_HASH160
cout[ 1] = 0x14; // push 20 bytes
memcpy(&cout[2], &addrbin[1], 20);
cout[22] = 0x87; // OP_EQUAL
return rv;
default:
return 0;
}
}