diff --git a/class/acinqStrikeLightningWallet.js b/class/acinqStrikeLightningWallet.js new file mode 100644 index 000000000..e1191795a --- /dev/null +++ b/class/acinqStrikeLightningWallet.js @@ -0,0 +1,149 @@ +import { LegacyWallet } from './legacy-wallet'; +import Frisbee from 'frisbee'; +import { BitcoinUnit } from '../models/bitcoinUnits'; + +export class ACINQStrikeLightningWallet extends LegacyWallet { + static type = 'acinqStrikeLightningWallet'; + static typeReadable = 'strike'; + + constructor(props) { + super(props); + this.setBaseURI(); // no args to init with default value + this.init(); + this.secret = ''; + this.user_charges_raw = []; + this.preferredBalanceUnit = BitcoinUnit.SATS; + } + + /** + * requires calling init() after setting + * + * @param URI + */ + setBaseURI(URI) { + if (URI) { + this.baseURI = URI; + } else { + this.baseURI = 'https://api.strike.acinq.co/api/v1'; + } + } + + getBaseURI() { + return this.baseURI; + } + + allowSend() { + return false; + } + + timeToRefreshBalance() { + // only manual refresh for now + return false; + } + + timeToRefreshTransaction() { + // only manual refresh for now + return false; + } + + static fromJson(param) { + let obj = super.fromJson(param); + obj.init(); + return obj; + } + + init() { + this._api = new Frisbee({ + baseURI: this.baseURI, + headers: { + 'cache-control': 'no-cache', + 'Content-Type': 'application/json', + }, + }); + } + + setSecret(secret) { + this.secret = secret; + this._api.auth(this.secret); + } + + /** + * Returns list of LND invoices created by user + * + * @return {Promise.} + */ + async getCharge(chargeId) { + let response = await this._api.get('/charges/' + chargeId); + let json = response.body; + if (typeof json === 'undefined') { + throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse)); + } + + if (json && json.error) { + throw new Error('API error: ' + json.message + ' (code ' + json.code + ')'); + } + + this.user_charges_raw = json.sort((a, b) => { + return a.created - b.created; + }); + + return this.user_charges_raw; + } + + async getUserCharges() { + let response = await this._api.get('/charges'); + let json = response.body; + if (typeof json === 'undefined') { + throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse)); + } + + if (json && json.code && json.code === 401) { + throw new Error('API error: ' + json.message + ' (code ' + json.code + ')'); + } + this.user_charges_raw = json.sort((a, b) => { + return a.created - b.created; + }); + return this.user_charges_raw; + } + + /** + * Basically the same as this.getUserInvoices() but saves invoices list + * to internal variable + * + * @returns {Promise} + */ + getTransactions() { + console.warn('heee'); + + return this.user_charges_raw; + } + + async authenticate() { + await this.getUserCharges(); + return true; + } + + async createCharge(amount, description = 'ACINQ strike Charge') { + let response = await this._api.post('/charges', { + body: { amount: Number(amount), description: String(description), currency: 'btc' }, + auth: { + user: this.secret, + pass: '', + }, + }); + let json = response.body; + if (typeof json === 'undefined') { + throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse)); + } + + if (json && json.code) { + throw new Error('API error: ' + json.message + ' (code ' + json.code + ')'); + } + + return json; + } + + allowReceive() { + return true; + } +} diff --git a/class/index.js b/class/index.js index cb9470800..04344d2e4 100644 --- a/class/index.js +++ b/class/index.js @@ -10,3 +10,4 @@ export * from './hd-legacy-p2pkh-wallet'; export * from './watch-only-wallet'; export * from './lightning-custodian-wallet'; export * from './abstract-hd-wallet'; +export * from './acinqStrikeLightningWallet'; diff --git a/package-lock.json b/package-lock.json index 664714a66..a5741ac56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3386,9 +3386,9 @@ "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=" }, "dayjs": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.8.0.tgz", - "integrity": "sha512-2ofInmfMKLLR5R02q3WEUuDt86UK33VQQTaEeJudF+C04ZUaekCP3VpB0NJPiyPDCGJWq9XYhHX2AemdxA8+dg==" + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.8.2.tgz", + "integrity": "sha512-iHNxe5kIbxmPgzJFjks9TFMokOu3TQcUUSagb/Ff7GZNi7ulYF0qaAZ61trZEFOONgrp4jvKVpBJ86qy4UsSoA==" }, "debug": { "version": "4.1.1", diff --git a/package.json b/package.json index 6cf65a2fb..4ad4efba0 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "buffer": "^5.2.1", "buffer-reverse": "^1.0.1", "crypto-js": "^3.1.9-1", - "dayjs": "^1.8.0", + "dayjs": "^1.8.2", "electrum-client": "git+https://github.com/Overtorment/node-electrum-client.git", "electrum-host-parse": "^0.1.1", "eslint-config-prettier": "^4.0.0", diff --git a/screen/send/details.js b/screen/send/details.js index b5a54b44c..b6c02bd69 100644 --- a/screen/send/details.js +++ b/screen/send/details.js @@ -556,9 +556,15 @@ export default class SendDetails extends Component { } else { try { const { address, amount, memo } = this.decodeBitcoinUri(text); - this.setState({ address, amount, memo, isLoading: false, bip70TransactionExpiration: null }); + this.setState({ + address: address.replace('bitcoin:', ''), + amount, + memo, + isLoading: false, + bip70TransactionExpiration: null, + }); } catch (_) { - this.setState({ address: text.trim(), isLoading: false, bip70TransactionExpiration: null }); + this.setState({ address: text.trim().replace('bitcoin:', ''), isLoading: false, bip70TransactionExpiration: null }); } } }}