Nuvo SDK
Change log
SDK install
npm install @metis.io/middleware-client
To enable a user of a website/app to log into Nuvo and access its functionalities such as checking balance and sending transaction, follows these high level steps:
- Acquire access token using Oauth2 client
- Use the acquired access token to initialize a PolisProvider or PolisClient Object
- Use the methods provided by PolisProvider or PolisClient to execute user's Nuvo account's functionalities.
First we use this code example to demostrate how to initialize an Oauth2 client and acquire an access token from a user's account:
Oauth2 client
import & create
import { Oauth2Client } from '@metis.io/middleware-client'
const oauth2Client = new Oauth2Client()
start oauth
oauth2Client.startOauth2(`APPID`, `RETURN URL`, `newWindow`,`switchAccount`);
The APPID
and RETURN URL
can get from Polis Developer User page
request access token & refresh token on RETURN URL page in backend
get(`https://polis.metis.io/api/v1/oauth2/access_token?app_id=${this.appid}&app_key=${this.appsecret}&code=${this.code}`)
res => {
if (res.status == 200 && res.data && res.data.code == 200) {
}
}
refresh token
const oauth2User = await oauth2Client.refreshTokenAsync(`APPID`, `RefreshToken`)
get user info
const userInfo = await oauth2Client.getUserInfoAsync(`AccessToken`)
{
'display_name': '',
'username': '',
'email': '',
'eth_address': '',
'last_login_time': timestamp
}
oauth logout
logout(appId:string, accessToken:string, refreshToken:string="").then(res => {
})
.catch(res=>{
})
Once we acquired the access token, we can use either a PolisProvider object that is compatible with ethers provider, or a PolisClient object to access user account's functionalities.
1、Use Ethers BrowserProvider
step 1: IPolisProviderOpts
const opts: IPolisProviderOpts = {
apiHost: 'https://api.nuvosphere.io/',
oauthHost?: "",
token?: {accessToken},
chainId: 4,
}
const polisprovider = new PolisProvider(opts)
step 2: Ethers Web3 Provider
const ethersProvider = new ethers.BrowserProvider(polisprovider)
2、 Use Polis Client
step 1
const clientOps:IPolisClientOpts = {
chainId: CHAIN_ID,
appId:APP_ID,
apiHost :apiHost
}
client = new PolisClient(clientOps);
client.web3Provider.getBalance("address")
client.connect(oauthInfo);
await client.connect(oauthInfo);
Polis Client Events
client.on('debug', function (data) {
console.log('debug data:%s', JSON.stringify(data));
})
client.on('error', function (data) {
console.log('error:', data instanceof Error)
});
client.on('chainChanged', (chainId) => {
console.log('polis-client print chainId =>', chainId);
});
client.on('accountsChanged', (account) => {
console.log('polis-client print account =>', account);
});
step 2: get Web3 Provider
const ethersProvider=client.web3Provider
const singer = await ethersProvider.getSinger();
singer.signMessage("aa");
const daiAddress = this.contract.address;
const daiAbi = [
"function name() view returns (string)",
"function symbol() view returns (string)",
"function balanceOf(address) view returns (uint)",
"function transfer(address to, uint amount)",
"event Transfer(address indexed from, address indexed to, uint amount)"
];
const daiContract = await client.getContract(daiAddress, daiAbi);
await daiContract['name']();