-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add discriminator nodes * Add byteDiscriminatorNodeFromBase58 helper * Update @solana/codecs-strings to the latest version * Fix typo * Add DiscriminatorNodes to AccountNode * Rename AnchorDiscriminator helper file * Add DiscriminatorNodes to InstructionNode * Update merge and identity visitors * Add changeset
- Loading branch information
1 parent
cd243ea
commit 6d02b0e
Showing
22 changed files
with
207 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@metaplex-foundation/kinobi': minor | ||
--- | ||
|
||
Add discriminator nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getBase58Encoder } from '@solana/codecs-strings'; | ||
|
||
export type ByteDiscriminatorNode = { | ||
readonly kind: 'byteDiscriminatorNode'; | ||
|
||
// Data. | ||
readonly bytes: number[]; | ||
readonly offset: number; | ||
}; | ||
|
||
export function byteDiscriminatorNode( | ||
bytes: number[], | ||
offset: number = 0 | ||
): ByteDiscriminatorNode { | ||
return { kind: 'byteDiscriminatorNode', bytes, offset }; | ||
} | ||
|
||
export function byteDiscriminatorNodeFromBase58( | ||
base58Bytes: string, | ||
offset: number = 0 | ||
): ByteDiscriminatorNode { | ||
return byteDiscriminatorNode( | ||
[...getBase58Encoder().encode(base58Bytes)], | ||
offset | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getNodeKinds } from '../../shared/utils'; | ||
import type { ByteDiscriminatorNode } from './ByteDiscriminatorNode'; | ||
import type { FieldDiscriminatorNode } from './FieldDiscriminatorNode'; | ||
import type { SizeDiscriminatorNode } from './SizeDiscriminatorNode'; | ||
|
||
// Discriminator Node Registration. | ||
|
||
export const REGISTERED_DISCRIMINATOR_NODES = { | ||
byteDiscriminatorNode: {} as ByteDiscriminatorNode, | ||
fieldDiscriminatorNode: {} as FieldDiscriminatorNode, | ||
sizeDiscriminatorNode: {} as SizeDiscriminatorNode, | ||
}; | ||
|
||
export const REGISTERED_DISCRIMINATOR_NODE_KINDS = getNodeKinds( | ||
REGISTERED_DISCRIMINATOR_NODES | ||
); | ||
export type RegisteredDiscriminatorNodeKind = | ||
typeof REGISTERED_DISCRIMINATOR_NODE_KINDS[number]; | ||
export type RegisteredDiscriminatorNode = | ||
typeof REGISTERED_DISCRIMINATOR_NODES[RegisteredDiscriminatorNodeKind]; | ||
|
||
// Discriminator Node Helpers. | ||
|
||
export type DiscriminatorNode = RegisteredDiscriminatorNode; | ||
export const DISCRIMINATOR_NODES = REGISTERED_DISCRIMINATOR_NODE_KINDS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MainCaseString, mainCase } from '../../shared'; | ||
|
||
export type FieldDiscriminatorNode = { | ||
readonly kind: 'fieldDiscriminatorNode'; | ||
|
||
// Data. | ||
readonly name: MainCaseString; | ||
readonly offset: number; | ||
}; | ||
|
||
export function fieldDiscriminatorNode( | ||
name: string, | ||
offset: number = 0 | ||
): FieldDiscriminatorNode { | ||
return { kind: 'fieldDiscriminatorNode', name: mainCase(name), offset }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type SizeDiscriminatorNode = { | ||
readonly kind: 'sizeDiscriminatorNode'; | ||
|
||
// Data. | ||
readonly size: number; | ||
}; | ||
|
||
export function sizeDiscriminatorNode(size: number): SizeDiscriminatorNode { | ||
return { kind: 'sizeDiscriminatorNode', size }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './ByteDiscriminatorNode'; | ||
export * from './DiscriminatorNode'; | ||
export * from './FieldDiscriminatorNode'; | ||
export * from './SizeDiscriminatorNode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 3 additions & 5 deletions
8
src/shared/AnchorDiscriminator.ts → src/shared/anchorHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { pascalCase, snakeCase } from './utils'; | ||
import { ArrayValueNode, arrayValueNode, numberValueNode } from '../nodes'; | ||
|
||
export type AnchorDiscriminator = ArrayValueNode; | ||
import { pascalCase, snakeCase } from './utils'; | ||
|
||
export const getAnchorInstructionDiscriminator = ( | ||
idlName: string | ||
): AnchorDiscriminator => { | ||
): ArrayValueNode => { | ||
const hash = sha256(`global:${snakeCase(idlName)}`).slice(0, 8); | ||
return arrayValueNode([...hash].map((byte) => numberValueNode(byte))); | ||
}; | ||
|
||
export const getAnchorAccountDiscriminator = ( | ||
idlName: string | ||
): AnchorDiscriminator => { | ||
): ArrayValueNode => { | ||
const hash = sha256(`account:${pascalCase(idlName)}`).slice(0, 8); | ||
return arrayValueNode([...hash].map((byte) => numberValueNode(byte))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.