feat: update superstruct and future proof type validations (#15491)

* chore: update superstruct

* fix: fix program account notification type coercion
This commit is contained in:
Justin Starry 2021-02-26 15:06:12 +08:00 committed by GitHub
parent d866f742e2
commit bb06817e16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 591 additions and 939 deletions

View File

@ -3741,6 +3741,16 @@
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"superstruct": {
"version": "0.8.4",
"resolved": "https://registry.npmjs.org/superstruct/-/superstruct-0.8.4.tgz",
"integrity": "sha512-48Ors8IVWZm/tMr8r0Si6+mJiB7mkD7jqvIzktjJ4+EnP5tBp0qOpiM1J8sCUorKx+TXWrfb3i1UcjdD1YK/wA==",
"dev": true,
"requires": {
"kind-of": "^6.0.2",
"tiny-invariant": "^1.0.6"
}
}
}
},
@ -5914,7 +5924,7 @@
},
"is-obj": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz",
"resolved": "http://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz",
"integrity": "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==",
"dev": true
}
@ -18426,20 +18436,9 @@
}
},
"superstruct": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/superstruct/-/superstruct-0.8.3.tgz",
"integrity": "sha512-LbtbFpktW1FcwxVIJlxdk7bCyBq/GzOx2FSFLRLTUhWIA1gHkYPIl3aXRG5mBdGZtnPNT6t+4eEcLDCMOuBHww==",
"requires": {
"kind-of": "^6.0.2",
"tiny-invariant": "^1.0.6"
},
"dependencies": {
"kind-of": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",
"integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw=="
}
}
"version": "0.14.2",
"resolved": "https://registry.npmjs.org/superstruct/-/superstruct-0.14.2.tgz",
"integrity": "sha512-nPewA6m9mR3d6k7WkZ8N8zpTWfenFH3q9pA2PkuiZxINr9DKB2+40wEQf0ixn8VaGuJ78AB6iWOtStI+/4FKZQ=="
},
"supports-color": {
"version": "2.0.0",
@ -18857,9 +18856,10 @@
}
},
"tiny-invariant": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.0.6.tgz",
"integrity": "sha512-FOyLWWVjG+aC0UqG76V53yAWdXfH8bO6FNmyZOuUrzDzK8DI3/JRY25UD7+g49JWM1LXwymsKERB+DzI0dTEQA=="
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.1.0.tgz",
"integrity": "sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==",
"dev": true
},
"to-fast-properties": {
"version": "1.0.3",

View File

@ -94,7 +94,7 @@
"node-fetch": "^2.6.1",
"rpc-websockets": "^7.4.2",
"secp256k1": "^4.0.2",
"superstruct": "^0.8.3",
"superstruct": "^0.14.2",
"tweetnacl": "^1.0.0"
},
"devDependencies": {

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,12 @@
// @flow
import {Buffer} from 'buffer';
import {struct} from 'superstruct';
import {
assert as assertType,
optional,
string,
type as pick,
} from 'superstruct';
import * as Layout from './layout';
import * as shortvec from './util/shortvec-encoding';
@ -35,11 +40,11 @@ export type Info = {|
keybaseUsername?: string,
|};
const InfoString = struct({
name: 'string',
website: 'string?',
details: 'string?',
keybaseUsername: 'string?',
const InfoString = pick({
name: string(),
website: optional(string()),
details: optional(string()),
keybaseUsername: optional(string()),
});
/**
@ -94,7 +99,8 @@ export class ValidatorInfo {
if (configKeys[0].publicKey.equals(VALIDATOR_INFO_KEY)) {
if (configKeys[1].isSigner) {
const rawInfo = Layout.rustString().decode(Buffer.from(byteArray));
const info = InfoString(JSON.parse(rawInfo));
const info = JSON.parse(rawInfo);
assertType(info, InfoString);
return new ValidatorInfo(configKeys[1].publicKey, info);
}
}

View File

@ -1542,6 +1542,7 @@ describe('Connection', () => {
lamports: LAMPORTS_PER_SOL,
data: ['', 'base64'],
executable: false,
rentEpoch: 20,
},
withContext: true,
});
@ -1569,6 +1570,7 @@ describe('Connection', () => {
lamports: LAMPORTS_PER_SOL,
data: ['', 'base64'],
executable: false,
rentEpoch: 20,
},
withContext: true,
});
@ -1903,10 +1905,7 @@ describe('Connection', () => {
const subscriptionId = connection.onProgramAccountChange(
SystemProgram.programId,
(keyedAccountInfo: KeyedAccountInfo) => {
// FIX: accountId should be `PublicKey` not `string`
if (
keyedAccountInfo.accountId === programAccount.publicKey.toBase58()
) {
if (keyedAccountInfo.accountId.equals(programAccount.publicKey)) {
expect(keyedAccountInfo.accountInfo.lamports).to.eq(balanceNeeded);
expect(
keyedAccountInfo.accountInfo.owner.equals(

View File

@ -92,6 +92,7 @@ describe('Nonce', () => {
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
rentEpoch: 20,
},
withContext: true,
});
@ -162,6 +163,7 @@ describe('Nonce', () => {
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
rentEpoch: 20,
},
withContext: true,
});