generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { mergeBytes } from '@metaplex-foundation/umi/serializers'; | ||
import { keccak_256 } from '@noble/hashes/sha3'; | ||
|
||
export function hash(input: Uint8Array | Uint8Array[]): Uint8Array { | ||
return keccak_256(Array.isArray(input) ? mergeBytes(input) : input); | ||
} |
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,3 +1,4 @@ | ||
export * from './generated'; | ||
export * from './plugin'; | ||
export * from './hooked'; | ||
export * from './hash'; | ||
export * from './hooked'; |
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,94 @@ | ||
import { generateSigner } from '@metaplex-foundation/umi'; | ||
import test from 'ava'; | ||
import { Asset, DataState, create, fetchAsset, fetchHashedAsset, getAssetAccountDataSerializer, compress, HashedAssetSchema, getHashedAssetSchemaSerializer } from '../src'; | ||
import { createUmi } from './_setup'; | ||
import bs58 from 'bs58'; | ||
import { hash } from '../src'; | ||
|
||
test('it can compress an asset without any plugins as the owner', async (t) => { | ||
// Given a Umi instance and a new signer. | ||
const umi = await createUmi(); | ||
const assetAddress = generateSigner(umi); | ||
|
||
// When we create a new account. | ||
await create(umi, { | ||
dataState: DataState.AccountState, | ||
assetAddress, | ||
name: 'Test Bread', | ||
uri: 'https://example.com/bread', | ||
}).sendAndConfirm(umi); | ||
|
||
// Then an account was created with the correct data. | ||
const beforeAsset = await fetchAsset(umi, assetAddress.publicKey); | ||
//console.log("Account State:", beforeAsset); | ||
t.like(beforeAsset, <Asset>{ | ||
publicKey: assetAddress.publicKey, | ||
updateAuthority: umi.identity.publicKey, | ||
owner: umi.identity.publicKey, | ||
name: 'Test Bread', | ||
uri: 'https://example.com/bread', | ||
}); | ||
|
||
// And when we compress the asset. | ||
let tx = await compress(umi, { | ||
assetAddress: assetAddress.publicKey, | ||
owner: umi.identity, | ||
}).sendAndConfirm(umi); | ||
console.log("Compress signature: ", bs58.encode(tx.signature)); | ||
|
||
// And the asset is now compressed as a hashed asset. | ||
const afterAsset = await fetchHashedAsset(umi, assetAddress.publicKey); | ||
//console.log("Account State:", afterAsset); | ||
|
||
// And the hash matches the expected value. | ||
let hashedAssetSchema: HashedAssetSchema = { | ||
assetHash: hash(getAssetAccountDataSerializer().serialize(beforeAsset)), | ||
pluginHashes: [], | ||
}; | ||
|
||
let hashedAsset = hash(getHashedAssetSchemaSerializer().serialize(hashedAssetSchema)); | ||
t.deepEqual(afterAsset.hash, hashedAsset); | ||
}); | ||
|
||
test('it cannot compress an asset if not the owner', async (t) => { | ||
// Given a Umi instance and a new signer. | ||
const umi = await createUmi(); | ||
const assetAddress = generateSigner(umi); | ||
const attacker = generateSigner(umi); | ||
|
||
// When we create a new account. | ||
await create(umi, { | ||
dataState: DataState.AccountState, | ||
assetAddress, | ||
name: 'Test Bread', | ||
uri: 'https://example.com/bread', | ||
}).sendAndConfirm(umi); | ||
|
||
// Then an account was created with the correct data. | ||
const beforeAsset = await fetchAsset(umi, assetAddress.publicKey); | ||
// console.log("Account State:", beforeAsset); | ||
t.like(beforeAsset, <Asset>{ | ||
publicKey: assetAddress.publicKey, | ||
updateAuthority: umi.identity.publicKey, | ||
owner: umi.identity.publicKey, | ||
name: 'Test Bread', | ||
uri: 'https://example.com/bread', | ||
}); | ||
|
||
const result = compress(umi, { | ||
assetAddress: assetAddress.publicKey, | ||
owner: attacker, | ||
}).sendAndConfirm(umi); | ||
|
||
await t.throwsAsync(result, { name: 'InvalidAuthority' }) | ||
|
||
const afterAsset = await fetchAsset(umi, assetAddress.publicKey); | ||
// console.log("Account State:", afterAsset); | ||
t.like(afterAsset, <Asset>{ | ||
publicKey: assetAddress.publicKey, | ||
updateAuthority: umi.identity.publicKey, | ||
owner: umi.identity.publicKey, | ||
name: 'Test Bread', | ||
uri: 'https://example.com/bread', | ||
}); | ||
}); |