Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
foundation
scripts
Commits
89bc0449
Commit
89bc0449
authored
Apr 9, 2022
by
Bernardo Cardoso
Browse files
Options
Downloads
Patches
Plain Diff
Add ledger interaction example
parent
cff89d6d
No related branches found
No related tags found
No related merge requests found
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ledger/index.js
+87
-0
87 additions, 0 deletions
ledger/index.js
ledger/package.json
+18
-0
18 additions, 0 deletions
ledger/package.json
ledger/yarn.lock
+1449
-0
1449 additions, 0 deletions
ledger/yarn.lock
with
1554 additions
and
0 deletions
ledger/index.js
0 → 100644
+
87
−
0
View file @
89bc0449
const
{
newElixxirApp
}
=
require
(
'
@zondax/ledger-substrate
'
);
const
TransportNodeHid
=
require
(
"
@ledgerhq/hw-transport-node-hid
"
);
const
{
ApiPromise
,
WsProvider
}
=
require
(
'
@polkadot/api
'
);
require
(
'
dotenv
'
).
config
();
const
LEDGER
=
'
6WA132Y5Ycgz3cXUt6a6rEhcJ4imhxXAij7x5Gefha5wu83g
'
;
const
ALICE
=
'
6a6XHUGV5mu33rX1vn3iivxxUR4hwoUapsuECGyHpPzfaNtt
'
;
/**
* Connect to an xx network node using websocket
* @return {Promise} chain api
*/
async
function
connect
()
{
const
provider
=
new
WsProvider
(
process
.
env
.
WS_CHAIN
);
const
api
=
await
ApiPromise
.
create
({
provider
,
});
await
api
.
isReady
;
return
api
;
}
async
function
sendAndWait
(
extrinsic
,
final
)
{
return
new
Promise
((
reject
,
resolve
)
=>
{
// Sign and send the extrinsic using our wallet
extrinsic
.
send
(({
status
})
=>
{
if
(
status
.
isInBlock
)
{
console
.
log
(
`Transfer included in block
${
status
.
asInBlock
}
`
);
if
(
!
final
)
{
resolve
();
}
}
if
(
status
.
isFinalized
&&
final
)
{
console
.
log
(
`Transfer finalized in block
${
status
.
asFinalized
}
`
);
resolve
();
}
});
})
}
async
function
main
()
{
const
transport
=
await
TransportNodeHid
.
default
.
create
();
const
app
=
newElixxirApp
(
transport
);
// Connect to the node
const
api
=
await
connect
();
// Create a extrinsic, transferring 10 units to Alice (with 9 decimals)
const
transfer
=
api
.
tx
.
balances
.
transfer
(
ALICE
,
10
_000000000
);
// Get the current nonce for the sending account
const
nonce
=
await
api
.
rpc
.
system
.
accountNextIndex
(
LEDGER
);
// Create the signing payload
// These options are for an immortal extrinsic
const
signPayload
=
api
.
registry
.
createTypeUnsafe
(
'
SignerPayload
'
,
[
{
genesisHash
:
api
.
genesisHash
,
blockHash
:
api
.
genesisHash
,
runtimeVersion
:
api
.
runtimeVersion
,
signedExtensions
:
api
.
registry
.
signedExtensions
,
version
:
4
,
specVersion
:
api
.
runtimeVersion
.
specVersion
,
transactionVersion
:
api
.
runtimeVersion
.
transactionVersion
,
nonce
:
nonce
,
address
:
LEDGER
,
method
:
transfer
.
method
,
blockNumber
:
0
,
}
]);
const
txData
=
signPayload
.
toRaw
().
data
.
substring
(
2
);
const
txBlob
=
Buffer
.
from
(
txData
,
'
hex
'
);
const
signature
=
await
app
.
sign
(
0x80000000
,
0x80000000
,
0x80000000
,
txBlob
);
// Add signature
transfer
.
addSignature
(
LEDGER
,
signature
.
signature
,
signPayload
.
toPayload
()
);
await
sendAndWait
(
transfer
,
true
);
}
main
().
catch
(
console
.
error
).
finally
(()
=>
process
.
exit
());
This diff is collapsed.
Click to expand it.
ledger/package.json
0 → 100644
+
18
−
0
View file @
89bc0449
{
"name"
:
"ledger"
,
"version"
:
"1.0.0"
,
"description"
:
"Ledger integration"
,
"main"
:
"index.js"
,
"repository"
:
"github.com/xx-labs/scripts/ledger"
,
"author"
:
"xx labs"
,
"license"
:
"MIT"
,
"scripts"
:
{
"start"
:
"node index.js"
},
"dependencies"
:
{
"@ledgerhq/hw-transport-node-hid"
:
"^6.24.1"
,
"@polkadot/api"
:
"^7.15.1"
,
"@zondax/ledger-substrate"
:
"^0.24.0"
,
"dotenv"
:
"^16.0.0"
}
}
This diff is collapsed.
Click to expand it.
ledger/yarn.lock
0 → 100644
+
1449
−
0
View file @
89bc0449
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment