requests bees binance signed in Nestjs
When interacting with the binance bees that uses the Backand Nestjs, it is essential to obtain required switches signed and prevent unauthorized access. In this article, we will provide guidelines for setting the signed endpoints/bees/v3/Order ‘and Order/Testo.
Why are signed keys needed?
Binance API requires an API key valid to complete certain operations. However, by default, the key is not signed, which makes it vulnerable to unauthorized access. To solve this problem, you will have to generate an application of the application (also known as Key-Pubblica-Pubblica) and use it to sign your requests.
Signed Keys
**
To get a valid signature, follow these steps:
- Create a Binance developer account : if you haven’t already done so, register for an account with a binance developer.
- Generate Secret : Once you insert in the dashboard of your developer, open the API key card and click “Generate new secret of the app”. You can choose from the Json (JWT) or the server token via the public encryption key (SSEK).
- Select an algorithm : select the right algorithm for the generation of signed keys.
- Use the Secret App App in the Backand Nestjs : In the Nestjs application, it matters the
@Nestjs/JWT
and use it to create a certification service.
Expel for example
Here is an example of an example of an endpoint key signed /bees/v3/Order 'and' Order/Testo Endpoint using JWT:
Dictiloscript
Import {injectable} of '@Nestjs/Municipality';
Import * like jwt from "jsonwebtoken";
@Injectable ()
Authervice export class {
Private Appsecret: string;
CONSTRUTOR () {
This.Appsecret = Process.env.binance_app_secret;
}
Async Genettattoken (User: User): Promise
Const Payload = {User};
Return jwt.sign (useful load, this.appsecret, {Scadein: '1h'});
}
Async Verifytken (Token: String): Promise
Attempt {
Const Decoded = JWT.Verify (Token, This.Appsecret);
Return decoded.user as a user;
} Catch (error) {
Return null;
}
}
Async Createorder (User: User, Order: Orderinput): Promise
Const Token = Wait for authservice.generatetoken (user);
Const Payload = {User, Order};
Const signature = jwt.sign (useful load, this.appsecret, {Scadein: '1h'});
Return {... order, signature};
}
}
In the example above:
- We generate a sign signed by usingJsonwebtoken
and we store it as an environmental variable.
- When we create a new order, we check the user's marker with the key signed to guarantee authenticity.
Assembly all this
Here is an updated version of the Nestjs App, which includes the signed keys:
Dictiloscript
Import {Module} from '@Nestjs/Municipality';
Import {Appcontroller} from './App.Controller';
Import {appservice} from './app.service';
Import {Autorservice} from './auth.service';
@Module ({
Controller: [Appcontroller],
Suppliers: [AuthService, Appservice],
})
Appmodule export class {}
` Dictiloscript
Import Express Express;
Import of the self -controller from ‘./auth.controler’;
Import the authervice from ‘./auth.service’;
Const App = Express ();
App.use (Express.json ());
App.
Const Order = req.body;
Attempt {
// Create a new order with a key signed
Const token = wait for authservice.generatetoken ({User: ‘Your_user_name’});
Const Payload = {User: ‘Your_user_name’, Order};
Const signature = jwt.sign (Payload, ‘Your_App_Secret_key’);
Return Res.status (201) .json ({
Message: “Orders creates correctly!”,
Data: {
… an order,
signature,
},
});
} Catch (error) {
Console.error (error);
Returns Res.status (500).