bitpay / nodejs-bitpay-client

NodeJs implementation for the BitPay Cryptographically Secure RESTful API
MIT License
48 stars 42 forks source link

cannot create token, not working from init stage #59

Closed jackykwandesign closed 1 year ago

jackykwandesign commented 2 years ago

By running node node_modules/bitpay-sdk/dist/setup/BitPaySetup.js i can only get a private key, no pair code, no SIN, no merchant or payout token generated

image image

And in the dashboard, i cannot found my SIN or clientId And there should be someway to direct generate this info in the dashboard, like generating the API token

jackykwandesign commented 2 years ago

Right now, i just use Bitauth to generate SIN i get public key, private key, sin from result

var bitauth = require('bitauth');
const main = () =>{
    const newSin = bitauth.generateSin()
    console.log("newSin", newSin)
}

main()

The key is not the same as below since i generate for this picture only image

And i use API test.bitpay.com/tokens with SIN to get merchant token and approve in the dashboard image image

And i can direct access API /tokens/:token/sins, which prove it works in API level Before i send API, i signed ecdsa with this website https://kjur.github.io/jsrsasign/sample/sample-ecdsa.html image

image

But then i try Bitpay-sdk again, it still give me UnhandledPromiseRejection, which i already handle trycatch in my code block, so there is something broken in sdk image image

jackykwandesign commented 2 years ago

Finally, i do this from the beginning

  1. Gen SIN + key pair
  2. Get Token
  3. Approve Token
  4. Use token in code API call
  5. Sign the API manually (for anyone confuse with signature, this is how to make it in Nodejs, and the result signature is same as i generate from last website, which that website is a demo of this lib jsrsasign, check script tag in HTML can see the whole demo )

    import * as rs from "jsrsasign"
    const testAPI = async () => {
    try {
        let baseURL = "https://test.bitpay.com"
        let finalURL = `${baseURL}/tokens/${process.env.BITPAY_MERCHANT_TOKEN}/sins`
        console.log("finalURL", finalURL)
    
        const sig = new rs.KJUR.crypto.Signature({ alg: "SHA256withECDSA" })
        sig.init({ d: process.env.BITPAY_PRIVATE_KEY, curve: "secp256k1" })
        sig.updateString(finalURL)
        var sigValueHex = sig.sign()
        console.log("sigValueHex", sigValueHex)
        console.log(
            "process.env.BITPAY_PUBLIC_KEY",
            process.env.BITPAY_PUBLIC_KEY,
        )
        const result = await axios.get(finalURL, {
            headers: {
                "Content-Type": "application/json",
                "X-Accept-Version": "2.0.0",
                "X-Identity": process.env.BITPAY_PUBLIC_KEY,
                "X-Signature": sigValueHex,
            },
        })
        console.log("result", result.data)
    } catch (error) {
        console.log("error", error)
    }
    }

And the code suppose to be work

However, it still not working and return a cloudflare page in response image image

And i realise the problem is my remote machine IP is dirty in Cloudflare record due to some loading test sign project I start a new VM win10 and use postman to send same API request, and result different from local machine

Local postman: image

VM postman with dirty IP image

So i just open a instance in AWS and all work, including this Nodejs-SDK So maybe it will be better if SDK client can show some error message about 403, instead of unhandled reject promise

Case close ,thanks

kameshdhana commented 1 year ago

Leave out a breakpoint for console.log() of merchantPairCode in BitPaySetup.js & then it'll give you the pairing code. Sadly it's not provided with the config.json

bobbrodie commented 1 year ago

Closing due to age, but we are actively working on and releasing SDKs so please don't hesitate to reach out if this is still something you need. Thank you!