addr list and test

This commit is contained in:
Sebastian 2022-04-20 15:14:18 -03:00
parent 37ce3972f9
commit 0cb4e4498d
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
3 changed files with 45 additions and 34 deletions

View File

@ -24,21 +24,21 @@ import {
} from "./bitcoin.js"; } from "./bitcoin.js";
test("generate testnet", (t) => { test("generate testnet", (t) => {
const { addr1, addr2 } = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v") const [addr1, addr2] = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "tb1qhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v")
t.assert(addr1 === "tb1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxpf0lfjw"); t.assert(addr1 === "tb1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxpf0lfjw");
t.assert(addr2 === "tb1qmfwqwa5vr5vdac6wr20ts76aewakzpmns40yuf"); t.assert(addr2 === "tb1qmfwqwa5vr5vdac6wr20ts76aewakzpmns40yuf");
}); });
test("generate mainnet", (t) => { test("generate mainnet", (t) => {
const { addr1, addr2 } = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq") const [addr1, addr2] = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq")
//bc //bc
t.assert(addr1 === "bc1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxprfy6fa"); t.assert(addr1 === "bc1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxprfy6fa");
t.assert(addr2 === "bc1qmfwqwa5vr5vdac6wr20ts76aewakzpmn6n5h86"); t.assert(addr2 === "bc1qmfwqwa5vr5vdac6wr20ts76aewakzpmn6n5h86");
}); });
test("generate Regtest", (t) => { test("generate Regtest", (t) => {
const { addr1, addr2 } = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v") const [addr1, addr2] = generateFakeSegwitAddress("B9E0EXNDKGJX7WFAEVZCZXM0R661T66YWD71N7NRFDEWQEV10XSG", "bcrtqhxrhccqexg0dv4nltgkuw4fg2ce7muplmjsn0v")
t.assert(addr1 === "bcrt1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxptxxy98"); t.assert(addr1 === "bcrt1qtfwqwaj6tsrhdtvuyhflr6nklm8ldqxptxxy98");
t.assert(addr2 === "bcrt1qmfwqwa5vr5vdac6wr20ts76aewakzpmnjukftq"); 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, [])
});

View File

@ -26,10 +26,6 @@ import { AmountJson, Amounts } from "./amounts.js";
import { decodeCrock } from "./talerCrypto.js"; import { decodeCrock } from "./talerCrypto.js";
import * as segwit from "./segwit_addr.js"; import * as segwit from "./segwit_addr.js";
export interface SegwitAddrs {
addr1: string;
addr2: string;
}
function buf2hex(buffer: Uint8Array) { function buf2hex(buffer: Uint8Array) {
// buffer is an ArrayBuffer // buffer is an ArrayBuffer
@ -43,10 +39,17 @@ const hext2buf = (hexString: string) =>
export function generateFakeSegwitAddress( export function generateFakeSegwitAddress(
reservePub: string, reservePub: string | undefined,
addr: string addr: string
): SegwitAddrs { ): string[] {
const pub = decodeCrock(reservePub); 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); const first_rnd = new Uint8Array(4);
first_rnd.set(pub.subarray(0, 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); const second_part = new Uint8Array(first_rnd.length + pub.length / 2);
second_part.set(second_rnd, 0); second_part.set(second_rnd, 0);
second_part.set(pub.subarray(16), 4); second_part.set(pub.subarray(16, 32), 4);
const prefix = const prefix =
addr[0] === "t" && addr[1] == "b" addr[0] === "t" && addr[1] == "b"
@ -74,10 +77,10 @@ export function generateFakeSegwitAddress(
: undefined; : undefined;
if (prefix === undefined) throw new Error("unknown bitcoin net"); if (prefix === undefined) throw new Error("unknown bitcoin net");
return { const addr1 = segwit.default.encode(prefix, 0, first_part);
addr1: segwit.default.encode(prefix, 0, first_part), const addr2 = segwit.default.encode(prefix, 0, second_part);
addr2: segwit.default.encode(prefix, 0, second_part),
}; return [addr1, addr2]
} }
// https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.cpp // https://github.com/bitcoin/bitcoin/blob/master/src/policy/policy.cpp

View File

@ -49,27 +49,11 @@ export interface PaytoUriTalerBank extends PaytoUriGeneric {
export interface PaytoUriBitcoin extends PaytoUriGeneric { export interface PaytoUriBitcoin extends PaytoUriGeneric {
isKnown: true; isKnown: true;
targetType: "bitcoin"; targetType: "bitcoin";
addr1: string; segwitAddrs: Array<string>;
addr2: string;
} }
const paytoPfx = "payto://"; 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 * Add query parameters to a payto URI
*/ */
@ -158,14 +142,14 @@ export function parsePaytoUri(s: string): PaytoUri | undefined {
if (targetType === "bitcoin") { if (targetType === "bitcoin") {
const msg = /\b([A-Z0-9]{52})\b/.exec(params["message"]) const msg = /\b([A-Z0-9]{52})\b/.exec(params["message"])
const reserve = !msg ? params["subject"] : msg[0]; const reserve = !msg ? params["subject"] : msg[0];
const { addr1, addr2 } = generateFakeSegwitAddress(reserve, targetPath); const segwitAddrs = !reserve ? [] : generateFakeSegwitAddress(reserve, targetPath);
const result: PaytoUriBitcoin = { const result: PaytoUriBitcoin = {
isKnown: true, isKnown: true,
targetPath, targetPath,
targetType, targetType,
params, params,
addr1, addr2 segwitAddrs
}; };
return result; return result;