Authentication
Table of Contents
How message signing works in spv-wallet-go-client library?
SetSignature method
Signing process is a part of SetSignature
:
Algorithm Overview
The algorithm is presented below:
The createSignature function takes a private key (xPriv) and body contents (bodyString) as input.
It retrieves the extended public key (xPub) from the xPriv using the GetExtendedPublicKey function. This is used for verification purposes.
A random and unique string (AuthNonce) is generated as the authentication nonce to seed the signing message.
The AuthNonce and xPriv are used to derive a child extended key using the DeriveChildKeyFromHex function.
The child extended key is then converted to a private key (privateKey) using the GetPrivateKeyFromHDKey function.
The createSignatureCommon function is called with the necessary parameters to create the signature.
In createSignatureCommon, the message body is hashed using the SHA-256 algorithm, and the result is assigned to AuthHash.
The current timestamp in milliseconds is assigned to AuthTime.
The signature is generated using the bitcoin.SignMessage function, which signs the message using the private key and the signing message composed of xPub, AuthHash, AuthNonce, and AuthTime.
The signing algorithm used in the bitcoin.SignMessage function internally uses the SHA-256 algorithm for hashing the message.
Finally, the createSignatureCommon function returns the AuthPayload containing the extended public key, authentication nonce, authentication hash, authentication time, and signature.
Created headers
Auth headers
x-auth-key
-> xpub
Signature headers
x-auth-hash
-> sha256 hash of the body stringx-auth-nonce
-> random stringx-auth-time
-> timestamp in millisecondsx-auth-signature
-> signature
Get Transaction endpoint example
Create the client instance with Access Key - Get Transaction endpoint example
First we can call the New
method from spv-wallet-go-client library:
As the parameter we can specify:
options
are used to provide the url of the SPV Wallet and to enable/disable debugging mode.
the signRequest option is used to enable/disable signing all messages send to the SPV Wallet.
The most important for us is the walletclient.WithAccessKey(accessKey)
option, which is used to provide the access key.
In a return we will receive a spv-wallet-go-client instance prepared to communicate with the server and an error if something went wrong.
What is behind the New method?
In above example we use the .WithAccessKey method so the below code was be executed:
so after this the accessKeyString
field of the walletclient instance will be set to the value of the accessKeyString
parameter.
It takes us to the walletclient.go
file inside spv-wallet-go-client library and the else if
statement related to the accessKeyString
will be executed:
to summarize the above code, the accessKeyString
is decoded to the accessKey
and stored in the accessKey
field of the walletclient instance. There are two dependencies used here:
wif
fromgithub.com/libsv/go-bk/wif
,bitcoin
fromgithub.com/bitcoinschema/go-bitcoin/v2
.
If you want to know more how the decode wif process works, you can read the short docs here.
Sending request to the SPV Wallet - access key authentication
To send a SPV Wallet request from a client library we use the doHTTPRequest
(transports/http.go
) method which is a wrapper for the http.Do
method from the net/http
library.
as we set the accessKey
in the walletclient instance, the authenticateWithAccessKey
method will be executed:
This method will preare request headers presented in this section: Created headers
Create transaction endpoint example
Create the client instance with Access Key - Create Transaction endpoint example
The difference between the Get Transaction
and Create Transaction
endpoints is that the Create Transaction
endpoint requires the xPriv
key to be set in the walletclient instance instead of the accessKey
.
the proper if/else statement will be executed and the authenticateWithXpriv method will be called:
for the DraftTransaction
endpoint the sign
parameter is set to true
so the addSignature
method will be executed.
Body string will be signed with xpriv with createSignature
and the same headers as in the Get Transaction
endpoint will be added to the request.
Last updated