# keyForEnvelope

> **keyForEnvelope**(`node`, `envelopeMetadata`, `holder?`): [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)

Defined in: [derive.ts:589](https://github.com/FilOzone/synapse-sdk/blob/840d7b2c6263846ac51b7558000e62dd68d33a44/packages/keysmith/src/derive.ts#L589)

Derive a piece's key from whichever node the caller holds.

A group key holder walks the piece's whole group path. A group holder walks
the rest of the path below its own group — which works only if its group is the
piece's group or an ancestor of it. A piece holder already has the key, and
gets it back only for the piece whose salt it names.

Takes the metadata as FEE returns it (`info.appMetadata`),
unvalidated, and checks every field before using it (see [readPieceMetadata](/reference/filoz/keysmith/functions/readpiecemetadata/)).

Pass the grant that delivered the key as `holding` where you have it: then the
piece's owner and group key are checked against the grant too, so a key from the
wrong group key fails here with a clear reason rather than later inside FEE. A descriptor's `v` follows
[holdingOf](/reference/filoz/keysmith/functions/holdingof/): absent, or exactly 2.

## Parameters

| Parameter | Type | Default value |
| ------ | ------ | ------ |
| `node` | [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | `undefined` |
| `envelopeMetadata` | [`PieceMetadataV2`](/reference/filoz/keysmith/type-aliases/piecemetadatav2/) \| [`Readonly`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype)\<[`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type)\<`string`, `unknown`\>\> \| `undefined` | `undefined` |
| `holder?` | [`GrantDescriptor`](/reference/filoz/keysmith/interfaces/grantdescriptor/) \| [`Holding`](/reference/filoz/keysmith/type-aliases/holding/) | `'groupKey'` |

## Returns

[`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)

## Throws

If the piece is not format 2, belongs to another owner or group key
than the grant, the held group does not contain the piece's group, or the held
piece is a different piece.