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

Update keystore-idb #286

Merged
merged 3 commits into from
Sep 14, 2021
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Added the `fs.addPublicExchangeKey()` function which adds the public exchange key of that domain/browser/device to your filesystem at `/public/.well-known/exchange/DID_KEY`. Along with the `fs.hasPublicExchangeKey()` to check if it's there.
- Made the login low more resilient. Should work better with extensions triggering `postMessage`s now.
- Updated keystore-idb to v0.15.0, which renamed `publicReadKey()` to `publicExchangeKey()` (among other functions). The read key-pair is now properly named the exchange key-pair.



### v0.27.0
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webnative",
"version": "0.27.1",
"version": "0.28.0",
"description": "Fission Webnative SDK",
"keywords": [
"WebCrypto",
Expand Down Expand Up @@ -64,7 +64,7 @@
"publish-latest": "npm publish --tag latest"
},
"devDependencies": {
"@ipld/car": "^3.1.8",
"@ipld/car": "^3.1.16",
"@types/expect": "^24.3.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.4.1",
Expand Down Expand Up @@ -96,8 +96,9 @@
"ipfs-message-port-client": "https://ipfs.runfission.com/ipfs/bafybeigx6q4aezve7my76s5vvfuiinbxtepapqvmjf2jbgrozrut6cjape/p/ipfs-message-port-client.tar.gz",
"ipfs-message-port-protocol": "https://ipfs.runfission.com/ipfs/bafybeigx6q4aezve7my76s5vvfuiinbxtepapqvmjf2jbgrozrut6cjape/p/ipfs-message-port-protocol.tar.gz",
"ipld-dag-pb": "^0.22.2",
"keystore-idb": "0.14.2",
"keystore-idb": "0.15.0",
"localforage": "^1.9.0",
"multiformats": "^9.0.0",
"noble-ed25519": "^1.2.4",
"throttle-debounce": "^3.0.1",
"uint8arrays": "^2.1.7"
Expand Down
2 changes: 1 addition & 1 deletion src/common/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.27.1"
export const VERSION = "0.28.0"
6 changes: 3 additions & 3 deletions src/crypto/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export const ed25519Verify = (message: Uint8Array, signature: Uint8Array, public
return ed25519.verify(signature, message, publicKey)
}

export const ksPublicReadKey = async (): Promise<string> => {
assertBrowser("keystore.publicReadKey")
export const ksPublicExchangeKey = async (): Promise<string> => {
assertBrowser("keystore.publicExchangeKey")
const ks = await keystore.get()
return ks.publicReadKey()
return ks.publicExchangeKey()
}

export const ksPublicWriteKey = async (): Promise<string> => {
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export const hash = {
sha256Str: sha256Str
}
export const aes = {
encrypt: (bytes: Uint8Array, key: string): Promise<Uint8Array> =>
encrypt: (bytes: Uint8Array, key: string): Promise<Uint8Array> =>
impl.aes.encrypt(bytes, key),
decrypt: (bytes: Uint8Array, key: string): Promise<Uint8Array> =>
decrypt: (bytes: Uint8Array, key: string): Promise<Uint8Array> =>
impl.aes.decrypt(bytes, key),
genKeyStr: (): Promise<string> =>
impl.aes.genKeyStr(),
Expand All @@ -35,8 +35,8 @@ export const ed25519 = {
}

export const keystore = {
publicReadKey: (): Promise<string> =>
impl.keystore.publicReadKey(),
publicExchangeKey: (): Promise<string> =>
impl.keystore.publicExchangeKey(),
publicWriteKey: (): Promise<string> =>
impl.keystore.publicWriteKey(),
decrypt: (encrypted: string): Promise<string> =>
Expand Down
2 changes: 1 addition & 1 deletion src/did/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { toKeyType } from "./util.js"
* Create a DID based on the exchange key-pair.
*/
export async function exchange(): Promise<string> {
const pubKeyB64 = await crypto.keystore.publicReadKey()
const pubKeyB64 = await crypto.keystore.publicExchangeKey()
const ksAlg = await crypto.keystore.getAlg()

return publicKeyToDid(
Expand Down
8 changes: 4 additions & 4 deletions src/setup/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as browserCrypto from "../crypto/browser.js"
import * as browserStorage from "../storage/browser.js"

export const DEFAULT_IMPLEMENTATION: Dependencies = {
export const DEFAULT_IMPLEMENTATION: Dependencies = {
hash: {
sha256: browserCrypto.sha256
},
Expand All @@ -18,7 +18,7 @@ export const DEFAULT_IMPLEMENTATION: Dependencies = {
verify: browserCrypto.ed25519Verify
},
keystore: {
publicReadKey: browserCrypto.ksPublicReadKey,
publicExchangeKey: browserCrypto.ksPublicExchangeKey,
publicWriteKey: browserCrypto.ksPublicWriteKey,
decrypt: browserCrypto.ksDecrypt,
sign: browserCrypto.ksSign,
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface Dependencies {
encrypt: (bytes: Uint8Array, key: string) => Promise<Uint8Array>
decrypt: (bytes: Uint8Array, key: string) => Promise<Uint8Array>
genKeyStr: () => Promise<string>
decryptGCM: (encrypted: string, keyStr: string, ivStr: string) => Promise<string>
decryptGCM: (encrypted: string, keyStr: string, ivStr: string) => Promise<string>
}
rsa: {
verify: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) => Promise<boolean>
Expand All @@ -74,7 +74,7 @@ export interface Dependencies {
verify: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) => Promise<boolean>
}
keystore: {
publicReadKey: () => Promise<string>
publicExchangeKey: () => Promise<string>
publicWriteKey: () => Promise<string>
decrypt: (encrypted: string) => Promise<string>
sign: (message: string, charSize: number) => Promise<string>
Expand Down
28 changes: 14 additions & 14 deletions src/setup/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ const ed25519Verify = (message: Uint8Array, signature: Uint8Array, publicKey: Ui
class InMemoryRSAKeyStore implements KeyStore {

cfg: Config
readKeyPair: CryptoKeyPair
exchangeKeyPair: CryptoKeyPair
writeKeyPair: CryptoKeyPair
inMemoryStore: Record<string, CryptoKey>

constructor(cfg: Config, readKeyPair: CryptoKeyPair, writeKeyPair: CryptoKeyPair) {
constructor(cfg: Config, exchangeKeyPair: CryptoKeyPair, writeKeyPair: CryptoKeyPair) {
this.cfg = cfg
this.inMemoryStore = {}
this.readKeyPair = readKeyPair
this.exchangeKeyPair = exchangeKeyPair
this.writeKeyPair = writeKeyPair
}

Expand All @@ -108,14 +108,14 @@ class InMemoryRSAKeyStore implements KeyStore {

const { rsaSize, hashAlg } = cfg

const readKeyPair = await rsa.makeKeypair(rsaSize, hashAlg, KeyUse.Read)
const exchangeKeyPair = await rsa.makeKeypair(rsaSize, hashAlg, KeyUse.Exchange)
const writeKeyPair = await rsa.makeKeypair(rsaSize, hashAlg, KeyUse.Write)

return new InMemoryRSAKeyStore(cfg, readKeyPair, writeKeyPair)
return new InMemoryRSAKeyStore(cfg, exchangeKeyPair, writeKeyPair)
}

async readKey() {
return this.readKeyPair
async exchangeKey() {
return this.exchangeKeyPair
}

async writeKey() {
Expand Down Expand Up @@ -229,21 +229,21 @@ class InMemoryRSAKeyStore implements KeyStore {
publicKey?: string | PublicKey, // unused param so that keystore interfaces match
cfg?: Partial<Config>
): Promise<string> {
const readKey = await this.readKey()
const exchangeKey = await this.exchangeKey()
const mergedCfg = config.merge(this.cfg, cfg)

return utils.arrBufToStr(
await rsa.decrypt(
cipherText,
readKey.privateKey,
exchangeKey.privateKey,
),
mergedCfg.charSize
)
}

async publicReadKey(): Promise<string> {
const readKey = await this.readKey()
return rsa.getPublicKey(readKey)
async publicExchangeKey(): Promise<string> {
const exchangeKey = await this.exchangeKey()
return rsa.getPublicKey(exchangeKey)
}

async publicWriteKey(): Promise<string> {
Expand Down Expand Up @@ -287,9 +287,9 @@ export const NODE_IMPLEMENTATION = {
verify: ed25519Verify
},
keystore: {
async publicReadKey(): Promise<string> {
async publicExchangeKey(): Promise<string> {
const ks = await getKeystore()
return ks.publicReadKey()
return ks.publicExchangeKey()
},
async publicWriteKey(): Promise<string> {
const ks = await getKeystore()
Expand Down
46 changes: 30 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@
"@hapi/bourne" "2.x.x"
"@hapi/hoek" "9.x.x"

"@ipld/car@^3.1.8":
version "3.1.8"
resolved "https://registry.yarnpkg.com/@ipld/car/-/car-3.1.8.tgz#e6e4154b888426f3f251681ca3fb56a863ce677a"
integrity sha512-6GbVnsgjlhia3glezx1Rbc4FPU9ZKS94GQwjab1J3auxIYXHrxQ3SV1WUUCjX5lO9qGCB10KiyiKEIY2Mj8/jA==
"@ipld/car@^3.1.16":
version "3.1.16"
resolved "https://registry.yarnpkg.com/@ipld/car/-/car-3.1.16.tgz#8b0acfef0e9a74fa285731486ab2d3a1b7c12236"
integrity sha512-LvIXl7BYmy8em1PFB1l2Iu3/bFHYHy3h3x7HcDi9veoGSYi70dAIthJsXgkoYoGfRSLONR1FDjaoFjbbXk87Qw==
dependencies:
"@ipld/dag-cbor" "^6.0.0"
"@types/varint" "^6.0.0"
Expand Down Expand Up @@ -794,7 +794,7 @@
"@typescript-eslint/types" "4.29.3"
eslint-visitor-keys "^2.0.0"

"@ungap/global-this@^0.4.3":
"@ungap/global-this@^0.4.4":
version "0.4.4"
resolved "https://registry.yarnpkg.com/@ungap/global-this/-/global-this-0.4.4.tgz#8a1b2cfcd3e26e079a847daba879308c924dd695"
integrity sha512-mHkm6FvepJECMNthFuIgpAEFmPOk71UyXuIxYfjytvFTnSDBIz7jmViO+LfHI/AjrazWije0PnSP3+/NlwzqtA==
Expand Down Expand Up @@ -3541,14 +3541,14 @@ keypair@^1.0.1:
resolved "https://registry.yarnpkg.com/keypair/-/keypair-1.0.3.tgz#4314109d94052a0acfd6b885695026ad29529c80"
integrity sha512-0wjZ2z/SfZZq01+3/8jYLd8aEShSa+aat1zyPGQY3IuKoEAp6DJGvu2zt6snELrQU9jbCkIlCyNOD7RdQbHhkQ==

keystore-idb@0.14.2:
version "0.14.2"
resolved "https://registry.yarnpkg.com/keystore-idb/-/keystore-idb-0.14.2.tgz#409a488acf900123ee1c6758a19dd2efde5fdf78"
integrity sha512-1jjL3Tjn4NSjczl/KBBnK1JefmouMSHC18Gv+2Eo27PiBSNrfa9ipGZkpW4OsV/tdYC50JA1IQ2yZRpeiYmH+w==
keystore-idb@0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/keystore-idb/-/keystore-idb-0.15.0.tgz#87897f2bafa9ebe42d52970d59034e22e8652df3"
integrity sha512-CSUCwdSR/gv/ydyv0m4DiTVdtHw0WPKmiw1frMh0jI3Ri9eGwtLBnYefppb2zgC6xLk4aBrPpojpjrRCEaL0/A==
dependencies:
"@ungap/global-this" "^0.4.3"
buffer "^6.0.3"
localforage "^1.7.3"
"@ungap/global-this" "^0.4.4"
localforage "^1.10.0"
uint8arrays "^3.0.0"

level-codec@^10.0.0:
version "10.0.0"
Expand Down Expand Up @@ -3966,7 +3966,14 @@ lie@3.1.1:
dependencies:
immediate "~3.0.5"

localforage@^1.7.3, localforage@^1.9.0:
localforage@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4"
integrity sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==
dependencies:
lie "3.1.1"

localforage@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1"
integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g==
Expand Down Expand Up @@ -5653,9 +5660,16 @@ uint8arrays@^2.0.5, uint8arrays@^2.1.2, uint8arrays@^2.1.3, uint8arrays@^2.1.4,
multibase "^4.0.1"

uint8arrays@^2.1.7:
version "2.1.7"
resolved "https://registry.yarnpkg.com/uint8arrays/-/uint8arrays-2.1.7.tgz#16719b54b7b17be66eb9e44698a45f8371750b84"
integrity sha512-k+yuEWEHQG/TuRaxL+JVEe8IBqyU5dhDkw+CISCDccOcW90dIju0A6i0Iwav0MK7kg73FZpowqOByS5e/B6GYA==
version "2.1.10"
resolved "https://registry.yarnpkg.com/uint8arrays/-/uint8arrays-2.1.10.tgz#34d023c843a327c676e48576295ca373c56e286a"
integrity sha512-Q9/hhJa2836nQfEJSZTmr+pg9+cDJS9XEAp7N2Vg5MzL3bK/mkMVfjscRGYruP9jNda6MAdf4QD/y78gSzkp6A==
dependencies:
multiformats "^9.4.2"

uint8arrays@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/uint8arrays/-/uint8arrays-3.0.0.tgz#260869efb8422418b6f04e3fac73a3908175c63b"
integrity sha512-HRCx0q6O9Bfbp+HHSfQQKD7wU70+lydKVt4EghkdOvlK/NlrF90z+eXV34mUd48rNvVJXwkrMSPpCATkct8fJA==
dependencies:
multiformats "^9.4.2"

Expand Down