2024-05-15 23:46:54 +02:00
|
|
|
import BIP47Factory from '@spsina/bip47';
|
2024-05-20 11:54:13 +02:00
|
|
|
|
2024-05-30 15:54:29 +02:00
|
|
|
import { SilentPayment } from 'silent-payments';
|
|
|
|
|
2024-05-15 23:46:54 +02:00
|
|
|
import ecc from '../blue_modules/noble_ecc';
|
2024-06-15 21:11:02 +02:00
|
|
|
import * as bitcoin from 'bitcoinjs-lib';
|
2024-05-15 23:46:54 +02:00
|
|
|
|
|
|
|
export class ContactList {
|
2024-05-30 15:54:29 +02:00
|
|
|
isBip47PaymentCodeValid(pc: string) {
|
2024-05-15 23:46:54 +02:00
|
|
|
try {
|
|
|
|
BIP47Factory(ecc).fromPaymentCode(pc);
|
|
|
|
return true;
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 15:54:29 +02:00
|
|
|
|
|
|
|
isBip352PaymentCodeValid(pc: string) {
|
|
|
|
return SilentPayment.isPaymentCodeValid(pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPaymentCodeValid(pc: string): boolean {
|
|
|
|
return this.isBip47PaymentCodeValid(pc) || this.isBip352PaymentCodeValid(pc);
|
|
|
|
}
|
2024-06-15 21:11:02 +02:00
|
|
|
|
|
|
|
isAddressValid(address: string): boolean {
|
|
|
|
try {
|
|
|
|
bitcoin.address.toOutputScript(address); // throws, no?
|
|
|
|
|
|
|
|
if (!address.toLowerCase().startsWith('bc1')) return true;
|
|
|
|
const decoded = bitcoin.address.fromBech32(address);
|
|
|
|
if (decoded.version === 0) return true;
|
|
|
|
if (decoded.version === 1 && decoded.data.length !== 32) return false;
|
|
|
|
if (decoded.version === 1 && !ecc.isPoint(Buffer.concat([Buffer.from([2]), decoded.data]))) return false;
|
|
|
|
if (decoded.version > 1) return false;
|
|
|
|
// ^^^ some day, when versions above 1 will be actually utilized, we would need to unhardcode this
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 23:46:54 +02:00
|
|
|
}
|