aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/bitcoin.test.ts
blob: fe8de89c34a6c9df2e16ec40365bedfbb7b96d80 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 This file is part of GNU Taler
 (C) 2018-2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Type-safe codecs for converting from/to JSON.
 */

import test from "ava";
import { generateFakeSegwitAddress } from "./bitcoin.js";

test("generate testnet", (t) => {
  const [addr1, addr2] = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
    "tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );

  t.assert(addr1 === "tb1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxpf0lfjw");
  t.assert(addr2 === "tb1qmfwqwa5vr5vdac6wr20ts76aewakzpmns40yuf");
});

test("generate mainnet", (t) => {
  const [addr1, addr2] = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
    "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
  );
  //bc
  t.assert(addr1 === "bc1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxprfy6fa");
  t.assert(addr2 === "bc1qmfwqwa5vr5vdac6wr20ts76aewakzpmn6n5h86");
});

test("generate Regtest", (t) => {
  const [addr1, addr2] = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );

  t.assert(addr1 === "bcrt1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxptxxy98");
  t.assert(addr2 === "bcrt1qmfwqwa5vr5vdac6wr20ts76aewakzpmnjukftq");
});

test("unknown net", (t) => {
  t.throws(() => {
    generateFakeSegwitAddress(
      "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
      "abqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
    );
  });
});

test("invalid or no reserve", (t) => {
  let result = undefined;
  // empty
  result = generateFakeSegwitAddress(
    "",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  // small
  result = generateFakeSegwitAddress(
    "s",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  result = generateFakeSegwitAddress(
    "asdsad",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  result = generateFakeSegwitAddress(
    "asdasdasdasdasdasd",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  result = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XS",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  result = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSSSS",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  // no reserve
  result = generateFakeSegwitAddress(
    undefined,
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
  result = generateFakeSegwitAddress(
    "B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XS-",
    "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
  );
  t.deepEqual(result, []);
});