Skip to content

Commit

Permalink
chore: add types for the keychain api (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 authored Apr 24, 2024
1 parent 5151293 commit 701fbeb
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions features/keychain/api/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const solanaKeyId = new KeyIdentifier({
// genFixtures()

describe('keychain api', () => {
/** @type {import('../').KeychainApi} */
let api

beforeEach(() => {
Expand Down
60 changes: 60 additions & 0 deletions features/keychain/api/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import KeyIdentifier from '@exodus/key-identifier'
import BN from 'bn.js'

type SeedId = string
type KeySource = { seedId: SeedId; keyId: KeyIdentifier }

type EcOptions = {
canonical?: boolean
}

type EcSignature = {
r: BN
s: BN
recoveryParam?: number
}

type PublicKeys = {
publicKey: Buffer
xpub: string
}

type PrivateKeys = {
privateKey: Buffer
xpriv: string
}

export interface KeychainApi {
exportKey(params: KeySource): Promise<PublicKeys>
exportKey(params: { exportPrivate: false } & KeySource): Promise<PublicKeys>
exportKey(params: { exportPrivate: true } & KeySource): Promise<PublicKeys & PrivateKeys>
arePrivateKeysLocked(): Promise<boolean>
sodium: {
signDetached(params: { data: Buffer } & KeySource): Promise<Buffer>
getKeysFromSeed(
params: KeySource
): Promise<{ box: { publicKey: Buffer }; sign: { publicKey: Buffer } }>
}
ed25519: {
signBuffer(params: { data: Buffer } & KeySource): Promise<Buffer>
}
secp256k1: {
signBuffer(params: { data: Buffer; ecOptions?: EcOptions } & KeySource): Promise<Buffer>
signBuffer(
params: { data: Buffer; ecOptions?: EcOptions; enc: 'der' } & KeySource
): Promise<Buffer>
signBuffer(
params: { data: Buffer; ecOptions?: EcOptions; enc: 'raw' } & KeySource
): Promise<EcSignature>
}
}

declare const keychainApiDefinition: {
id: 'keychainApi'
type: 'api'
factory(): {
keychain: KeychainApi
}
}

export default keychainApiDefinition
8 changes: 8 additions & 0 deletions features/keychain/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import keychainApiDefinition from './api'

declare const keychain: () => {
id: 'keychain'
definitions: [{ definition: typeof keychainApiDefinition }]
}

export default keychain
2 changes: 2 additions & 0 deletions features/keychain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"files": [
"index.js",
"index.d.ts",
"module",
"api",
"README.md",
Expand Down Expand Up @@ -44,6 +45,7 @@
"devDependencies": {
"@exodus/key-ids": "^1.0.0",
"bip39": "2.6.0",
"bn.js": "^5.2.1",
"eslint": "^8.44.0",
"events": "^3.3.0",
"jest": "^29.1.2"
Expand Down
4 changes: 4 additions & 0 deletions features/keychain/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["**/*.d.ts"],
}
1 change: 1 addition & 0 deletions libraries/key-identifier/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './key-identifier'
20 changes: 20 additions & 0 deletions libraries/key-identifier/src/key-identifier.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type ConstructorParams = {
derivationAlgorithm: 'BIP32' | 'SLIP10'
derivationPath: string
assetName?: string
keyType: 'legacy' | 'nacl' | 'secp2561'
}

type KeyIdentifierLike = Partial<ConstructorParams>

export default class KeyIdentifier {
derivationAlgorithm: ConstructorParams['derivationAlgorithm']
derivationPath: ConstructorParams['derivationPath']
assetName: ConstructorParams['assetName']
keyType: ConstructorParams['keyType']

constructor(params: ConstructorParams)

static validate(potentialKeyIdentifier: KeyIdentifierLike): boolean
static compare(a: KeyIdentifierLike, b: KeyIdentifierLike): boolean
}
4 changes: 4 additions & 0 deletions libraries/key-identifier/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["**/*.d.ts"],
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,7 @@ __metadata:
"@exodus/slip10": ^1.0.0
"@exodus/sodium-crypto": ^3.1.0
bip39: 2.6.0
bn.js: ^5.2.1
buffer-json: ^2.0.0
create-hash: ^1.2.0
eslint: ^8.44.0
Expand Down Expand Up @@ -4377,6 +4378,13 @@ __metadata:
languageName: node
linkType: hard

"bn.js@npm:^5.2.1":
version: 5.2.1
resolution: "bn.js@npm:5.2.1"
checksum: 3dd8c8d38055fedfa95c1d5fc3c99f8dd547b36287b37768db0abab3c239711f88ff58d18d155dd8ad902b0b0cee973747b7ae20ea12a09473272b0201c9edd3
languageName: node
linkType: hard

"brace-expansion@npm:^1.0.0, brace-expansion@npm:^1.1.7":
version: 1.1.11
resolution: "brace-expansion@npm:1.1.11"
Expand Down

0 comments on commit 701fbeb

Please sign in to comment.