Skip to content

Commit

Permalink
Merge pull request #228 from ardriveapp/PE-4879_fix_gen_keypair_from_…
Browse files Browse the repository at this point in the history
…seed_phrase

fix(seed phrase): use human crypto key with a set bip 39 version for generating keypair from seed phrase
  • Loading branch information
fedellen authored Oct 27, 2023
2 parents 2401fa4 + 6e98e9f commit d0a2adb
Show file tree
Hide file tree
Showing 34 changed files with 77 additions and 514 deletions.
288 changes: 20 additions & 268 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/long-npm-4.0.0-ecd96a31ed-16afbe8f74.zip
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/nan-npm-2.15.0-505c98ef4d-33e1bb4dfc.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ardrive-core-js",
"version": "2.0.0",
"version": "2.0.1",
"description": "ArDrive Core contains the essential back end application features to support the ArDrive CLI and Desktop apps, such as file management, Permaweb upload/download, wallet management and other common functions.",
"main": "./lib/exports.js",
"types": "./lib/exports.d.ts",
Expand All @@ -11,13 +11,13 @@
"@alexsasharegan/simple-cache": "^3.3.3",
"arbundles": "^0.6.19",
"arweave": "^1.11.4",
"arweave-mnemonic-keys": "^0.0.9",
"axios": "^0.21.1",
"axios-retry": "^3.6.0",
"base64-js": "^1.5.1",
"bignumber.js": "^9.0.1",
"bn.js": "^5.2.1",
"futoin-hkdf": "^1.3.3",
"human-crypto-keys": "git+https://github.com/ardriveapp/js-human-crypto-keys.git#expose_lib",
"jwk-to-pem": "^2.0.4",
"lodash": "^4.17.21",
"mime-types": "^2.1.29",
Expand All @@ -30,6 +30,7 @@
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/bn.js": "^5",
"@types/chai": "^4.2.15",
"@types/human-crypto-keys": "^0.1.2",
"@types/jwk-to-pem": "^2.0.0",
"@types/lodash": "^4",
"@types/mime-types": "^2.1.0",
Expand Down
19 changes: 19 additions & 0 deletions src/wallet_dao.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from 'chai';
import { fakeArweave } from '../tests/stubs';
import { SeedPhrase } from './exports';
import { WalletDAO } from './wallet_dao';

// This test runs too slow to be included in the CI pipeline. But it provides a snapshot of the
// seed phrase to wallet functionality and should be run locally before a release
describe.skip('Wallet DAO', function () {
this.timeout(90_000);
const walletDAO = new WalletDAO(fakeArweave);
it('generateWallet from seedphrase function', async () => {
const seedPhrase = new SeedPhrase(
'slender during cost problem tortoise extra deal walnut great oblige planet kid'
);
const wallet = await walletDAO.generateJWKWallet(seedPhrase);
const address = await wallet.getAddress();
expect(address.toString()).to.equal('FOKCJ1sz9XfFGy8KwVQczDPdavCEu6c5GkzTNfEbRI8');
});
});
20 changes: 14 additions & 6 deletions src/wallet_dao.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Arweave from 'arweave';
import { CreateTransactionInterface } from 'arweave/node/common';
import { JWKInterface } from 'arweave/node/lib/wallet';
import { JWKWallet } from './jwk_wallet';
import {
TransactionID,
Expand All @@ -14,17 +13,22 @@ import {
GQLTagInterface,
TxID
} from './types';
import * as mnemonicKeys from 'arweave-mnemonic-keys';
import { Wallet } from './wallet';
import { DEFAULT_APP_NAME, DEFAULT_APP_VERSION } from './utils/constants';
import assertTagLimits from './arfs/tags/tag_assertions';
import { generateKeyPair, getKeyPairFromMnemonic } from 'human-crypto-keys';
import nodeCrypto from 'node:crypto';
import { JWKInterface } from 'arweave/node/lib/wallet';

export type ARTransferResult = {
txID: TransactionID;
winston: Winston;
reward: NetworkReward;
};

const algorithm = { id: 'rsa', modulusLength: 4096 } as const;
const options = { privateKeyFormat: 'pkcs8-pem' } as const;

export class WalletDAO {
constructor(
private readonly arweave: Arweave,
Expand All @@ -33,13 +37,17 @@ export class WalletDAO {
) {}

async generateSeedPhrase(): Promise<SeedPhrase> {
const seedPhrase: SeedPhrase = await mnemonicKeys.generateMnemonic();
return Promise.resolve(seedPhrase);
const keys = await generateKeyPair(algorithm, options);
return new SeedPhrase(keys.mnemonic);
}

async generateJWKWallet(seedPhrase: SeedPhrase): Promise<JWKWallet> {
const jwkWallet: JWKInterface = await mnemonicKeys.getKeyFromMnemonic(seedPhrase.toString());
return Promise.resolve(new JWKWallet(jwkWallet));
const { privateKey } = await getKeyPairFromMnemonic(seedPhrase.toString(), algorithm, options);

const pem = nodeCrypto.createPrivateKey({ key: privateKey, format: 'pem' });
const jwk = pem.export({ format: 'jwk' });

return Promise.resolve(new JWKWallet(jwk as JWKInterface));
}

async getWalletWinstonBalance(wallet: Wallet): Promise<Winston> {
Expand Down
Loading

0 comments on commit d0a2adb

Please sign in to comment.