diff --git a/web3.js/.gitignore b/web3.js/.gitignore index d9e5fcfcd2..70d8589e04 100644 --- a/web3.js/.gitignore +++ b/web3.js/.gitignore @@ -12,10 +12,6 @@ node_modules # Coverage coverage -# Types -flow-typed/npm/* -!flow-typed/npm/module_vx.x.x.js - # Release lib diff --git a/web3.js/flow-typed/hasha.js b/web3.js/flow-typed/hasha.js deleted file mode 100644 index 4d3113a2a8..0000000000 --- a/web3.js/flow-typed/hasha.js +++ /dev/null @@ -1,4 +0,0 @@ -declare module 'hasha' { - // TODO: Fill in types - declare module.exports: any; -} diff --git a/web3.js/flow-typed/npm/crypto-hash_vx.x.x.js b/web3.js/flow-typed/npm/crypto-hash_vx.x.x.js new file mode 100644 index 0000000000..7acfdfa8ff --- /dev/null +++ b/web3.js/flow-typed/npm/crypto-hash_vx.x.x.js @@ -0,0 +1,45 @@ +// flow-typed signature: bd59b655ec89c675a5f5c2188a37c49d +// flow-typed version: <>/crypto-hash_v1.2.2/flow_v0.120.1 + +/** + * This is an autogenerated libdef stub for: + * + * 'crypto-hash' + * + * Fill this stub out by replacing all the `any` types. + * + * Once filled out, we encourage you to share your work with the + * community by sending a pull request to: + * https://github.com/flowtype/flow-typed + */ + +declare module 'crypto-hash' { + declare module.exports: any; +} + +/** + * We include stubs for each file inside this npm package in case you need to + * require those files directly. Feel free to delete any files that aren't + * needed. + */ +declare module 'crypto-hash/browser' { + declare module.exports: any; +} + +declare module 'crypto-hash/thread' { + declare module.exports: any; +} + +// Filename aliases +declare module 'crypto-hash/browser.js' { + declare module.exports: $Exports<'crypto-hash/browser'>; +} +declare module 'crypto-hash/index' { + declare module.exports: $Exports<'crypto-hash'>; +} +declare module 'crypto-hash/index.js' { + declare module.exports: $Exports<'crypto-hash'>; +} +declare module 'crypto-hash/thread.js' { + declare module.exports: $Exports<'crypto-hash/thread'>; +} diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 8b524f1307..6b881ab769 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -6,6 +6,11 @@ declare module '@solana/web3.js' { export class PublicKey { constructor(value: number | string | Buffer | Uint8Array | Array); static isPublicKey(o: object): boolean; + static createWithSeed( + fromPublicKey: PublicKey, + seed: string, + programId: PublicKey, + ): Promise; equals(publickey: PublicKey): boolean; toBase58(): string; toBuffer(): Buffer; diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 06cec220c0..6ac0d7d863 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -20,6 +20,11 @@ declare module '@solana/web3.js' { value: number | string | Buffer | Uint8Array | Array, ): PublicKey; static isPublicKey(o: Object): boolean; + static createWithSeed( + fromPublicKey: PublicKey, + seed: string, + programId: PublicKey, + ): Promise; equals(publickey: PublicKey): boolean; toBase58(): string; toBuffer(): Buffer; diff --git a/web3.js/package-lock.json b/web3.js/package-lock.json index bfb28e77a9..4a79673071 100644 --- a/web3.js/package-lock.json +++ b/web3.js/package-lock.json @@ -8175,6 +8175,11 @@ "randomfill": "^1.0.3" } }, + "crypto-hash": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/crypto-hash/-/crypto-hash-1.2.2.tgz", + "integrity": "sha512-rXXMXepuKg9gIfqE7I1jtVa6saLhzIkDQ2u3kTGUWYiUGsHcUa3LTsfrjPEdOY8kxKlryQtsOmJOU0F23yRJTg==" + }, "crypto-random-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", @@ -10942,27 +10947,6 @@ "minimalistic-assert": "^1.0.1" } }, - "hasha": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/hasha/-/hasha-5.2.0.tgz", - "integrity": "sha512-2W+jKdQbAdSIrggA8Q35Br8qKadTrqCTC8+XZvBWepKDK6m9XkX6Iz1a2yh2KP01kzAR/dpuMeUnocoLYDcskw==", - "requires": { - "is-stream": "^2.0.0", - "type-fest": "^0.8.0" - }, - "dependencies": { - "is-stream": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz", - "integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==" - }, - "type-fest": { - "version": "0.8.1", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz", - "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==" - } - } - }, "hawk": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/hawk/-/hawk-6.0.2.tgz", diff --git a/web3.js/package.json b/web3.js/package.json index c340cae311..966221aecd 100644 --- a/web3.js/package.json +++ b/web3.js/package.json @@ -73,8 +73,8 @@ "bs58": "^4.0.1", "buffer": "^5.4.3", "buffer-layout": "^1.2.0", + "crypto-hash": "^1.2.2", "esdoc-inject-style-plugin": "^1.0.0", - "hasha": "^5.1.0", "jayson": "^3.0.1", "mz": "^2.7.0", "node-fetch": "^2.2.0", diff --git a/web3.js/rollup.config.js b/web3.js/rollup.config.js index 4373053e07..ce3966a8f7 100644 --- a/web3.js/rollup.config.js +++ b/web3.js/rollup.config.js @@ -98,6 +98,7 @@ function generateConfig(configType) { 'bn.js', 'bs58', 'buffer-layout', + 'crypto-hash', 'jayson/lib/client/browser', 'node-fetch', 'rpc-websockets', diff --git a/web3.js/src/publickey.js b/web3.js/src/publickey.js index 58930a4bdc..eda451614c 100644 --- a/web3.js/src/publickey.js +++ b/web3.js/src/publickey.js @@ -2,7 +2,7 @@ import BN from 'bn.js'; import bs58 from 'bs58'; -import hasha from 'hasha'; +import {sha256} from 'crypto-hash'; /** * A public key @@ -80,17 +80,17 @@ export class PublicKey { /** * Derive a public key from another key, a seed, and a programId. */ - static createWithSeed( + static async createWithSeed( fromPublicKey: PublicKey, seed: string, programId: PublicKey, - ): PublicKey { + ): Promise { const buffer = Buffer.concat([ fromPublicKey.toBuffer(), Buffer.from(seed), programId.toBuffer(), ]); - const hash = hasha(buffer, {algorithm: 'sha256'}); + const hash = await sha256(new Uint8Array(buffer)); return new PublicKey('0x' + hash); } } diff --git a/web3.js/test/publickey.test.js b/web3.js/test/publickey.test.js index c58a5923f7..b66fba51af 100644 --- a/web3.js/test/publickey.test.js +++ b/web3.js/test/publickey.test.js @@ -224,42 +224,9 @@ test('equals (II)', () => { expect(key1.equals(key2)).toBe(true); }); -test('createWithSeed', () => { - const defaultPublicKey = new PublicKey([ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ]); - const derivedKey = PublicKey.createWithSeed( +test('createWithSeed', async () => { + const defaultPublicKey = new PublicKey('11111111111111111111111111111111'); + const derivedKey = await PublicKey.createWithSeed( defaultPublicKey, 'limber chicken: 4/45', defaultPublicKey, diff --git a/web3.js/test/stake-program.test.js b/web3.js/test/stake-program.test.js index 88adeed5c2..3f1f94045c 100644 --- a/web3.js/test/stake-program.test.js +++ b/web3.js/test/stake-program.test.js @@ -22,10 +22,10 @@ if (!mockRpcEnabled) { jest.setTimeout(30000); } -test('createAccountWithSeed', () => { +test('createAccountWithSeed', async () => { const fromPubkey = new Account().publicKey; const seed = 'test string'; - const newAccountPubkey = PublicKey.createWithSeed( + const newAccountPubkey = await PublicKey.createWithSeed( fromPubkey, seed, StakeProgram.programId, @@ -180,10 +180,10 @@ test('deactivate', () => { expect(params).toEqual(StakeInstruction.decodeDeactivate(stakeInstruction)); }); -test('StakeInstructions', () => { +test('StakeInstructions', async () => { const from = new Account(); const seed = 'test string'; - const newAccountPubkey = PublicKey.createWithSeed( + const newAccountPubkey = await PublicKey.createWithSeed( from.publicKey, seed, StakeProgram.programId, @@ -288,7 +288,7 @@ test('live staking actions', async () => { // Create Stake account with seed const seed = 'test string'; - const newAccountPubkey = PublicKey.createWithSeed( + const newAccountPubkey = await PublicKey.createWithSeed( from.publicKey, seed, StakeProgram.programId, diff --git a/web3.js/test/testnet.test.js b/web3.js/test/testnet.test.js index 30ff820dc1..2b40299909 100644 --- a/web3.js/test/testnet.test.js +++ b/web3.js/test/testnet.test.js @@ -8,9 +8,7 @@ test('invalid', () => { }); test('stable', () => { - expect(testnetChannelEndpoint('stable')).toEqual( - 'https://devnet.solana.com', - ); + expect(testnetChannelEndpoint('stable')).toEqual('https://devnet.solana.com'); expect(testnetChannelEndpoint('stable', true)).toEqual( 'https://devnet.solana.com',