# keyForEnvelope

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

Defined in: [derive.ts:367](https://github.com/FilOzone/synapse-sdk/blob/68e30a2a5457180e60a5579eb5ecff90b65894b3/packages/keysmith/src/derive.ts#L367)

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

A keyspace-key holder walks the piece's whole scope path. A scope holder walks
the rest of the path below its own scope — which works only if its scope is the
piece's scope 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.

Pass the grant that delivered the key as `holding` where you have it: then the
piece's owner and keyspace are checked against the grant too, so a key from the
wrong keyspace fails here with a clear reason rather than later inside FEE.

## Parameters

| Parameter | Type | Default value |
| ------ | ------ | ------ |
| `node` | [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | `undefined` |
| `envelopeMetadata` | [`PieceMetadata`](/reference/filoz/keysmith/type-aliases/piecemetadata/) \| [`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/) | `'keyspace'` |

## Returns

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

## Throws

If the piece is not in this format, belongs to another owner or keyspace
than the grant, the held scope does not contain the piece's scope, or the held
piece is a different piece.