A software engineer has been jailed for five years for ordering a handgun and ammunition using the dark web.
US border agents spotted the weapon concealed within a package addressed to 48-year-old David Mitchell at his office in Dunfermline, Fife.
An operation by the Organised Crime Partnership in Scotland saw him being placed under surveillance while a dummy package was sent instead.
Mitchell was then arrested following a search of his Edinburgh home.
Mitchell, who spent more than 2,000 units of cryptocurrency on the purchase, admitted firearms charges at the High Court in Edinburgh last December.
The case is being hailed as the first major success for the new Organised Crime Partnership (Scotland), which aims to build on the relationship between Police Scotland and the National Crime Agency (NCA).
The package sent to Mitchell, which was intercepted, contained an amplifier.
Hidden inside was a fully operational Glock handgun, a silencer and bullets.
Advocate depute Liam Ewing earlier told the court how the Organised Crime Partnership created a package designed to look like the original.
Mitchell was put under surveillance and a cardboard box containing the dummy weapon was delivered to his workplace in Dunfermline.
Mr Ewing said: “The accused was seen signing for and taking delivery of the package.
“Later that day the accused was observed exiting the premises and placing the box in his car before driving home.”
The prosecutor said that Mitchell “appeared distant” and was staring into space. He said that he suffered from depression and took medication for the condition.
Lord Pentland told Mitchell: “These offences arose from your planned and deliberate conduct in attempting to acquire a working Glock pistol and a quantity of suitable ammunition.
“It appears you formed a plan to obtain these items by carrying out research on the dark web.”
The judge said he noted that Mitchell said he had no intention to cause harm to anyone, but said they were serious offences.
Mitchell admitted purchasing and attempting to possess the pistol, silencer and ammunition between 17 and 19 September 2018 and attempting to possess a prohibited firearm.
Defence counsel John Scott QC said: “It is a very unusual case. In all respects he is someone your lordship might never expect to see before a court.”
He said Mitchell was assessed as posing little risk of reconviction.
Det Ch Supt Gerry McLean, of Police Scotland, said: “David Mitchell tried to bypass Scottish, UK and American laws as he attempted to purchase a dangerous firearm and bring it into this country.
“The Organised Crime Partnership (Scotland), which launched on 1st September 2018, took on this inquiry and saw officers from Police Scotland and NCA using a range of specialist skills to investigate Mitchell before enforcement activity was conducted at two properties linked to him.
“His sentence should serve as a reminder that organised crime offences will not be tolerated.”
Rob Burgess, NCA regional head of investigations, said: “This conviction is an important milestone for law enforcement in Scotland, and results like this show what can be achieved by close working between the NCA and Police Scotland.
“There is a strong link between illegal drugs supply and the use of firearms, with criminals seeking them to intimidate rivals and enforce control of criminal operations.”
Andrew Laing, deputy procurator fiscal specialist casework, said: “This was an organised and premeditated effort to bring an illegal firearm, ammunition and silencer into Scotland and the conviction and sentence granted for David Mitchell sends a strong message to others that this kind of criminal behaviour will not be tolerated.”