|
| 1 | +# Ed25519 Authentication with Binance APIs in Node.js |
| 2 | + |
| 3 | +## Creating Ed25519 Keys |
| 4 | + |
| 5 | +Officially, binance recommends downloading and running a key generator from their repo. Guidance for this can be found on the binance website when trying to add a new Ed25519 API key, or in their GitHub repository: https://github.com/binance/asymmetric-key-generator |
| 6 | + |
| 7 | + |
| 8 | +## Using the Ed25519 public key to get an API key from Binance |
| 9 | + |
| 10 | +Once created, keep your **private key** completely secret! The **public** key needs to be provided to binance when creating new API credentials with the "Self-generated" option. |
| 11 | + |
| 12 | +Your public key should look something like this: |
| 13 | + |
| 14 | +```pem |
| 15 | +-----BEGIN PUBLIC KEY----- |
| 16 | +lkn123bx123x+7lkmlkn123bx123xAMDO/lkm123x= |
| 17 | +-----END PUBLIC KEY----- |
| 18 | +``` |
| 19 | + |
| 20 | +Submit this in the "Upload public key" form, shown when creating a new API key on binance and choosing the "self-generated" option. |
| 21 | + |
| 22 | +Note: the "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" header & footer can be included. |
| 23 | + |
| 24 | +After using the public key to create a new API key, you will be given an API Key such as the following: |
| 25 | + |
| 26 | +``` |
| 27 | +mlk2mx3l12m3lxk1m3lxk1m3l1k2mx3l12km3xl1km23x1l2k3mx1l2km3x |
| 28 | +``` |
| 29 | + |
| 30 | +This is the first piece, used as the "apiKey" in the [rest-private-ed25519.ts](./rest-private-ed25519.ts) example. |
| 31 | + |
| 32 | +## Using the Ed25519 private key for Ed25519 authentication with binance APIs in Node.js |
| 33 | + |
| 34 | +Your private key, if generated with the above steps, should look something like this (but with much more text): |
| 35 | + |
| 36 | +```pem |
| 37 | +-----BEGIN PRIVATE KEY----- |
| 38 | +lx1k2m3xl12lkm2l1kmx312312l3mx1lk23m |
| 39 | +-----END PRIVATE KEY----- |
| 40 | +``` |
| 41 | + |
| 42 | +This is your secret, you should **never** share this with anyone, not even binance! Treat this like a password. |
| 43 | + |
| 44 | +As part of this authentication process, your private key is used to generate a signature. This SDK handles this process automatically for you. Ed25519 authentication is automatically detected if the "api_secret" parameter contains the words "PRIVATE KEY", such as the header shown in the example above. |
| 45 | + |
| 46 | +From here, simply use the key provided by binance as the `api_key` parameter and your private key (with the header) as the `api_secret` parameter. |
| 47 | + |
| 48 | +Based on the above example, the following would prepare the main REST client using the above credentials: |
| 49 | + |
| 50 | +```typescript |
| 51 | +const ed25519PrivateKey = ` |
| 52 | +-----BEGIN PRIVATE KEY----- |
| 53 | +lkmlkm123lkms1s12s+lkmlkm123lkms1s12s |
| 54 | +-----END PRIVATE KEY----- |
| 55 | +`; |
| 56 | + |
| 57 | +const ed25519APIKey = 'lkmlkm123lkms1s12slkmlkm123lkms1s12slkmlkm123lkms1s12s'; |
| 58 | + |
| 59 | + |
| 60 | +const client = new MainClient({ |
| 61 | + api_key: ed25519APIKey, |
| 62 | + api_secret: ed25519PrivateKey, |
| 63 | + beautifyResponses: true, |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +The rest is automatic - just continue using the SDK as you would normally. It will automatically handle signing requests using Ed25519 for you. |
| 68 | + |
| 69 | +For a complete example, refer to the [rest-private-ed25519.ts](./rest-private-ed25519.ts) file on GitHub. |
0 commit comments