wallet-core/packages/taler-util/src/bitcoin.test.ts

109 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-04-20 18:25:13 +02:00
/*
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";
2022-09-21 20:43:35 +02:00
import { generateFakeSegwitAddress } from "./bitcoin.js";
2022-04-20 18:25:13 +02:00
test("generate testnet", (t) => {
2022-09-21 20:43:35 +02:00
const [addr1, addr2] = generateFakeSegwitAddress(
"B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
"tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
);
2022-04-20 18:25:13 +02:00
t.assert(addr1 === "tb1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxpf0lfjw");
t.assert(addr2 === "tb1qmfwqwa5vr5vdac6wr20ts76aewakzpmns40yuf");
});
test("generate mainnet", (t) => {
2022-09-21 20:43:35 +02:00
const [addr1, addr2] = generateFakeSegwitAddress(
"B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
"bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
);
2022-04-20 18:25:13 +02:00
//bc
t.assert(addr1 === "bc1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxprfy6fa");
t.assert(addr2 === "bc1qmfwqwa5vr5vdac6wr20ts76aewakzpmn6n5h86");
});
test("generate Regtest", (t) => {
2022-09-21 20:43:35 +02:00
const [addr1, addr2] = generateFakeSegwitAddress(
"B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
"bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
);
2022-04-20 18:25:13 +02:00
t.assert(addr1 === "bcrt1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxptxxy98");
t.assert(addr2 === "bcrt1qmfwqwa5vr5vdac6wr20ts76aewakzpmnjukftq");
});
test("unknown net", (t) => {
t.throws(() => {
2022-09-21 20:43:35 +02:00
generateFakeSegwitAddress(
"B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG",
"abqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
);
});
2022-04-20 18:25:13 +02:00
});
2022-04-20 20:14:18 +02:00
test("invalid or no reserve", (t) => {
let result = undefined;
2022-09-21 20:43:35 +02:00
// 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, []);
2022-04-20 20:14:18 +02:00
// no reserve
2022-09-21 20:43:35 +02:00
result = generateFakeSegwitAddress(
undefined,
"bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
);
t.deepEqual(result, []);
result = generateFakeSegwitAddress(
"B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XS-",
"bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v",
);
t.deepEqual(result, []);
2022-04-20 20:14:18 +02:00
});