deso-protocol
Client side typescript/javascript SDK for building web3 applications for the DeSo blockchain.
Installation
npm i deso-protocol
Configuration
import { configure } from 'deso-protocol';
configure({
spendingLimitOptions: {
GlobalDESOLimit: 1 * 1e9
TransactionCountLimitMap: {
BASIC_TRANSFER: 2,
SUBMIT_POST: 4,
},
}
nodeURI: 'https://mynode.com',
redirectURI: 'https://mydomain.com/my-redirect-path',
appName: 'My Cool App',
MinFeeRateNanosPerKB: 1000,
storageProvider?: Storage | AsyncStorage;
identityPresenter?: (url: string) => void;
})
Usage
Identity: (logging in and out, creating new accounts, etc)
import { identity } from 'deso-protocol';
identity.subscribe((state) => {
const event = state.event;
const currentUser = state.currentUser;
const alernateUsers = state.alternateUsers;
});
await identity.login();
await identity.logout();
identity.setActiveUser(publicKey);
await identity.jwt();
const tx = await axios.post('https://node.deso.org/api/v0/submit-post');
const submittedTx = await identity.signAndSubmit(tx);
const postTransaction = await axios.post(
'https://node.deso.org/api/v0/submit-post'
);
const signedTx = await identity.signTx(postTransaction.TransactionHex);
const submittedTx = await identity.submitTx(signedTx);
const hasPermission = identity.hasPermissions({
TransactionCountLimitMap: {
SUBMIT_POST: 1,
},
});
if (!hasPermissions) {
await identity.requestPermissions({
TransactionCountLimitMap: {
SUBMIT_POST: 1,
},
});
}
const encryptedMessageHex = await identity.encryptMessage(
recipientPublicKeyBase58Check,
plaintextMsg
);
const decryptedMessagePlaintext = await identity.decryptMessage(
message,
accessGroups
);
Data: fetching data from a node
import { getUsersStateless, getPostsStateless } from 'deso-protocol';
const users = await getUsersStateless({
PublicKeysBase58Check: [key1, key2, ...rest],
});
const posts = await getPostsStateless({ NumToFetch: 20 });
See the backend api documentation for reference.
See an exhaustive list of the available data fetching functions here.
Transactions: Writing data to the blockchain
The deso-protocol library will handle signing and submitting transactions for
confirmation for you. All you need to do is construct them by providing the raw
data.
import { submitPost } from 'deso-protocol';
const txInfo = submitPost({
UpdaterPublicKeyBase58Check: currentUser.publicKey,
BodyObj: {
Body: 'My first post on DeSo!',
ImageURLs: [],
VideoURLs: [],
},
});
See the transaction construction api documentation for reference.
See an exhaustive list of the available transaction construction functions here
React Native (beta)
React native support is a work in progress, but there is a beta version
available if you'd like to test to it out. You will need to run react native
version 0.71.7
or later to ensure BigInt
support is available.
Installation
npm i deso-protocol@beta
There a few peer dependencies that are required for everything to work smoothly.
npm i react-native-get-random-values react-native-webview react-native-webview-crypto text-encoding @react-native-async-storage/async-storage @ethersproject/shims
NOTE: you may need to install native modules for the target platform. For iOS you can do this via cocoapods:
cd ios && pod install && cd -
You will need to add these shims to your application:
import 'react-native-get-random-values';
import '@ethersproject/shims';
import { TextDecoder, TextEncoder } from 'text-encoding';
if (typeof global.TextEncoder === 'undefined') {
global.TextEncoder = TextEncoder;
}
if (typeof global.TextDecoder === 'undefined') {
global.TextDecoder = TextDecoder;
}
deso-protocol
requires the web crypto APIs, which are provided via
the
react-native-webview-crypto
package. TL;DR you need to render a hidden webview at the top level of your app
to proxy crypto method calls to, so please pay special attention to their usage
documentation.
And finally you will need to configure deso-protocol
with a redirectURI
, identityPresenter
, and storageProvider
.
If you are using Expo it is very easy to set things up.
import { configure } from 'deso-protocol';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as AuthSession from 'expo-auth-session';
import * as WebBrowser from 'expo-web-browser';
configure({
redirectURI: AuthSession.makeRedirectUri(),
identityPresenter: async (url) => {
const result = await WebBrowser.openAuthSessionAsync(url);
if (result.type === 'success') {
identity.handleRedirectURI(result.url);
}
},
storageProvider: AsyncStorage,
});
Contributing
Pull requests are welcome!
Setup
git clone ...
cd deso-js
Useful workflows
npm run test
- Link local changes into another project
npm run link
cd $your_project_root_dir
npm link deso-protocol