This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (87 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const NamiDelegationError = require( "./errors/DelegationError" ).NamiDelegationError;
const NamiInterface = require( "./cardano/nami" );
/**
*
* @param {string} pool_id_bech_32 pool id to delegate to
* @param {string} blockfrost_project_id refer to https://blockfrost.io to get your
* @param {object | undefined} options alter the default behavior in case nami has not been enabled before calling the function,
* if window.cardano.isEnabled() returns true you should not worry about this parameter
* property to modify are:
*
* onNamiNotEnabled_shouldThrow, if true everything else is ignored and throws a NamiDelegationError if nami is not enabled;
*
* onNamiNotEnabled_shouldRequestEnable: if true calls window.cardano.enable() once
*
* @returns the delegation transaction hash you can use to check the transaction state on cardano mainnet explorers
*/
module.exports.delegateUsingNami = async function(
pool_id_bech_32,
blockfrost_project_id,
options = {}
)
{
// -------------- env check -------------- //
if( typeof window === "undefined" ) throw new NamiDelegationError("delegateUsingNami can work only in a browser context", -1);
if( !window.cardano ) throw new NamiDelegationError("unable to detect the Nami browser extension", 1);
// -------------- handle unabled Nami -------------- //
if( typeof options !== "object" ) throw NamiDelegationError("invaid option");
const _opt = {
// default options
onNamiNotEnabled_shouldThrow: true,
onNamiNotEnabled_shouldRequestEnable: true,
// overriding if any
...options
}
if( !( await window.cardano.isEnabled() ) )
{
console.warn("Nami wallet is present as browser extension but is not connected to this website");
if( _opt.onNamiNotEnabled_shouldThrow )
{
throw new NamiDelegationError("nami wallet has not been enabled", 3);
}
if( _opt.onNamiNotEnabled_shouldRequestEnable )
{
window.cardano.enable();
}
if( _opt.onNamiNotEnabled_shouldRequestEnable === false && _opt.onNamiNotEnabled_shouldThrow == false )
{
// explicit request to do nothing
// in any case if nami is not enabled no transaction can be done
// exit silently
return "";
}
}
// -------------- input check -------------- //
// removes any potential whitespace
const _pool_id = formatString( pool_id_bech_32 );
if( !_pool_id.startsWith("pool") )
{
throw new NamiDelegationError(
"pool_id_bech_32 must start with the \"pool\" word, pheraps you provvided the hex pool Id? pool_id_bech_32 was: " + pool_id_bech_32 ,
2
);
}
// removes any potential whitespace
const _blockfrost = formatString( blockfrost_project_id );
// -------------- magic starts here -------------- //
NamiInterface.init( _blockfrost );
// returns the transaction hash
return (
await NamiInterface.submitTransaction(
await NamiInterface.signTransaction(
await NamiInterface.createDelegationTransaction(
_pool_id
)
)
)
);
};
function formatString( str )
{
if( typeof str !== "string" )
{
throw new Error("can't format a non string value, input: " + str );
}
// remove any potential whitespace
return ( str.trim().split(" ").join("").split("\n").join("") );
}