Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(guide): Adjust low-lvl API #887

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 14 additions & 139 deletions docs/examples/node/aecrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,6 @@ const promptSchema = {




## Key Extraction (from Node nodes)




```js
function extractReadableKeys (dir, options) {
const pwd = options.input
prompt.start()
prompt.get(promptSchema, (_, { password }) => {
const key = fs.readFileSync(path.join(pwd, dir, 'sign_key'))
const pubKey = fs.readFileSync(path.join(pwd, dir, 'sign_key.pub'))

const decrypted = Crypto.decryptPrivateKey(password, key)

const privateHex = Buffer.from(decrypted).toString('hex')
const decryptedPub = Crypto.decryptPubKey(password, pubKey)

console.log(`Private key (hex): ${privateHex}`)
console.log(`Public key (base check): ak_${Crypto.encodeBase58Check(decryptedPub)}`)
console.log(`Public key (hex): ${decryptedPub.toString('hex')}`)
})
}


```







## Key Pair Generation


Expand All @@ -147,19 +113,9 @@ function extractReadableKeys (dir, options) {
```js
function generateKeyPair (name, { output }) {
const { publicKey, secretKey } = Crypto.generateKeyPair()

const data = [
[path.join(output, name), secretKey],
[path.join(output, `${name}.pub`), publicKey]
]

data.forEach(([path, data]) => {
fs.writeFileSync(path, data)
console.log(`Wrote ${path}`)
})
return { publicKey, secretKey }

}


```


Expand All @@ -170,94 +126,27 @@ function generateKeyPair (name, { output }) {

## Transaction Signing

This function shows how to use a compliant private key to sign an æternity
This function shows how to sign an æternity
transaction and turn it into an RLP-encoded tuple ready for mining




```js
function signTx (tx, privKey) {
function signTx (tx, secretKey) {
if (!tx.match(/^tx_.+/)) {
throw Error('Not a valid transaction')
}

const binaryKey = (() => {
if (program.file) {
return fs.readFileSync(program.file)
} else if (privKey) {
return Buffer.from(privKey, 'hex')
} else {
throw Error('Must provide either [privkey] or [file]')
}
})()

const decryptedKey = program.password ? Crypto.decryptKey(program.password, binaryKey) : binaryKey


```







Split the base58Check part of the transaction




```js
const base58CheckTx = tx.split('_')[1]

```







... and sign the binary create_contract transaction


secretKey = Buffer.from(keyPair.secretKey, 'hex')
const rlpBinaryTx = Crypto.decodeBase64Check(Crypto.assertedType(tx, 'tx'))
// Prepend `NETWORK_ID` to begin of data binary
const txWithNetworkId = Buffer.concat([Buffer.from(program.networkId), rlpBinaryTx])


```js
const binaryTx = Crypto.decodeBase58Check(base58CheckTx)

const signature = Crypto.sign(binaryTx, decryptedKey)


```







the signed tx deserializer expects a 4-tuple:
<tag, version, signatures_array, binary_tx>




```js
const unpackedSignedTx = [
Buffer.from([11]),
Buffer.from([1]),
[Buffer.from(signature)],
binaryTx
]

console.log(Crypto.encodeTx(unpackedSignedTx))
const signatures = [Crypto.sign(txWithNetworkId, secretKey)]
const { tx } = buildTx({ encodedTx: rlpBinaryTx, signatures }, TX_TYPE.signed)
console.log('Signed transaction: ' + tx)
}


```

````



Expand All @@ -276,8 +165,6 @@ function unpackTx (tx) {
const deserializedTx = TxBuilder.unpackTx(tx)
console.log(JSON.stringify(deserializedTx, undefined, 2))
}


```


Expand All @@ -296,22 +183,10 @@ The `commander` library provides maximum command line parsing convenience.
```js
program.version('0.1.0')

program
.command('decrypt <directory>')
.description('Decrypts public and private key to readable formats for testing purposes')
.option('-i, --input [directory]', 'Directory where to look for keys', '.')
.action(extractReadableKeys)

program
.command('genkey <keyname>')
.description('Generate keypair')
.option('-o, --output [directory]', 'Output directory for the keys', '.')
.action(generateKeyPair)

program
.command('sign <tx> [privkey]')
.option('-p, --password [password]', 'password of the private key')
.option('-f, --file [file]', 'private key file')
.command('sign <tx> <priv>')
.option('--networkId [networkId]', 'Network Id')
.action(signTx)

program
Expand Down
22 changes: 12 additions & 10 deletions docs/guides/low-vs-high-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ Example spend function, using aeternity's SDK abstraction
Example spend function, using the SDK, talking directly to the [**API**](https://github.com/aeternity/protocol/tree/master/node/api):
```js
// Import necessary Modules
import Tx from '@aeternity/aepp-sdk/es/tx/tx.js'
import Chain from '@aeternity/aepp-sdk/es/chain/node.js'
import Account from '@aeternity/aepp-sdk/es/account/memory.js'
import Node from '@aeternity/aepp-sdk/es/node' // import from SDK es-modules
import Universal from '@aeternity/aepp-sdk/es/ae/universal'
import Node from '@aeternity/aepp-sdk/es/node'
import MemoryAccount from '@aeternity/aepp-sdk/es/account/memory'

async function spend (amount, receiver_pub_key) {
const node = await Node({ url, internalUrl })
const nodes = [{ name: 'testnet-node', instance: node }]
const account = MemoryAccount({ keypair: { secretKey: 'PRIV_KEY_HERE', publicKey: 'PUB_KEY_HERE'} })

const sdkInstance = await Universal({
nodes,
accounts: [account]
})

const tx = await Tx({ nodes })
const chain = await Chain({ nodes })
const account = Account({keypair: {secretKey: 'PRIV_KEY_HERE', publicKey: 'PUB_KEY_HERE'}, networkId: 'NETWORK_ID_HERE'})
const spendTx = await tx.spendTx({ sender: 'PUB_KEY_HERE', receiver_pub_key, amount })
const spendTx = await sdkInstance.spendTx({ sender: await sdkInstance.address(), receiver_pub_key, amount })

const signed = await account.signTransaction(spendTx)
return chain.sendTransaction(signed, opt)
const signed = await sdkInstance.signTransaction(spendTx)
return sdkInstance.sendTransaction(signed, opt)

}
```