Skip to content

Commit

Permalink
refactor: remove deprecated methods and interfaces related to account… (
Browse files Browse the repository at this point in the history
#288)

* refactor: remove deprecated methods and interfaces related to account management

* chore: update GitHub Actions workflows to use latest versions of actions
  • Loading branch information
IgorShadurin authored Nov 13, 2023
1 parent 279dda7 commit 3844760
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 514 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish_npmjs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: 'https://registry.npmjs.org'
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -42,7 +42,7 @@ jobs:
run: fdp-play start -d --fairos --fairos-image $FAIROS_IMAGE --bee-version $BEE_VERSION

## Try getting the node modules from cache, if failed npm ci
- uses: actions/cache@v2
- uses: actions/cache@v3
id: cache-npm
with:
path: node_modules
Expand Down Expand Up @@ -70,12 +70,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -90,7 +90,7 @@ jobs:
run: fdp-play start -d --bee-version $BEE_VERSION

## Try getting the node modules from cache, if failed npm ci
- uses: actions/cache@v2
- uses: actions/cache@v3
id: cache-npm
with:
path: node_modules
Expand Down
25 changes: 0 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,31 +310,6 @@ Checks whether the public key associated with the username in ENS is identical w
await fdp.account.isPublicKeyEqual('username')
```

### Migrate from v1 to v2 account

Export old wallet with mnemonic

```js
const wallet = await fdp.account.exportWallet('oldusername', 'oldpassword', {
mnemonic: 'one two three one two three one two three one two three'
})
```

or with address

```js
const wallet = await fdp.account.exportWallet('oldusername', 'oldpassword', {
address: '0x...'
})
```

```js
// ask user to top up his account, then can be started the migration process
await fdp.account.migrate('oldusername', 'oldpassword', {
mnemonic: wallet.mnemonic.phrase
})
```

Using FDP instance with cache

```js
Expand Down
63 changes: 3 additions & 60 deletions src/account/account-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { utils, Wallet } from 'ethers'
import { assertAccount, assertMnemonic, assertPassword, assertUsername, HD_PATH, removeZeroFromHex } from './utils'
import { getEncryptedMnemonic } from './mnemonic'
import { decryptText } from '../utils/encryption'
import { downloadPortableAccount, uploadPortableAccount, UserAccountWithMnemonic } from './account'
import { assertAccount, assertPassword, assertUsername, HD_PATH, removeZeroFromHex } from './utils'
import { downloadPortableAccount, uploadPortableAccount } from './account'
import { Connection } from '../connection/connection'
import { AddressOptions, isAddressOptions, isMnemonicOptions, MnemonicOptions, RegistrationRequest } from './types'
import { RegistrationRequest } from './types'
import { ENS, PublicKey } from '@fairdatasociety/fdp-contracts-js'
import { Reference, Utils } from '@ethersphere/bee-js'
import CryptoJS from 'crypto-js'
Expand Down Expand Up @@ -77,61 +75,6 @@ export class AccountData {
return wallet
}

/**
* Exports wallet from version 1 account
*
* Account and postage batch id are not required
*
* @deprecated the method will be removed after an accounts' migration process is completed
*
* @param username username from version 1 account
* @param password password from version 1 account
* @param options migration options with address or mnemonic from version 1 account
*/
async exportWallet(
username: string,
password: string,
options: AddressOptions | MnemonicOptions,
): Promise<UserAccountWithMnemonic> {
assertUsername(username)
assertPassword(password)

let mnemonic = isMnemonicOptions(options) ? options.mnemonic : undefined

if (isAddressOptions(options)) {
const address = prepareEthAddress(options.address)
const encryptedMnemonic = await getEncryptedMnemonic(this.connection.bee, username, address)
mnemonic = decryptText(password, encryptedMnemonic)
}

assertMnemonic(mnemonic)

const wallet = Wallet.fromMnemonic(mnemonic)

return { wallet, mnemonic }
}

/**
* Migrates from FDP account without ENS to account with ENS
*
* Account and postage batch id are not required
*
* @deprecated the method will be removed after an accounts' migration process is completed
*
* @param username username from version 1 account
* @param password password from version 1 account
* @param options migration options with address or mnemonic from version 1 account
*/
async migrate(username: string, password: string, options: AddressOptions | MnemonicOptions): Promise<Reference> {
assertUsername(username)
assertPassword(password)

const exported = await this.exportWallet(username, password, options)
this.setAccountFromMnemonic(exported.mnemonic)

return this.register(this.createRegistrationRequest(username, password))
}

/**
* Logs in with the FDP credentials and gives back ethers wallet
*
Expand Down
88 changes: 2 additions & 86 deletions src/account/account.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
import { Bee, PrivateKeyBytes, Reference, Utils } from '@ethersphere/bee-js'
import { utils, Wallet } from 'ethers'
import { encryptText, IV_LENGTH, decryptBytes, encryptBytes } from '../utils/encryption'
import { uploadEncryptedMnemonic } from './mnemonic'
import {
assertChunkSizeLength,
assertMnemonic,
assertPassword,
CHUNK_SIZE,
SEED_SIZE,
createCredentialsTopic,
HD_PATH,
} from './utils'
import { IV_LENGTH, decryptBytes, encryptBytes } from '../utils/encryption'
import { assertChunkSizeLength, CHUNK_SIZE, SEED_SIZE, createCredentialsTopic, HD_PATH } from './utils'
import { Connection } from '../connection/connection'
import CryptoJS from 'crypto-js'
import { wordArrayToBytes } from '../utils/bytes'

/**
* Created and encrypted user account to upload to the network
* @deprecated interface for v1 accounts
*/
interface UserAccount {
wallet: Wallet
mnemonic: string
encryptedMnemonic: string
}

/**
* Account and mnemonic phrase
* @deprecated interface for v1 accounts
*/
export interface UserAccountWithMnemonic {
wallet: Wallet
mnemonic: string
}

/**
* User account with seed phrase
*/
Expand All @@ -42,62 +14,6 @@ export interface UserAccountWithSeed {
seed: Uint8Array
}

/**
* Encrypted account uploaded to the network
*/
export interface UserAccountWithReference extends UserAccount {
reference: Reference
}

/**
* Creates a new user account based on the passed mnemonic phrase or without it, encrypted with a password
*
* @deprecated method for v1 accounts
*
* @param password FDP password
* @param mnemonic mnemonic phrase
*/
async function createUserAccount(password: string, mnemonic?: string): Promise<UserAccount> {
assertPassword(password)

if (mnemonic) {
assertMnemonic(mnemonic)
} else {
mnemonic = Wallet.createRandom().mnemonic.phrase
}

const wallet = Wallet.fromMnemonic(mnemonic)
const encryptedMnemonic = encryptText(password, mnemonic)

return {
wallet,
mnemonic,
encryptedMnemonic,
}
}

/**
* Creates a new user (version 1) and uploads the encrypted account to the network
*
* @deprecated use `createUser` method instead to create the latest version of an account
*
* @param connection connection information for data uploading
* @param username FDP username
* @param password FDP password
* @param mnemonic mnemonic phrase
*/
export async function createUserV1(
connection: Connection,
username: string,
password: string,
mnemonic?: string,
): Promise<UserAccountWithReference> {
const account = await createUserAccount(password, mnemonic)
const reference = await uploadEncryptedMnemonic(connection, account.wallet, username, account.encryptedMnemonic)

return { ...account, reference }
}

/**
* Uploads portable account (version 2)
*
Expand Down
45 changes: 0 additions & 45 deletions src/account/mnemonic.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/account/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import { Utils } from '@ethersphere/bee-js'
import { isEthAddress, isObject } from '../utils/type'
import { isValidMnemonic } from 'ethers/lib/utils'
import { ServiceRequest } from '@fairdatasociety/fdp-contracts-js/build/types/model/service-request.model'
import { RegisterUsernameRequestData } from '@fairdatasociety/fdp-contracts-js'

/**
* Migrate options with possibility to migrate with an address
*/
export interface AddressOptions {
address: Utils.EthAddress
}

/**
* Migrate options with possibility to migrate with a mnemonic
*/
export interface MnemonicOptions {
mnemonic: string
}

/**
* This objects encapsulates state of registration process.
*/
Expand All @@ -28,20 +11,6 @@ export interface RegistrationRequest {
ensRequest?: ServiceRequest<RegisterUsernameRequestData>
}

/**
* Checks that value is an AddressOptions
*/
export function isAddressOptions(options: unknown): options is AddressOptions {
return isObject(options) && isEthAddress((options as AddressOptions).address)
}

/**
* Checks that value is an MnemonicOptions
*/
export function isMnemonicOptions(options: unknown): options is MnemonicOptions {
return isObject(options) && isValidMnemonic((options as MnemonicOptions).mnemonic)
}

/**
* Assert account options
*/
Expand Down
Loading

0 comments on commit 3844760

Please sign in to comment.