summaryrefslogtreecommitdiff
path: root/packages/taler-util/src
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-03-03 17:49:28 -0300
committerSebastian <sebasjm@gmail.com>2022-03-03 17:50:19 -0300
commit7d8acca7f948ded5f188e2fb9af6b0a11f9c0572 (patch)
tree968d03e399061542d358bd71c3790336490880bb /packages/taler-util/src
parent39d9b1e6f8b63e10a0c8b6ad811d5d086b53abb7 (diff)
downloadwallet-core-7d8acca7f948ded5f188e2fb9af6b0a11f9c0572.tar.gz
wallet-core-7d8acca7f948ded5f188e2fb9af6b0a11f9c0572.tar.bz2
wallet-core-7d8acca7f948ded5f188e2fb9af6b0a11f9c0572.zip
changed implementation of bech32 to https://github.com/sipa/bech32/tree/master/ref/javascript
Diffstat (limited to 'packages/taler-util/src')
-rw-r--r--packages/taler-util/src/bech32.ts131
-rw-r--r--packages/taler-util/src/bitcoin.ts15
-rw-r--r--packages/taler-util/src/segwit_addr.ts91
3 files changed, 232 insertions, 5 deletions
diff --git a/packages/taler-util/src/bech32.ts b/packages/taler-util/src/bech32.ts
new file mode 100644
index 000000000..03c24e807
--- /dev/null
+++ b/packages/taler-util/src/bech32.ts
@@ -0,0 +1,131 @@
+// Copyright (c) 2017, 2021 Pieter Wuille
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
+var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
+
+const encodings: any = {
+ BECH32: "bech32",
+ BECH32M: "bech32m",
+};
+
+export default {
+ decode: decode,
+ encode: encode,
+ encodings: encodings,
+};
+
+function getEncodingConst(enc: any) {
+ if (enc == encodings.BECH32) {
+ return 1;
+ } else if (enc == encodings.BECH32M) {
+ return 0x2bc830a3;
+ } else {
+ throw new Error('unknown encoding')
+ }
+}
+
+function polymod(values: any) {
+ var chk = 1;
+ for (var p = 0; p < values.length; ++p) {
+ var top = chk >> 25;
+ chk = (chk & 0x1ffffff) << 5 ^ values[p];
+ for (var i = 0; i < 5; ++i) {
+ if ((top >> i) & 1) {
+ chk ^= GENERATOR[i];
+ }
+ }
+ }
+ return chk;
+}
+
+function hrpExpand(hrp: any) {
+ var ret = [];
+ var p;
+ for (p = 0; p < hrp.length; ++p) {
+ ret.push(hrp.charCodeAt(p) >> 5);
+ }
+ ret.push(0);
+ for (p = 0; p < hrp.length; ++p) {
+ ret.push(hrp.charCodeAt(p) & 31);
+ }
+ return ret;
+}
+
+function verifyChecksum(hrp: any, data: any, enc: any) {
+ return polymod(hrpExpand(hrp).concat(data)) === getEncodingConst(enc);
+}
+
+function createChecksum(hrp: any, data: any, enc: any) {
+ var values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
+ var mod = polymod(values) ^ getEncodingConst(enc);
+ var ret = [];
+ for (var p = 0; p < 6; ++p) {
+ ret.push((mod >> 5 * (5 - p)) & 31);
+ }
+ return ret;
+}
+
+function encode(hrp: any, data: any, enc: any): string {
+ var combined = data.concat(createChecksum(hrp, data, enc));
+ var ret = hrp + '1';
+ for (var p = 0; p < combined.length; ++p) {
+ ret += CHARSET.charAt(combined[p]);
+ }
+ return ret;
+}
+
+function decode(bechString: any, enc: any) {
+ var p;
+ var has_lower = false;
+ var has_upper = false;
+ for (p = 0; p < bechString.length; ++p) {
+ if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) {
+ return null;
+ }
+ if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) {
+ has_lower = true;
+ }
+ if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) {
+ has_upper = true;
+ }
+ }
+ if (has_lower && has_upper) {
+ return null;
+ }
+ bechString = bechString.toLowerCase();
+ var pos = bechString.lastIndexOf('1');
+ if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
+ return null;
+ }
+ var hrp = bechString.substring(0, pos);
+ var data = [];
+ for (p = pos + 1; p < bechString.length; ++p) {
+ var d = CHARSET.indexOf(bechString.charAt(p));
+ if (d === -1) {
+ return null;
+ }
+ data.push(d);
+ }
+ if (!verifyChecksum(hrp, data, enc)) {
+ return null;
+ }
+ return { hrp: hrp, data: data.slice(0, data.length - 6) };
+} \ No newline at end of file
diff --git a/packages/taler-util/src/bitcoin.ts b/packages/taler-util/src/bitcoin.ts
index dd90f514e..f4d3cfeb9 100644
--- a/packages/taler-util/src/bitcoin.ts
+++ b/packages/taler-util/src/bitcoin.ts
@@ -17,7 +17,7 @@
import { AmountJson } from "."
import { Amounts, } from "./amounts"
import { getRandomBytes, decodeCrock, encodeCrock } from "./talerCrypto"
-import { encode as segwitEncode } from "bech32-buffer"
+import * as segwit from "./segwit_addr"
/**
*
* @author sebasjm
@@ -34,7 +34,7 @@ function buf2hex(buffer: Uint8Array) { // buffer is an ArrayBuffer
.join('');
}
-export function generateSegwitAddress(reservePub: string): SegwitAddrs {
+export function generateFakeSegwitAddress(reservePub: string, addr: string): SegwitAddrs {
const pub = decodeCrock(reservePub)
const first_rnd = getRandomBytes(4)
@@ -49,11 +49,16 @@ export function generateSegwitAddress(reservePub: string): SegwitAddrs {
first_part.set(pub.subarray(0, 16), 4)
const second_part = new Uint8Array(first_rnd.length + pub.length / 2)
second_part.set(first_rnd, 0)
- second_part.set(pub.subarray(16, 32), 4)
+ second_part.set(pub.subarray(16), 4)
+
+ console.log(first_part.length, second_part.length)
+
+ const prefix = (addr[0] === 't' && addr[1] == 'b') ? "tb" : (addr[0] === 'b' && addr[1] == 'c' && addr[2] === 'r' && addr[3] == 't') ? 'bcrt' : (addr[0] === 'b' && addr[1] == 'c') ? 'bc' : undefined
+ if (prefix === undefined) throw new Error('unknown bitcoin net')
return {
- segwitAddr1: segwitEncode("bc", first_part),
- segwitAddr2: segwitEncode("bc", second_part),
+ segwitAddr1: segwit.default.encode(prefix, 0, first_part),
+ segwitAddr2: segwit.default.encode(prefix, 0, second_part),
}
}
diff --git a/packages/taler-util/src/segwit_addr.ts b/packages/taler-util/src/segwit_addr.ts
new file mode 100644
index 000000000..becc5d197
--- /dev/null
+++ b/packages/taler-util/src/segwit_addr.ts
@@ -0,0 +1,91 @@
+// Copyright (c) 2017, 2021 Pieter Wuille
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import bech32 from "./bech32.js"
+
+export default {
+ encode: encode,
+ decode: decode
+};
+
+function convertbits(data: any, frombits: number, tobits: number, pad: boolean): any[] {
+ var acc = 0;
+ var bits = 0;
+ var ret = [];
+ var maxv = (1 << tobits) - 1;
+ for (var p = 0; p < data.length; ++p) {
+ var value = data[p];
+ if (value < 0 || (value >> frombits) !== 0) {
+ return []; //check this, was returning null
+ }
+ acc = (acc << frombits) | value;
+ bits += frombits;
+ while (bits >= tobits) {
+ bits -= tobits;
+ ret.push((acc >> bits) & maxv);
+ }
+ }
+ if (pad) {
+ if (bits > 0) {
+ ret.push((acc << (tobits - bits)) & maxv);
+ }
+ } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
+ return []; //check this, was returning null
+ }
+ return ret;
+}
+
+function decode(hrp: any, addr: string) {
+ var bech32m = false;
+ var dec = bech32.decode(addr, bech32.encodings.BECH32);
+ if (dec === null) {
+ dec = bech32.decode(addr, bech32.encodings.BECH32M);
+ bech32m = true;
+ }
+ if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) {
+ return null;
+ }
+ var res = convertbits(dec.data.slice(1), 5, 8, false);
+ if (res === null || res.length < 2 || res.length > 40) {
+ return null;
+ }
+ if (dec.data[0] === 0 && res.length !== 20 && res.length !== 32) {
+ return null;
+ }
+ if (dec.data[0] === 0 && bech32m) {
+ return null;
+ }
+ if (dec.data[0] !== 0 && !bech32m) {
+ return null;
+ }
+ return { version: dec.data[0], program: res };
+}
+
+function encode(hrp: any, version: number, program: any): string {
+ var enc = bech32.encodings.BECH32;
+ if (version > 0) {
+ enc = bech32.encodings.BECH32M;
+ }
+ var ret = bech32.encode(hrp, [version].concat(convertbits(program, 8, 5, true)), enc);
+ if (decode(hrp, ret/*, enc*/) === null) {
+ return ""; //check this was returning null
+ }
+ return ret;
+} \ No newline at end of file