Skip to content

Fix: Ensure File Argument 'key' is Provided in kubo-rpc-client #297

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions src/key/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import type { KeyAPI } from './index.js'
import type { HTTPRPCClient } from '../lib/core.js'

export function createImport (client: HTTPRPCClient): KeyAPI['import'] {
return async function importKey (name, pem, password, options = {}) {
return async function importKey (name, file, ipnsBase, format, options = {}) {
const body = new FormData()
body.append('key', file)

const res = await client.post('key/import', {
signal: options.signal,
searchParams: toUrlSearchParams({
arg: name,
pem,
password,
format,
'ipns-base': ipnsBase,
...options
}),
headers: options.headers
body,
headers: {
...options.headers,
'Content-Type': 'multipart/form-data'
}
})
const data = await res.json()

Expand Down
11 changes: 8 additions & 3 deletions src/key/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,23 @@ export interface KeyAPI {
rename(oldName: string, newName: string, options?: HTTPRPCOptions): Promise<KeyRenameResult>

/**
* Remove a key
* import a key
*
* @param name - The name of the key
* @param file - The file to import
* @param ipnsBase - The base of the IPNS name default is base36, Takes {b58mh|base36|k|base32|b...}
* @param format - The format of the key, either libp2p-protobuf-cleartext or pem-pkcs8-cleartext
*
* @example
* ```js
* const key = await ipfs.key.import('clone', pem, 'password')
* const key = await ipfs.key.import('clone', new File(['keycontent'], 'test.key', { type: 'text/plain' }))
*
* console.log(key)
* // { id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ',
* // name: 'clone' }
* ```
*/
import(name: string, pem: string, password: string, options?: HTTPRPCOptions): Promise<Key>
import(name: string, file: File, ipnsBase?: string, format?: string, options?: HTTPRPCOptions): Promise<Key>
}

export function createKey (client: HTTPRPCClient): KeyAPI {
Expand Down
Loading