Skip to content

Commit 0277274

Browse files
committed
Connect to Spark network and get info
1 parent e3ca81b commit 0277274

File tree

9 files changed

+54
-6
lines changed

9 files changed

+54
-6
lines changed

.env.development

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ TWITTER_POSTER_API_KEY_SECRET=
3838
TWITTER_POSTER_ACCESS_TOKEN=
3939
TWITTER_POSTER_ACCESS_TOKEN_SECRET=
4040

41+
# Breez SDK
42+
NEXT_PUBLIC_BREEZ_SDK_API_KEY=
43+
4144
########################################
4245
# SNDEV STUFF WE PRESET #
4346
# which you can override in .env.local #

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"async-retry": "^1.3.3",
3737
"aws-sdk": "^2.1691.0",
3838
"bech32": "^2.0.0",
39+
"bip39": "^3.1.0",
3940
"bolt11": "^1.4.1",
4041
"bootstrap": "^5.3.3",
4142
"canonical-json": "0.0.4",

wallets/client/components/form/hooks.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export function useProtocolForm (protocol) {
9494
// after init, we use formState as the source of truth everywhere
9595
let value = formState?.config?.[field.name] ?? protocol.config?.[field.name]
9696

97+
if (!value && field.initial) {
98+
value = typeof field.initial === 'function' ? field.initial() : field.initial
99+
}
100+
97101
if (!value && field.share) {
98102
value = complementaryFormState?.config?.[field.name]
99103
}

wallets/client/components/form/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function WalletProtocolFormField ({ type, ...props }) {
189189
const [protocol] = useProtocol()
190190
const formik = useFormikContext()
191191

192-
function transform ({ validate, encrypt, editable, help, share, ...props }) {
192+
function transform ({ validate, encrypt, editable, help, share, initial, ...props }) {
193193
const [upperHint, bottomHint] = Array.isArray(props.hint) ? props.hint : [null, props.hint]
194194

195195
const parseHelpText = text => Array.isArray(text) ? text.join('\n\n') : text

wallets/client/protocols/spark.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1+
import init, { defaultConfig, connect } from '@breeztech/breez-sdk-spark'
2+
13
export const name = 'SPARK'
24

35
export async function sendPayment (bolt11, { mnemonic }, { signal }) {
46
// TODO: implement
57
}
68

7-
export async function testSendPayment (config, { signal }) {}
9+
export async function testSendPayment ({ mnemonic }, { signal }) {
10+
await init()
11+
12+
const config = defaultConfig('mainnet')
13+
// keeping this API a secret on a "best effort" basis
14+
config.apiKey = process.env.NEXT_PUBLIC_BREEZ_SDK_API_KEY
15+
config.lnurlDomain = 'breez.tips'
16+
17+
const sdk = await connect({
18+
config,
19+
mnemonic,
20+
// the SDK will create a IndexedDB with this name to store data
21+
storageDir: 'breez-sdk-spark'
22+
})
23+
24+
await sdk.getInfo({})
25+
26+
sdk.disconnect()
27+
}

wallets/lib/crypto.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import bip39Words from '@/lib/bip39-words'
2+
import * as bip39 from 'bip39'
23

34
export async function deriveKey (passphrase, salt) {
45
const enc = new TextEncoder()
@@ -77,7 +78,5 @@ export async function decrypt (key, { iv, value }) {
7778
}
7879

7980
export function generateRandomPassphrase () {
80-
const rand = new Uint32Array(12)
81-
window.crypto.getRandomValues(rand)
82-
return Array.from(rand).map(i => bip39Words[i % bip39Words.length]).join(' ')
81+
return bip39.generateMnemonic(128, null, bip39Words)
8382
}

wallets/lib/protocols/spark.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { bip39Validator, externalLightningAddressValidator } from '@/wallets/lib/validate'
2+
import { generateRandomPassphrase } from '@/wallets/lib/crypto'
23

34
// Spark
45
// https://github.com/breez/spark-sdk
56
// https://sdk-doc-spark.breez.technology/
7+
// https://breez.github.io/spark-sdk/breez_sdk_spark/
68

79
export default [
810
{
@@ -16,7 +18,9 @@ export default [
1618
type: 'password',
1719
required: true,
1820
validate: bip39Validator(),
19-
encrypt: true
21+
encrypt: true,
22+
initial: generateRandomPassphrase,
23+
disabled: true
2024
}
2125
],
2226
relationName: 'walletSendSpark'

wallets/lib/validate.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import bip39Words from '@/lib/bip39-words'
2+
import * as bip39 from 'bip39'
23
import { decodeRune } from '@/lib/cln'
34
import { B64_URL_REGEX } from '@/lib/format'
45
import { isInvoicableMacaroon, isInvoiceMacaroon } from '@/lib/macaroon'
@@ -147,6 +148,12 @@ export const bip39Validator = ({ min = 12, max = 24 } = {}) =>
147148
.test({
148149
name: 'bip39',
149150
test: async (value, context) => {
151+
// legacy mnemonics don't include a checksum so if validation with the
152+
// bip39 library fails we assume it's an old mnemonic and use legacy validation
153+
if (bip39.validateMnemonic(value, bip39Words)) {
154+
return true
155+
}
156+
150157
const words = value ? value.trim().split(/[\s]+/) : []
151158
for (const w of words) {
152159
try {

0 commit comments

Comments
 (0)