aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-04-20 15:14:18 -0300
committerSebastian <sebasjm@gmail.com>2022-04-20 15:14:18 -0300
commit0cb4e4498db5d87e0bc89062204b746992b22279 (patch)
tree8ac0a7cbcf576c1ec18e093b749f420809c21c31 /packages/taler-util
parent37ce3972f9bc37c0b96ed78814dc99303b202859 (diff)
addr list and test
Diffstat (limited to 'packages/taler-util')
-rw-r--r--packages/taler-util/src/bitcoin.test.ts30
-rw-r--r--packages/taler-util/src/bitcoin.ts27
-rw-r--r--packages/taler-util/src/payto.ts22
3 files changed, 45 insertions, 34 deletions
diff --git a/packages/taler-util/src/bitcoin.test.ts b/packages/taler-util/src/bitcoin.test.ts
index 287cd7e66..3070a8ab5 100644
--- a/packages/taler-util/src/bitcoin.test.ts
+++ b/packages/taler-util/src/bitcoin.test.ts
@@ -24,21 +24,21 @@ import {
} from "./bitcoin.js";
test("generate testnet", (t) => {
- const { addr1, addr2 } = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v")
+ const [addr1, addr2] = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v")
t.assert(addr1 === "tb1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxpf0lfjw");
t.assert(addr2 === "tb1qmfwqwa5vr5vdac6wr20ts76aewakzpmns40yuf");
});
test("generate mainnet", (t) => {
- const { addr1, addr2 } = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq")
+ 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")
+ const [addr1, addr2] = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v")
t.assert(addr1 === "bcrt1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxptxxy98");
t.assert(addr2 === "bcrt1qmfwqwa5vr5vdac6wr20ts76aewakzpmnjukftq");
@@ -51,3 +51,27 @@ test("unknown net", (t) => {
})
});
+
+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, [])
+});
+
diff --git a/packages/taler-util/src/bitcoin.ts b/packages/taler-util/src/bitcoin.ts
index 62d8d8dce..822652a8a 100644
--- a/packages/taler-util/src/bitcoin.ts
+++ b/packages/taler-util/src/bitcoin.ts
@@ -26,10 +26,6 @@ import { AmountJson, Amounts } from "./amounts.js";
import { decodeCrock } from "./talerCrypto.js";
import * as segwit from "./segwit_addr.js";
-export interface SegwitAddrs {
- addr1: string;
- addr2: string;
-}
function buf2hex(buffer: Uint8Array) {
// buffer is an ArrayBuffer
@@ -43,10 +39,17 @@ const hext2buf = (hexString: string) =>
export function generateFakeSegwitAddress(
- reservePub: string,
+ reservePub: string | undefined,
addr: string
-): SegwitAddrs {
- const pub = decodeCrock(reservePub);
+): string[] {
+ if (!reservePub) return []
+ let pub;
+ try {
+ pub = decodeCrock(reservePub);
+ } catch {
+ // pub = new Uint8Array(0)
+ }
+ if (!pub || pub.length !== 32) return []
const first_rnd = new Uint8Array(4);
first_rnd.set(pub.subarray(0, 4))
@@ -62,7 +65,7 @@ export function generateFakeSegwitAddress(
const second_part = new Uint8Array(first_rnd.length + pub.length / 2);
second_part.set(second_rnd, 0);
- second_part.set(pub.subarray(16), 4);
+ second_part.set(pub.subarray(16, 32), 4);
const prefix =
addr[0] === "t" && addr[1] == "b"
@@ -74,10 +77,10 @@ export function generateFakeSegwitAddress(
: undefined;
if (prefix === undefined) throw new Error("unknown bitcoin net");
- return {
- addr1: segwit.default.encode(prefix, 0, first_part),
- addr2: segwit.default.encode(prefix, 0, second_part),
- };
+ const addr1 = segwit.default.encode(prefix, 0, first_part);
+ const addr2 = segwit.default.encode(prefix, 0, second_part);
+
+ return [addr1, addr2]
}
// https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.cpp
diff --git a/packages/taler-util/src/payto.ts b/packages/taler-util/src/payto.ts
index 7dcb28098..f1596579f 100644
--- a/packages/taler-util/src/payto.ts
+++ b/packages/taler-util/src/payto.ts
@@ -49,27 +49,11 @@ export interface PaytoUriTalerBank extends PaytoUriGeneric {
export interface PaytoUriBitcoin extends PaytoUriGeneric {
isKnown: true;
targetType: "bitcoin";
- addr1: string;
- addr2: string;
+ segwitAddrs: Array<string>;
}
const paytoPfx = "payto://";
-function buildSegwitGenerator(result: PaytoUriBitcoin, targetPath: string) {
- //generate segwit address just once, save addr in payto object
- //and use it as cache
- return function generateSegwitAddress(reserve: string): {
- addr1: string;
- addr2: string;
- } {
- if (result.addr1 && result.addr2)
- return { addr1: result.addr1, addr2: result.addr2 };
- const { addr1, addr2 } = generateFakeSegwitAddress(reserve, targetPath);
- result.addr1 = addr1;
- result.addr2 = addr2;
- return { addr1, addr2 };
- };
-}
/**
* Add query parameters to a payto URI
*/
@@ -158,14 +142,14 @@ export function parsePaytoUri(s: string): PaytoUri | undefined {
if (targetType === "bitcoin") {
const msg = /\b([A-Z0-9]{52})\b/.exec(params["message"])
const reserve = !msg ? params["subject"] : msg[0];
- const { addr1, addr2 } = generateFakeSegwitAddress(reserve, targetPath);
+ const segwitAddrs = !reserve ? [] : generateFakeSegwitAddress(reserve, targetPath);
const result: PaytoUriBitcoin = {
isKnown: true,
targetPath,
targetType,
params,
- addr1, addr2
+ segwitAddrs
};
return result;