JS SDK for Bifrost.
<script src="bifrost.min.js"></script>
Represents Bifrost session with params
parameters.
params
is an object with the following fields:
Name | Possible Values | Description |
---|---|---|
network |
test or live |
Stellar network to use |
issuingPublicKey |
Stellar public key | Public key of issuing account |
assetCode |
string |
Asset code of token to sell, ex. TOKE |
price |
string |
Maximum price of 1 assetCode token (in BTC or ETH depending which start* method is used) |
bifrostURL |
string |
URL of Bifrost server |
horizonURL |
string |
URL of Horizon server (do not use SDF's servers!) |
horizonAllowHttp |
boolean |
(Optional) If set to true allows HTTP connections to Horizon server. Useful for testing. |
preSaleMode |
boolean |
(Optional) If set to true , BTC/ETH tokens will not be traded to assetCode . User will end up with BTC/ETH in their Stellar account. |
Example:
var params = {
network: 'test',
horizonURL: 'https://horizon-testnet.stellar.org',
bifrostURL: 'http://localhost:8000',
assetCode: 'TOKE',
price: '1',
issuingPublicKey: 'GDGVTKSEXWB4VFTBDWCBJVJZLIY6R3766EHBZFIGK2N7EQHVV5UTA63C',
preSaleMode: false,
};
var session = new Bifrost.Session(params);
Starts Bitcoin session and returns a promise that resolves with Bitcoin address where to send funds and Stellar keypair. Once the session is started, no other session can be created using the same object. You need to create a new one.
Example:
session.startBitcoin(onEvent).then({address, keypair} => {
document.getElementById("address").innerText = "Waiting for a transaction...";
document.getElementById("address").innerText = address;
document.getElementById("public-key").innerText = keypair.publicKey();
document.getElementById("secret").innerText = keypair.secret();
})
onEvent
description can be found in the section below.
Starts Ethereum session and returns a promise that resolves with Ethereum address where to send funds and Stellar keypair. Once the session is started, no other session can be created using the same object. You need to create a new one.
Example:
session.startEthereum(onEvent).then({address, keypair} => {
document.getElementById("address").innerText = "Waiting for a transaction...";
document.getElementById("address").innerText = address;
document.getElementById("public-key").innerText = keypair.publicKey();
document.getElementById("secret").innerText = keypair.secret();
})
onEvent
description can be found in the section below.
onEvent
function you pass to startBitcoin
or startEthereum
function accepts one parameter event
described below:
event |
data |
Description |
---|---|---|
Bifrost.AccountCreatedEvent |
none | Sent when account is created |
Bifrost.TrustLinesCreatedEvent |
none | Sent when trust line is created |
Bifrost.AccountCreditedEvent |
none | Sent when account is credited |
Bifrost.PurchasedEvent (This will not be triggered in preSaleMode ) |
none | Sent when token is purchased |
Bifrost.ErrorEvent |
Error object |
Sent when asynchronous, non-recoverable error occured |
Example:
function onEvent(event, data) {
if (event == Bifrost.TransactionReceivedEvent) {
setStatus("Transaction received, creating account...");
} else if (event == Bifrost.AccountCreatedEvent) {
setStatus("Account created, creating trust lines...");
} else if (event == Bifrost.TrustLinesCreatedEvent) {
setStatus("Trust lines created, waiting for tokens...");
} else if (event == Bifrost.AccountCreditedEvent) {
setStatus("Account credited, exchanging...");
} else if (event == Bifrost.PurchasedEvent) {
setStatus("Congrats! TOKE purchased.");
} else if (event == Bifrost.ErrorEvent) {
setStatus("Error!");
// Send `data` to the log server.
}
}