feat: use crypto-hash instead of hasha

This commit is contained in:
Justin Starry 2020-03-16 17:44:12 +08:00 committed by Michael Vines
parent bd7e802e46
commit 402c160ae9
12 changed files with 75 additions and 78 deletions

4
web3.js/.gitignore vendored
View File

@ -12,10 +12,6 @@ node_modules
# Coverage
coverage
# Types
flow-typed/npm/*
!flow-typed/npm/module_vx.x.x.js
# Release
lib

View File

@ -1,4 +0,0 @@
declare module 'hasha' {
// TODO: Fill in types
declare module.exports: any;
}

View File

@ -0,0 +1,45 @@
// flow-typed signature: bd59b655ec89c675a5f5c2188a37c49d
// flow-typed version: <<STUB>>/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'>;
}

5
web3.js/module.d.ts vendored
View File

@ -6,6 +6,11 @@ declare module '@solana/web3.js' {
export class PublicKey {
constructor(value: number | string | Buffer | Uint8Array | Array<number>);
static isPublicKey(o: object): boolean;
static createWithSeed(
fromPublicKey: PublicKey,
seed: string,
programId: PublicKey,
): Promise<PublicKey>;
equals(publickey: PublicKey): boolean;
toBase58(): string;
toBuffer(): Buffer;

View File

@ -20,6 +20,11 @@ declare module '@solana/web3.js' {
value: number | string | Buffer | Uint8Array | Array<number>,
): PublicKey;
static isPublicKey(o: Object): boolean;
static createWithSeed(
fromPublicKey: PublicKey,
seed: string,
programId: PublicKey,
): Promise<PublicKey>;
equals(publickey: PublicKey): boolean;
toBase58(): string;
toBuffer(): Buffer;

View File

@ -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",

View File

@ -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",

View File

@ -98,6 +98,7 @@ function generateConfig(configType) {
'bn.js',
'bs58',
'buffer-layout',
'crypto-hash',
'jayson/lib/client/browser',
'node-fetch',
'rpc-websockets',

View File

@ -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<PublicKey> {
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);
}
}

View File

@ -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,

View File

@ -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,

View File

@ -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',