/
memory.js
85 lines (77 loc) · 2.85 KB
/
memory.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
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Memory Account module
* @module @aeternity/aepp-sdk/es/account/memory
* @export MemoryAccount
* @example import MemoryAccount from '@aeternity/aepp-sdk/es/account/memory'
*/
import Account from './'
import * as Crypto from '../utils/crypto'
const secrets = new WeakMap()
async function sign (data) {
return Promise.resolve(Crypto.sign(data, secrets.get(this).secretKey))
}
async function address (format = Crypto.ADDRESS_FORMAT.api) {
return Promise.resolve(Crypto.formatAddress(format, secrets.get(this).publicKey))
}
/**
* Select specific account
* @alias module:@aeternity/aepp-sdk/es/account/memory
* @function
* @instance
* @rtype (keypair: {publicKey: String, secretKey: String}) => Void
* @param {Object} keypair - Key pair to use
* @param {String} keypair.publicKey - Public key
* @param {String} keypair.secretKey - Private key
* @return {Void}
* @example setKeypair(keypair)
*/
function setKeypair (keypair) {
if (keypair.hasOwnProperty('priv') && keypair.hasOwnProperty('pub')) {
keypair = { secretKey: keypair.priv, publicKey: keypair.pub }
console.warn('pub/priv naming for accounts has been deprecated, please use secretKey/publicKey')
}
secrets.set(this, {
secretKey: Buffer.from(keypair.secretKey, 'hex'),
publicKey: keypair.publicKey
})
}
/**
* In-memory `Account` factory
* @function
* @alias module:@aeternity/aepp-sdk/es/account/memory
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @param {Object} options.keypair - Key pair to use
* @param {String} options.keypair.publicKey - Public key
* @param {String} options.keypair.secretKey - Private key
* @return {Account}
*/
const MemoryAccount = Account.compose({
init ({ keypair }) {
try {
this.setKeypair(keypair || Crypto.envKeypair(process.env))
} catch (e) {
// Instead of throw error and crash show warning that you do not set `KEYPAIR`
// and can not sign transaction
console.log('Please provide KEY_PAIR for sign transaction')
}
},
methods: { sign, address, setKeypair }
})
export default MemoryAccount