Files
bee-dashboard/src/utils/identity.ts
T
Vojtech Simetka 80d684c1e5 fix: all wallet flows to use ethers (#395)
* fix: all wallet flows to use only ethers libraries

* feat: remove ethereumjs-wallet

* fix: remove the buggy `/wallet` bee call and use provider
2022-06-18 21:50:51 +02:00

101 lines
2.9 KiB
TypeScript

import { BatchId, Bee, BeeDebug, Reference } from '@ethersphere/bee-js'
import { Wallet } from 'ethers'
import { uuidV4, waitUntilStampUsable } from '.'
import { Identity, IdentityType } from '../providers/Feeds'
export function generateWallet(): Wallet {
return Wallet.createRandom()
}
export function persistIdentity(identities: Identity[], identity: Identity): void {
const existingIndex = identities.findIndex(x => x.uuid === identity.uuid)
if (existingIndex !== -1) {
identities.splice(existingIndex, 1)
}
identities.unshift(identity)
localStorage.setItem('feeds', JSON.stringify(identities))
}
export function persistIdentitiesWithoutUpdate(identities: Identity[]): void {
localStorage.setItem('feeds', JSON.stringify(identities))
}
export async function convertWalletToIdentity(
identity: Wallet,
type: IdentityType,
name: string,
password?: string,
): Promise<Identity> {
if (type === 'V3' && !password) {
throw Error('V3 passwords require password')
}
const identityString = type === 'PRIVATE_KEY' ? identity.privateKey : await identity.encrypt(password as string)
return {
uuid: uuidV4(),
name,
type: password ? 'V3' : 'PRIVATE_KEY',
address: identity.address,
identity: identityString,
}
}
export async function importIdentity(name: string, data: string): Promise<Identity | null> {
if (data.length === 64) {
const wallet = await getWallet('PRIVATE_KEY', data)
return {
uuid: uuidV4(),
name,
type: 'PRIVATE_KEY',
identity: data,
address: wallet.address,
}
}
if (data.length === 66 && data.toLowerCase().startsWith('0x')) {
const wallet = await getWallet('PRIVATE_KEY', data.slice(2))
return { uuid: uuidV4(), name, type: 'PRIVATE_KEY', identity: data, address: wallet.address }
}
try {
const { address } = JSON.parse(data)
return { uuid: uuidV4(), name, type: 'V3', identity: data, address }
} catch {
return null
}
}
function getWalletFromIdentity(identity: Identity, password?: string): Promise<Wallet> {
return getWallet(identity.type, identity.identity, password)
}
async function getWallet(type: IdentityType, data: string, password?: string): Promise<Wallet> {
return type === 'PRIVATE_KEY' ? new Wallet(data) : await Wallet.fromEncryptedJson(data, password as string)
}
export async function updateFeed(
beeApi: Bee,
beeDebugApi: BeeDebug | null,
identity: Identity,
hash: string,
stamp: string,
password?: string,
): Promise<void> {
const wallet = await getWalletFromIdentity(identity, password)
if (!identity.feedHash) {
identity.feedHash = await beeApi.createFeedManifest(stamp, 'sequence', '00'.repeat(32), wallet.address)
}
const writer = beeApi.makeFeedWriter('sequence', '00'.repeat(32), wallet.privateKey)
if (beeDebugApi) {
await waitUntilStampUsable(stamp as BatchId, beeDebugApi)
}
await writer.upload(stamp, hash as Reference)
}