All direct function call to the smart contract would authentication with the near blockchain. To generate auth token please look at the following script,
Copy const nearAPI = require ( 'near-api-js' )
const { Base64 } = require ( 'js-base64' )
const authToken = async (accountId , signer , networkId) => {
try {
const arr = new Array (accountId)
for ( var i = 0 ; i < accountId . length ; i ++ ) {
arr[i] = accountId .charCodeAt (i)
}
const msgBuf = new Uint8Array (arr)
const signedMsg = await signer .signMessage (
msgBuf ,
accountId ,
networkId
)
const pubKey = Buffer .from ( signedMsg . publicKey .data) .toString ( 'hex' )
const signature = Buffer .from ( signedMsg .signature) .toString ( 'hex' )
const payload = [accountId , pubKey , signature]
return Base64 .encode ( payload .join ( '&' ))
} catch (err) {
console .log (err)
return null
}
}
const main = async () => {
const config = {
networkId : 'testnet' ,
nodeUrl : 'https://rpc.testnet.near.org' ,
walletUrl : 'https://wallet.testnet.near.org' ,
appName : 'Paras Testnet' ,
contractName : `paras-token-v2.testnet`
}
try {
// Initializing nearAPI
// Login and init contract
const keyStore = new nearAPI . keyStores .UnencryptedFileSystemKeyStore (
` ${ process . env . HOME } /.near-credentials/`
)
const signer = new nearAPI .InMemorySigner (keyStore)
const connection = await nearAPI .connect ({
deps : {
keyStore : keyStore ,
} ,
... config ,
})
const account_id = 'orang.testnet'
const account = await connection .account (account_id)
const authorizationHeader = await authToken (account_id , signer , config .networkId)
console .log ( `Authorization Header : ${ authorizationHeader } ` )
} catch (err) {
throw err
}
}
main ()