1 | const Ledger = require('../index.js')
|
2 | const crypto = require('brave-crypto')
|
3 | const test = require('tape')
|
4 |
|
5 | const options = { debugP: true, version: 'v2', environment: 'staging' }
|
6 |
|
7 | test('fetchPublisherInfo', async (t) => {
|
8 | t.plan(3)
|
9 |
|
10 |
|
11 | const fetchOptions = {
|
12 | debugP: true,
|
13 | version: 'v2',
|
14 | environment: 'staging',
|
15 | server: 'https://publishers-staging.basicattentiontoken.org'
|
16 | }
|
17 |
|
18 | const client = new Ledger(null, fetchOptions)
|
19 | client.sync(function () {
|
20 | client.fetchPublisherInfo(function (err, result) {
|
21 | t.false(err)
|
22 | t.true(Array.isArray(result))
|
23 | t.equal(result[0].length, 3)
|
24 | })
|
25 | })
|
26 | })
|
27 |
|
28 | test('recoverWallet', async (t) => {
|
29 | t.plan(6)
|
30 | const client = new Ledger(null, options)
|
31 |
|
32 | client.sync(function () {
|
33 | const signingKey = client.getKeypair()
|
34 | const passPhrase = client.getWalletPassphrase()
|
35 | const addresses = client.getWalletAddresses()
|
36 |
|
37 | const client2 = new Ledger(null, options)
|
38 | client2.sync(function () {
|
39 | const signingKey2 = client2.getKeypair()
|
40 | const addresses2 = client2.getWalletAddresses()
|
41 |
|
42 | t.notEqual(crypto.uint8ToHex(signingKey.secretKey), crypto.uint8ToHex(signingKey2.secretKey))
|
43 | t.notEqual(crypto.uint8ToHex(signingKey.publicKey), crypto.uint8ToHex(signingKey2.publicKey))
|
44 | t.notDeepEqual(addresses, addresses2)
|
45 |
|
46 | client2.recoverWallet(null, passPhrase.join(' '), function () {
|
47 | const signingKey3 = client2.getKeypair()
|
48 | const addresses3 = client2.getWalletAddresses()
|
49 |
|
50 | t.equal(crypto.uint8ToHex(signingKey.secretKey), crypto.uint8ToHex(signingKey3.secretKey))
|
51 | t.equal(crypto.uint8ToHex(signingKey.publicKey), crypto.uint8ToHex(signingKey3.publicKey))
|
52 | t.deepEqual(addresses, addresses3)
|
53 | })
|
54 | })
|
55 | })
|
56 | })
|
57 |
|
58 | test('balance', async (t) => {
|
59 | t.plan(2)
|
60 | const client = new Ledger(null, options)
|
61 |
|
62 | client.sync(function () {
|
63 | client.getWalletProperties(function (err, resp) {
|
64 | t.false(err)
|
65 | t.equal(resp.probi, '0')
|
66 | })
|
67 | })
|
68 | })
|
69 |
|
70 | test('promotion', async (t) => {
|
71 | t.plan(7)
|
72 |
|
73 | const client = {
|
74 | sync: (callback) => {
|
75 | callback()
|
76 | },
|
77 | getPromotion: (lang, forPaymentId, callback) => {
|
78 | const resp = {
|
79 | promotionId: '5787de72e-174d-4fb3-bdf6-2e70b2b0ac86'
|
80 | }
|
81 | callback(null, resp)
|
82 | },
|
83 | setPromotion: (promotionId, callback) => {
|
84 | const resp = {
|
85 | probi: '10000000000000000000'
|
86 | }
|
87 | callback(null, resp)
|
88 | },
|
89 | getWalletProperties: (callback) => {
|
90 | const resp = {
|
91 | probi: '10000000000000000000'
|
92 | }
|
93 | callback(null, resp)
|
94 | },
|
95 | getPaymentId: () => {}
|
96 | }
|
97 |
|
98 | const client2 = {
|
99 | getPromotion: (lang, paymentId, callback) => {
|
100 | const err = {
|
101 | message: '404'
|
102 | }
|
103 | callback(err)
|
104 | }
|
105 | }
|
106 |
|
107 | client.sync(function () {
|
108 | client.getPromotion(null, null, function (err, resp) {
|
109 | t.false(err)
|
110 | t.true(resp.hasOwnProperty('promotionId'))
|
111 | const promotionId = resp.promotionId
|
112 | client.setPromotion(promotionId, function (err, resp) {
|
113 | t.false(err)
|
114 | t.true(resp.hasOwnProperty('probi'))
|
115 | const grantProbi = resp.probi
|
116 | client.getWalletProperties(function (err, resp) {
|
117 | t.false(err)
|
118 | t.equal(resp.probi, grantProbi)
|
119 | client2.getPromotion(null, client.getPaymentId(), function (err, resp) {
|
120 | t.true(err)
|
121 | })
|
122 | })
|
123 | })
|
124 | })
|
125 | })
|
126 | })
|