Author

Topic: PowerCoin -- A Powershell Wrapper for Bitcoin json-rpc (Read 1254 times)

newbie
Activity: 29
Merit: 0
Version 0.02 Signature: HzA9kY/zltEYsPO8QoZWn2BXNralYyIpjC42vKdSGQEzEIvqMNQej1+0dJKwZlBwgIodf2iYwv42wW2e7ZtrJ6c=

UPDATE
Code:
##Title: PowerCoin
##Version: 0.02
##Date: 6/10/2014
##Author: jagallout
##Usage: It works best to dot source the .ps1 file (e.g. PS C:\[path to PowerCoin.ps1]> . .\PowerCoin.ps1 )
##This will prompt you for a username/password and for the Uri for the rpc server, then you can just call the methods by name (e.g. getinfo)
##Question: jagallout on bitcointalk.org
##Tips: 1GqrY1LSRD4N99LQn4ULhSSoJ79B7rtC6W


$cred = Get-Credential
$Uri = Read-Host "Specify RPC Server"

$p_jsonrpc = "`"jsonrpc`":2.0"
$p_id = "`"id`":1"

$passwordFile = "~/powerCoin-info.txt"

function saveCredentials
{
    $kvp = ($cred.Username +"::::"+(ConvertFrom-SecureString $cred.Password))
    Set-Content -Path $passwordFile -Value $kvp
}

function loadCredentials
{
    if ([System.IO.File]::Exists($passwordFile))
    {
        $content = Get-Content $passwordFile
        $values = $content.Split("::::")
        $username = $values[0]
        $password = $values[1]
   
        $cred.Password = ConvertTo-SecureString $password
        $cred.Username = $username
    }
    else
    {
        $cred = Get-Credential
    }
}

function execute
{

    param
    (
    $json
    );

    $result = Invoke-WebRequest -Uri $Uri -Method Post -ContentType "application/json-rpc" -Credential $cred -Body $json

    return $result.Content
}

