doc: more docs
parent
6c8c5fd111
commit
91d403221f
78
README.md
78
README.md
|
@ -22,7 +22,7 @@ const connector = new TonhubConnector({ testnet: true });
|
|||
### Creating session
|
||||
|
||||
```typescript
|
||||
let session = await connector.createNewSession({
|
||||
let session: TonhubCreatedSession = await connector.createNewSession({
|
||||
name: 'Your app name',
|
||||
url: 'Your app url'
|
||||
});
|
||||
|
@ -36,7 +36,7 @@ const sessionLink = session.link;
|
|||
### Await session confirmation
|
||||
|
||||
```typescript
|
||||
const session = await connector.awaitSessionReady(sessionId, 5 * 60 * 1000); // 5 min timeout
|
||||
const session: TonhubSessionAwaited = await connector.awaitSessionReady(sessionId, 5 * 60 * 1000); // 5 min timeout
|
||||
|
||||
if (session.state === 'revoked' || session.state === 'expired') {
|
||||
// Handle revoked or expired session
|
||||
|
@ -62,5 +62,79 @@ if (session.state === 'revoked' || session.state === 'expired') {
|
|||
}
|
||||
```
|
||||
|
||||
### Check session validity
|
||||
After session established it is useful to check from time to time that session wasn't revoked
|
||||
|
||||
```typescript
|
||||
const session: TonhubSessionState = await connector.getSessionState(sessionId);
|
||||
```
|
||||
|
||||
### Request transaction
|
||||
|
||||
```typescript
|
||||
// Request body
|
||||
const request: TonhubTransactionRequest = {
|
||||
seed: sessionSeed, // Session Seed
|
||||
appPublicKey: walletConfig.appPublicKey, // Wallet's app public key
|
||||
to: 'EQCkR1cGmnsE45N4K0otPl5EnxnRakmGqeJUNua5fkWhales', // Destination
|
||||
value: '10000000000', // Amount in nano-tons
|
||||
timeout: 5 * 60 * 1000, // 5 minut timeout
|
||||
stateInit: '....', // Optional serialized to base64 string state_init cell
|
||||
text: 'Hello world', // Optional comment. If no payload specified - sends actual content, if payload is provided this text is used as UI-only hint
|
||||
payload: '....' // Optional serialized to base64 string payload cell
|
||||
};
|
||||
const response: TonhubTransactionResponse = await connector.requestTransaction(request);
|
||||
if (response.type === 'rejected') {
|
||||
// Handle rejection
|
||||
} else if (response.type === 'expired') {
|
||||
// Handle expiration
|
||||
} else if (response.type === 'invalid_session') {
|
||||
// Handle expired or invalid session
|
||||
} else if (response.type === 'success') {
|
||||
// Handle successful transaction
|
||||
const externalMessage = response.response; // Signed external message that was sent to the network
|
||||
} else {
|
||||
throw new Error('Impossible');
|
||||
}
|
||||
```
|
||||
|
||||
### Request signature
|
||||
In tonhub user user signs both text and binary payload, but only text is presented to the user. For future compatibility we recommend to prefix your cell binary payload with `0x00000000`.
|
||||
```typescript
|
||||
|
||||
const payloadToSign = Buffer.concat([Buffer.from([0, 0, 0, 0]), Buffer.from('Some random string')]);
|
||||
const payload = beginCell()
|
||||
.storeBuffer(payloadToSign)
|
||||
.endCell()
|
||||
.toBoc({idx:false})
|
||||
.toString('base64');
|
||||
const text = 'Please, sign our terms or service and privacy policy';
|
||||
|
||||
// Request body
|
||||
const request: TonhubSignRequest = {
|
||||
seed: sessionSeed, // Session Seed
|
||||
appPublicKey: walletConfig.appPublicKey, // Wallet's app public key
|
||||
timeout: 5 * 60 * 1000, // 5 minut timeout
|
||||
text: 'Hello world', // Text to sign, presented to the user.
|
||||
payload: payload // Optional serialized to base64 string payload cell
|
||||
};
|
||||
const response: TonhubSignResponse = await connector.requestSign(request);
|
||||
if (response.type === 'rejected') {
|
||||
// Handle rejection
|
||||
} else if (response.type === 'expired') {
|
||||
// Handle expiration
|
||||
} else if (response.type === 'invalid_session') {
|
||||
// Handle expired or invalid session
|
||||
} else if (response.type === 'success') {
|
||||
// Handle successful transaction
|
||||
const signature = response.signature;
|
||||
|
||||
// You can check signature on the backend with TonhubConnector.verifySignatureResponse
|
||||
let correctSignature = TonhubConnector.verifySignatureResponse({ signature: signature, config: walletConfig });
|
||||
} else {
|
||||
throw new Error('Impossible');
|
||||
}
|
||||
```
|
||||
|
||||
# License
|
||||
MIT
|
Loading…
Reference in New Issue