# Approve SRR By Commitment

## `approveSRRByCommitment()`

Approve transfer to those who knows the secret value set in approveSRRByCommitment() under a commit/reveal scheme.

{% hint style="info" %}
The approveSRRByCommitment() function alone does not initiate the transfer of the SRR. You need to call [Transfer SRR Ownership by RevealHash](/startrail-api/transfer-srr-ownership-by-revealhash.md) to finalize the transfer.
{% endhint %}

### How to complete ownership transfer with transferByReveal

When you provide a `preimage` for `approveSRRByCommitment`, you must hash the `preimage` using `keccak256` later on and provide the resulting hash as the `revealHash` parameter to the [`transferByReveal` endpoint](https://api-stg.startrail.startbahn.jp/api#/default/SRRController_transferByReveal) in order to complete the transfer. This is particularly useful in cases where the sender only knows the recipient's email, while the Dapp system, functioning as an intermediary, can determine the Ethereum address associated with that email.

When you provide a `revealHash` that has been hashed with keccak256 for `approveSRRByCommitment`, you can directly use it as the revealHash parameter for the [`transferByReveal` endpoint](https://api-stg.startrail.startbahn.jp/api#/default/SRRController_transferByReveal) to finalize the transfer.

### How to generate revealHash

```
import { bufferToHex, keccak256 } from 'ethereumjs-util'

const revealHash = bufferToHex(keccak256(Buffer.from('message')))
```

{% hint style="danger" %}
Please take care to ensure that the preimage remains sufficiently obscure to individuals who are not authorized. If it becomes predictable, anyone possessing this information will have the capability to execute the transfer.
{% endhint %}

## Method parameters

| Variable                      | Type               | Description                                                                                                                                                                                                                                                                                                     |
| ----------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `startrailLUWContractAddress` | `string`           | The address of LicensedUserWallet(LUW) contract. Sets it when you want to execute transaction by the LUW contract address                                                                                                                                                                                       |
| `contractAddress`             | `string`           | The address of collection contract. Sets it when you want to associate collection contract address with the SRR.                                                                                                                                                                                                |
| `preimage`                    | `string`           | A string value used to reserve the transfer. Anyone who knows the value is entitled to complete the transfer. If this value is provided along with an email, confirmation emails will be sent to the email address at the time of transfer reservation and completion. This value is exclusive to `revealHash`. |
| `revealHash`                  | `string`           | <p>A keccak256 hash value used to reserve the transfer. Anyone who knows the value is entitled to complete the transfer. This value is exclusive to <code>preimage</code>.<br><br>See the sample code for Generating the revealHash Value below.</p>                                                            |
| `metadata`                    | `TransferMetadata` | SRR Transfer Metadata conforming to the [Transfer Data schema](/metadata-schema/transfer.md)                                                                                                                                                                                                                    |
| `isHashPreimageEnabled`       | `boolean`          | (Deprecated. It will be removed soon) Setting true hashes the preimage with keccak256                                                                                                                                                                                                                           |

### TransferMetadata

| Variable          | Type     | Description                                                                     |
| ----------------- | -------- | ------------------------------------------------------------------------------- |
| `transferType`    | `string` | Select one of the followings "Primary sale", "Secondary sale", "Other transfer” |
| `remarks`         | `Lang`   | Write down whatever relevant to transfer ownership                              |
| `customHistoryId` | `number` | Custom History Id that is already registered in Startrail                       |

### Lang

| Variable | Type     | Description  |
| -------- | -------- | ------------ |
| `ja`     | `string` | Japanse text |
| `en`     | `string` | English text |

### Sample Code for Generating the revealHash Value

```typescript
import { keccak256 } from '@ethersproject/keccak256'
export const generateRevealHash = (preimage: string): string => {
  return keccak256(Buffer.from(preimage))
}

// OR

import { keccak256 } from 'ethereumjs-util'
export const generateRevealHash = (preimage: string): string => {
  return bufferToHex(keccak256(Buffer.from(preimage)))
}
```

### Parameters Example

```
await sdk.approveSRRByCommitment(
  {
    tokenId: '41052235',
    preimage: 'art@tuta.io',
    metadata: {
      transferType: "Primary sale",
      remarks: {
        en: "Reason for the transfer",
        ja: "移転の理由：日本語"
      },
      customHistoryId: 1,
    },
  }
)
// OR
await sdk.approveSRRByCommitment(
  {
    tokenId: '41052235',
    revealHash: '0e675a836831dd887e5ad4ce4e8365b979dcd2141536d69e3767c7c620bbfc1f',
    metadata: {
      transferType: "Primary sale",
      remarks: {
        en: "Reason for the transfer",
        ja: "移転の理由：日本語"
      },
      customHistoryId: 1,
    },
  }
)
```

## Returns

`Promise` will be returned which resolves with a `Response` object upon a successful confirmation. `false` will be returned when user flow is cancelled in such a case that a user closes the popup modal.

If the confirmation fails, the `Promise` will resolve with an {error} object that describes the failure.

`Promise<Response | false>`

{% hint style="warning" %}
The use of "txReceiptId" will soon be deprecated and removed.
{% endhint %}

### Response

| Variable      | Type     | Description                                             |
| ------------- | -------- | ------------------------------------------------------- |
| `txReceiptId` | `string` | ID to identify transaction details in Startrail-API DB. |

### Error

Custom `Error` objects. Refer to the [Error Catalogue](/startrail-sdk-js/errors.md) for possible data.

### Response Example

###

Example

```
{
  txReceiptId: 0
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.startrail.io/startrail-sdk-js/startrail-api-methods/approvesrrbycommitment.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
