Skip to content

Commit bec5d4c

Browse files
committed
feat: update key import function to handle file uploads
1 parent 1ab7941 commit bec5d4c

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/key/import.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ import type { KeyAPI } from './index.js'
44
import type { HTTPRPCClient } from '../lib/core.js'
55

66
export function createImport (client: HTTPRPCClient): KeyAPI['import'] {
7-
return async function importKey (name, pem, password, options = {}) {
7+
return async function importKey (name, file, ipnsBase, format, options = {}) {
8+
const body = new FormData()
9+
body.append('key', file)
10+
811
const res = await client.post('key/import', {
912
signal: options.signal,
1013
searchParams: toUrlSearchParams({
1114
arg: name,
12-
pem,
13-
password,
15+
format,
16+
'ipns-base': ipnsBase,
1417
...options
1518
}),
16-
headers: options.headers
19+
body,
20+
headers: {
21+
...options.headers,
22+
'Content-Type': 'multipart/form-data'
23+
}
1724
})
1825
const data = await res.json()
1926

src/key/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,23 @@ export interface KeyAPI {
9393
rename(oldName: string, newName: string, options?: HTTPRPCOptions): Promise<KeyRenameResult>
9494

9595
/**
96-
* Remove a key
96+
* import a key
97+
*
98+
* @param name - The name of the key
99+
* @param file - The file to import
100+
* @param ipnsBase - The base of the IPNS name default is base36, Takes {b58mh|base36|k|base32|b...}
101+
* @param format - The format of the key, either libp2p-protobuf-cleartext or pem-pkcs8-cleartext
97102
*
98103
* @example
99104
* ```js
100-
* const key = await ipfs.key.import('clone', pem, 'password')
105+
* const key = await ipfs.key.import('clone', new File(['keycontent'], 'test.key', { type: 'text/plain' }))
101106
*
102107
* console.log(key)
103108
* // { id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ',
104109
* // name: 'clone' }
105110
* ```
106111
*/
107-
import(name: string, pem: string, password: string, options?: HTTPRPCOptions): Promise<Key>
112+
import(name: string, file: File, ipnsBase?: string, format?: string, options?: HTTPRPCOptions): Promise<Key>
108113
}
109114

110115
export function createKey (client: HTTPRPCClient): KeyAPI {

0 commit comments

Comments
 (0)