Skip to content

Commit

Permalink
Update sign.mdx (#760)
Browse files Browse the repository at this point in the history
added convenient check if proof valid
  • Loading branch information
Jeanclaudeaoun authored Sep 17, 2024
1 parent 4b0544b commit d2cce2b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/develop/dapps/ton-connect/sign.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,50 @@ export function useBackendAuth() {
</details>

## Backend Example
<details>
<summary>Check if Proof Valid (Next.js)</summary>
```tsx
'use server'
import {Address, Cell, contractAddress, loadStateInit, TonClient4} from '@ton/ton'


export async function isValid(proof, account) {
const payload = {
address: account.address,
public_key: account.publicKey,
proof: {
...proof,
state_init: account.walletStateInit
}
}
const stateInit = loadStateInit(Cell.fromBase64(payload.proof.state_init).beginParse())
const client = new TonClient4({
endpoint: 'https://mainnet-v4.tonhubapi.com'
})
const masterAt = await client.getLastBlock()
const result = await client.runMethod(masterAt.last.seqno, Address.parse(payload.address), 'get_public_key', [])
const publicKey = Buffer.from(result.reader.readBigNumber().toString(16).padStart(64, '0'), 'hex')
if (!publicKey) {
return false
}
const wantedPublicKey = Buffer.from(payload.public_key, 'hex')
if (!publicKey.equals(wantedPublicKey)) {
return false
}
const wantedAddress = Address.parse(payload.address)
const address = contractAddress(wantedAddress.workChain, stateInit)
if (!address.equals(wantedAddress)) {
return false
}
const now = Math.floor(Date.now() / 1000)
if (now - (60 * 15) > payload.proof.timestamp) {
return false
}
return true
}

```
</details>
You can review our [example](https://github.com/ton-connect/demo-dapp-with-react-ui/tree/master/src/server) showcasing the key methods:
- [generatePayload](https://github.com/ton-connect/demo-dapp-with-react-ui/blob/master/src/server/api/generate-payload.ts): Generates a payload for ton proof
- [checkProof](https://github.com/ton-connect/demo-dapp-with-react-ui/blob/master/src/server/api/check-proof.ts): Checks the proof and returns an access token.
Expand Down

0 comments on commit d2cce2b

Please sign in to comment.