Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/nostr #287

Draft
wants to merge 3 commits into
base: is-sprint-27
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repository": "github:terra-money/station",
"license": "MIT",
"scripts": {
"start": "react-scripts start",
"start": "react-app-rewired start",
"build-scripts": "webpack --config scripts/webpack.config.js",
"build": "cross-env react-app-rewired build && npm run build-scripts",
"test": "react-scripts test",
Expand Down
2 changes: 1 addition & 1 deletion public/firefox.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"options_ui": {
"page": "index.html"
},
"web_accessible_resources": ["inpage.js", "keplr.js", "index.html"],
"web_accessible_resources": ["nostrProvider.js", "inpage.js", "keplr.js", "index.html"],
"icons": {
"128": "icon-128.png",
"180": "icon-180.png"
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"web_accessible_resources": [
{
"resources": ["inpage.js", "keplr.js", "index.html"],
"resources": ["nostrProvider.js", "inpage.js", "keplr.js", "index.html"],
"matches": ["<all_urls>"]
}
],
Expand Down
9 changes: 8 additions & 1 deletion scripts/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ function injectScript() {
container.insertBefore(scriptTag, container.children[0])
container.removeChild(scriptTag)

// inject the script that will provide window.nostr
const script = document.createElement('script')
script.setAttribute('async', 'false')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', chrome.runtime.getURL('nostrProvider.js'))
document.head.appendChild(script)

browser.storage.local.get(["replaceKeplr"]).then(({ replaceKeplr }) => {
if (replaceKeplr) {
const keplrScriptTag = document.createElement("script")
Expand Down Expand Up @@ -475,4 +482,4 @@ function openPopup() {

function closePopup() {
browser.runtime.sendMessage("CLOSE_POPUP")
}
}
44 changes: 44 additions & 0 deletions scripts/nostrProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
window.nostr = {
_requests: {},
_pubkey: null,

async getPublicKey() {
if (this._pubkey) return this._pubkey
this._pubkey = await this._call('getPublicKey', {})
return this._pubkey
},

async signEvent(event) {
return await this._call('signEvent', {event})
},

async getRelays() {
return this._call('getRelays', {})
},

nip04: {
async encrypt(peer, plaintext) {
return window.nostr._call('nip04.encrypt', {peer, plaintext})
},

async decrypt(peer, ciphertext) {
return window.nostr._call('nip04.decrypt', {peer, ciphertext})
}
},

_call(type, params) {
return new Promise((resolve, reject) => {
let id = Math.random().toString().slice(4)
this._requests[id] = {resolve, reject}
window.postMessage(
{
id,
ext: 'Station',
type,
params
},
'*'
)
})
}
}
1 change: 1 addition & 0 deletions scripts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
entry: {
contentScript: path.join(__dirname, "contentScript.js"),
background: path.join(__dirname, "background.js"),
nostrProvider: path.join(__dirname, "nostrProvider.js"),
inpage: path.join(__dirname, "inpage.js"),
keplr: path.join(__dirname, "keplr.js"),
},
Expand Down
8 changes: 8 additions & 0 deletions src/app/sections/CoinTypeMnemonicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ const CoinTypeMnemonicForm = ({ close }: { close: () => void }) => {
coinType: 60,
index,
})
const key1237 = new SeedKey({
seed,
coinType: 1237,
index,
})

const isLegacy =
wordsFromAddress(key118.accAddress("terra")) === wallet.words["330"]
Expand All @@ -81,6 +86,7 @@ const CoinTypeMnemonicForm = ({ close }: { close: () => void }) => {
),
"118": wordsFromAddress(key118.accAddress("terra")),
"60": wordsFromAddress(key60.accAddress("terra")),
"1237": wordsFromAddress(key1237.accAddress("npub")),
},
pubkey: {
// @ts-expect-error
Expand All @@ -89,6 +95,8 @@ const CoinTypeMnemonicForm = ({ close }: { close: () => void }) => {
"118": key118.publicKey.key,
// @ts-expect-error
"60": key60.publicKey.key,
// @ts-expect-error
"1237": key1237.publicKey.key,
},
seed,
password,
Expand Down
6 changes: 4 additions & 2 deletions src/auth/auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Bip = 118 | 330
type Bip = 118 | 330 | 1237

type LocalWallet = SingleWallet | LegacySingleWallet | MultisigWallet // wallet with name

Expand All @@ -22,11 +22,13 @@ interface SingleWallet {
"330": string
"118"?: string
"60"?: string
"1237"?: string
}
pubkey?: {
"330": string
"330"?: string
"118"?: string
"60"?: string
"1237"?: string
}
name: string
lock?: boolean
Expand Down
4 changes: 4 additions & 0 deletions src/auth/modules/create/CreateWalletWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ const CreateWalletWizard = ({ defaultMnemonic = "", beforeCreate }: Props) => {
const key330 = new SeedKey({ seed, coinType, index })
const key118 = new SeedKey({ seed, coinType: 118, index })
const key60 = new SeedKey({ seed, coinType: 60, index })
const key1237 = new SeedKey({ seed, coinType: 1237, index })
const words = {
"330": wordsFromAddress(key330.accAddress("terra")),
"118": wordsFromAddress(key118.accAddress("terra")),
"60": wordsFromAddress(key60.accAddress("inj")),
"1237": wordsFromAddress(key1237.accAddress("npub")),
}

const pubkey = {
Expand All @@ -72,6 +74,8 @@ const CreateWalletWizard = ({ defaultMnemonic = "", beforeCreate }: Props) => {
"118": key118.publicKey.key,
// @ts-expect-error
"60": key60.publicKey.key,
// @ts-expect-error
"1237": key1237.publicKey.key,
}

addWallet({
Expand Down
8 changes: 4 additions & 4 deletions src/auth/scripts/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ export const testPassword = (params: Params) => {

type AddWalletParams =
| {
words: { "330": string; "118"?: string; "60"?: string }
words: { "330": string; "118"?: string; "60"?: string; "1237"?: string }
password: string
seed: Buffer
name: string
index: number
legacy: boolean
pubkey: { "330": string; "118"?: string; "60"?: string }
pubkey: { "330": string; "118"?: string; "60"?: string; "1237"?: string }
}
| {
words: { "330": string; "118"?: string }
words: { "330": string; "118"?: string; "1237"?: string }
password: string
key: { "330": Buffer }
name: string
pubkey?: { "330": string; "118"?: string }
pubkey?: { "330": string; "118"?: string; "1237"?: string }
}
| LedgerWallet
| MultisigWallet
Expand Down
2 changes: 1 addition & 1 deletion src/extension/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const storeWalletAddress = (wallet: {
addresses: Record<ChainID, AccAddress>
name?: string
ledger?: boolean
pubkey?: { "330": string; "118"?: string }
pubkey?: { "330"?: string; "118"?: string; "1237"?: string }
network: string
}) => {
browser.storage?.local.set({
Expand Down