Skip to content

Commit

Permalink
feat(deps): replace bech32 and old chacha with noble and scure packag…
Browse files Browse the repository at this point in the history
…es (#294)
  • Loading branch information
paulmillr authored Sep 23, 2023
1 parent de2194c commit 853c799
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 92 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/preset-typescript": "7.22.15",
"@ethersproject/address": "5.7.0",
"@greymass/eosio": "^0.7.0",
"@jest/globals": "^29.7.0",
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
"@tonomy/antelope-did": "^0.1.5",
Expand All @@ -69,6 +70,7 @@
"eslint-plugin-jest": "27.2.3",
"eslint-plugin-prettier": "5.0.0",
"jest": "29.6.4",
"jest-config": "^29.7.0",
"jsontokens": "4.0.1",
"jsonwebtoken": "9.0.2",
"jwk-to-pem": "2.0.5",
Expand All @@ -86,10 +88,10 @@
"webpack-cli": "5.1.4"
},
"dependencies": {
"@noble/ciphers": "^0.3.0",
"@noble/curves": "^1.0.0",
"@noble/hashes": "^1.3.0",
"@stablelib/xchacha20poly1305": "^1.0.1",
"bech32": "^2.0.0",
"@scure/base": "^1.1.3",
"canonicalize": "^2.0.0",
"did-resolver": "^4.1.0",
"multiformats": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/blockchains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { secp256k1 } from '@noble/curves/secp256k1'
import { bech32 } from 'bech32'
import { bech32 } from '@scure/base'
import { sha256, ripemd160 } from '../Digest.js'

export function publicKeyToAddress(publicKey: string, prefix: string): string {
Expand Down
20 changes: 11 additions & 9 deletions src/encryption/xc20pDir.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Decrypter, Encrypter, EncryptionResult, ProtectedHeader } from './types.js'
import { bytesToBase64url, encodeBase64url } from '../util.js'
import { fromString } from 'uint8arrays/from-string'
import { XChaCha20Poly1305 } from '@stablelib/xchacha20poly1305'
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
import { randomBytes } from '@noble/hashes/utils'

export function xc20pEncrypter(key: Uint8Array): (cleartext: Uint8Array, aad?: Uint8Array) => EncryptionResult {
const cipher = new XChaCha20Poly1305(key)
return (cleartext: Uint8Array, aad?: Uint8Array) => {
const iv = randomBytes(cipher.nonceLength)
const sealed = cipher.seal(iv, cleartext, aad)
const iv = randomBytes(24)
const cipher = xchacha20poly1305(key, iv, aad)
const sealed = cipher.encrypt(cleartext)
return {
ciphertext: sealed.subarray(0, sealed.length - cipher.tagLength),
tag: sealed.subarray(sealed.length - cipher.tagLength),
ciphertext: sealed.subarray(0, sealed.length - 16),
tag: sealed.subarray(sealed.length - 16),
iv,
}
}
Expand Down Expand Up @@ -39,10 +39,12 @@ export function xc20pDirEncrypter(key: Uint8Array): Encrypter {
}

export function xc20pDirDecrypter(key: Uint8Array): Decrypter {
const cipher = new XChaCha20Poly1305(key)

async function decrypt(sealed: Uint8Array, iv: Uint8Array, aad?: Uint8Array): Promise<Uint8Array | null> {
return cipher.open(iv, sealed, aad)
try {
return xchacha20poly1305(key, iv, aad).decrypt(sealed)
} catch (error) {
return null
}
}

return { alg: 'dir', enc: 'XC20P', decrypt }
Expand Down
Loading

0 comments on commit 853c799

Please sign in to comment.