function getInfo
{

    $json =
@"
{"method":"getinfo",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getblockcount
{

    $json =
@"
{"method":"getblockcount",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getbestblockhash
{

    $json =
@"
{"method":"",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getdifficulty
{

    $json =
@"
{"method":"getdifficulty",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getreceivedbyaddress
{

    Param
    (
    [string]$address,
    [int]$confirmations
    );

    $params = "[`"$address`",$confirmations]"
   
    $json =
@"
{"method":"getreceivedbyaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
     
}

function signmessage
{
    Param
    (
    [parameter(mandatory=$true)][string]$address,
    [parameter(mandatory=$true)][string]$message
    );

    $params = "[`"$address`",`"$message`"]"

    $json =
@"
{"method":"signmessage","params":$($params.ToString()), $p_id, $P_jsonrpc}
"@

    return execute($json)
}

function verifymessage
{
    Param
    (
    [parameter(mandatory=$true)][string]$address,
    [parameter(mandatory=$true)][string]$signature,
    [parameter(mandatory=$true)][string]$message
    );

    $params = "[`"$address`",`"$signature`",`"$message`"]"
   
    $json =
@"
{"method":"verifymessage","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
     
}

function getbalance
{

    Param
    (
    [string]$account,
    [int]$confirmations
    );
   
    if (!$account) {$account = ""}
    if (!$confirmations) {$confirmations = 6}
   
    $params = "[`"$account`", $confirmations]"

    $json =
@"
{"method":"getbalance","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getaccount
{
    Param
    (
    [string]$account
    );

    $params = "[`"$account`"]"

    $json =
@"
{"method":"getaccount","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function getaccountaddress
{
    Param
    (
    [string]$account
    );
   
    $params = "[`"$account`"]"

    $json =
@"
{"method":"getaccountaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function dumpprivkey
{
    Param
    (
    [parameter(mandatory=$true)][string]$address
    );

    $params = "[`"$address`"]"

    $json =
@"
{"method":"dumpprivkey","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@
   
    return execute($json)
}

function getnewaddress
{
    Param
    (
    [string]$account
    );
   
    $params = "[`"$account`"]"

    $json =
@"
{"method":"getnewaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@
    return execute($json)
}

function gettransaction
{
    Param
    (
    [parameter(mandatory=$true)][string]$txid
    );

    $params = "[`"$txid`"]"

    $json =
@"
{"method":"gettransaction","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)

}

function sendtoaddress #TODO: Test Functionality
{
    param
    (
        [parameter(mandatory=$true)][string]$address,
        [parameter(mandatory=$true)][decimal]$amount #api calls for "real", is decimal okay to use?
    );

    $params = "[`"$address`",$amount]"

    $json =
@"
{"method":"sendtoaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function setaccount
{
    param
    (
        [parameter(mandatory=$true)][string]$address,
        [parameter(mandatory=$true)][string]$account
    );

    $params = "[`"$address`",`"$account`"]"

    $json =
@"
{"method":"setaccount","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function setgenerate
{
    param
    (
        [parameter(mandatory=$true)][bool]$enable,
        [parameter(mandatory=$false)][int]$proclimit
    );
    if ($enable) {$enableText = "true"}else{$enableText = "false"}
    if (!$proclimit){$proclimit = -1}

    $params = "[`"$enableText`",$proclimit]"

    $json =
@"
{"method":"setgenerate","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function settxfee
{
    param
    (
        [parameter(mandatory=$true)][decimal]$fee #api calls for "real", is decimal okay to use?
    );

    $params = "[`"$fee`"]"

    $json =
@"
{"method":"settxfee","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function stop
{
    $json =
@"
{"method":"stop", $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function validateaddress
{
    param
    (
        [parameter(mandatory=$true)][string]$address
    );

    $params = "[`"$address`"]"

    $json =
@"
{"method":"validateaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function walletlock
{
    $json =
@"
{"method":"walletlock", $p_id, $p_jsonrpc}
"@
    return execute($json)
}

function walletpassphrase
{
    param
    (
        [parameter(mandatory=$true)][string]$passphrase,
        [parameter(mandatory=$true)][int]$timeout
    );

    $params = "[`"$passphrase`", $timeout]"

    $json =
@"
{"method":"walletpassphrase","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function walletpassphrasechange
{
    param
    (
        [parameter(mandatory=$true)][string]$old,
        [parameter(mandatory=$true)][string]$new
    );

    $params = "[`"$old`", `"$new`"]"

    $json =
@"
{"method":"walletpassphrasechange","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}
hero member
Activity: 756
Merit: 500
CryptoTalk.Org - Get Paid for every Post!
PowerShell, spew! Lol I've spent far too much time working with it recently - good work!
newbie
Activity: 29
Merit: 0
Hello All,

I am a hobbyist .Net developer and had been trying (unsuccessfully) to use the "Bitnet" project (http://sourceforge.net/projects/bitnet/) for a while.  I finally got fed up and decided to start from scratch, below you will find the "first-pass"/"proof of concept" implementation of some of the "more commonly" used bitcoind rpc calls.  Most of the work is in the string formatting, and you will still have to handle the json string that gets returned, but its a start, and hopefully someone will find it useful.

It makes use of Invoke-WebRequest command available in Powershell 4.0

Any input would be appreciated!  Feel free to use/distribute to your hearts content, I simply ask that any improvements or extensions make their way back to this thread.

Enjoy:
copy the following text into a new file and save it as PowerCoin.ps1

H07ZmHjToR9k3L6+FtInvboy3TQktffyrVKph2hRnhHmZwOu1zomf9kgVBThEaCLbo0Kb5VfSpesiTvmsCnNBiE= (Signed with my Tip Address)

Code:
##Title: PowerCoin
##Version: 0.01
##Date: 6/7/2014
##Author: jagallout
##Usage: It works best to dot source the .ps1 file (e.g. PS C:\[path to PowerCoin.ps1]> . .\PowerCoin.ps1 )
##This will prompt you for a username/password and for the Uri for the rpc server, then you can just call the methods by name (e.g. getinfo)
##Question: jagallout on bitcointalk.org
##Tips: 1GqrY1LSRD4N99LQn4ULhSSoJ79B7rtC6W


$cred = Get-Credential
$Uri = Read-Host "Specify RPC Server"

$p_jsonrpc = "`"jsonrpc`":2.0"
$p_id = "`"id`":1"

function execute
{

    param
    (
    $json
    );

    $result = Invoke-WebRequest -Uri $Uri -Method Post -ContentType "application/json-rpc" -Credential $cred -Body $json

    return $result.Content
}

function getInfo
{

    $json =
@"
{"method":"getinfo",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getblockcount
{

    $json =
@"
{"method":"getblockcount",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getbestblockhash
{

    $json =
@"
{"method":"",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getdifficulty
{

    $json =
@"
{"method":"getdifficulty",$p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getreceivedbyaddress
{

    Param
    (
    [string]$address,
    [int]$confirmations
    );

    $params = "[`"$address`",$confirmations]"
    
    $json =
@"
{"method":"getreceivedbyaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
    
}

function signmessage
{
    Param
    (
    [parameter(mandatory=$true)][string]$address,
    [parameter(mandatory=$true)][string]$message
    );

    $params = "[`"$address`",`"$message`"]"

    $json =
@"
"@

    return execute($json)
}

function verifymessage
{
    Param
    (
    [parameter(mandatory=$true)][string]$address,
    [parameter(mandatory=$true)][string]$signature,
    [parameter(mandatory=$true)][string]$message
    );

    $params = "[`"$address`",`"$signature`",`"$message`"]"
    
    $json =
@"
{"method":"verifymessage","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
    
}

function getbalance
{

    Param
    (
    [string]$account,
    [int]$confirmations
    );
  
    if (!$account) {$account = ""}
    if (!$confirmations) {$confirmations = 6}
    
    $params = "[`"$account`", $confirmations]"

    $json =
@"
{"method":"getbalance","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)

}

function getaccount
{
    Param
    (
    [string]$account
    );

    $params = "[`"$account`"]"

    $json =
@"
{"method":"getaccount","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function getaccountaddress
{
    Param
    (
    [string]$account
    );
    
    $params = "[`"$account`"]"

    $json =
@"
{"method":"getaccountaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)
}

function dumpprivkey
{
    Param
    (
    [parameter(mandatory=$true)][string]$address
    );

    $params = "[`"$address`"]"

    $json =
@"
{"method":"dumpprivkey","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@
    
    return execute($json)
}

function getnewaddress
{
    Param
    (
    [string]$account
    );
    
    $params = "[`"$account`"]"

    $json =
@"
{"method":"getnewaddress","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@
    return execute($json)
}

function gettransaction
{
    Param
    (
    [parameter(mandatory=$true)][string]$txid
    );

    $params = "[`"$txid`"]"

    $json =
@"
{"method":"gettransaction","params":$($params.ToString()), $p_id, $p_jsonrpc}
"@

    return execute($json)

}
Jump to: